mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.12] Further improve docs for typing.Annotated
(GH-105498) (#105503)
Further improve docs for `typing.Annotated` (GH-105498)
(cherry picked from commit d213c2990f
)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
2b6f475db8
commit
3c5f0eadd8
2 changed files with 81 additions and 57 deletions
|
@ -2021,7 +2021,7 @@ class Annotated:
|
|||
|
||||
assert Annotated[int, '$'].__metadata__ == ('$',)
|
||||
|
||||
- Nested Annotated are flattened::
|
||||
- Nested Annotated types are flattened::
|
||||
|
||||
assert Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
|
||||
|
||||
|
@ -2032,15 +2032,17 @@ class Annotated:
|
|||
|
||||
- Annotated can be used as a generic type alias::
|
||||
|
||||
Optimized = Annotated[T, runtime.Optimize()]
|
||||
assert Optimized[int] == Annotated[int, runtime.Optimize()]
|
||||
type Optimized[T] = Annotated[T, runtime.Optimize()]
|
||||
# type checker will treat Optimized[int]
|
||||
# as equivalent to Annotated[int, runtime.Optimize()]
|
||||
|
||||
OptimizedList = Annotated[List[T], runtime.Optimize()]
|
||||
assert OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
|
||||
type OptimizedList[T] = Annotated[list[T], runtime.Optimize()]
|
||||
# type checker will treat OptimizedList[int]
|
||||
# as equivalent to Annotated[list[int], runtime.Optimize()]
|
||||
|
||||
- Annotated cannot be used with an unpacked TypeVarTuple::
|
||||
|
||||
Annotated[*Ts, Ann1] # NOT valid
|
||||
type Variadic[*Ts] = Annotated[*Ts, Ann1] # NOT valid
|
||||
|
||||
This would be equivalent to::
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue