@@ -474,12 +474,12 @@ classes without a metaclass conflict.
474
474
Type variables with an upper bound
475
475
----------------------------------
476
476
477
- A type variable may specify an upper bound using ``bound=<type> `` (when
478
- using the ``TypeVar `` constructor) or using ``: <type> `` (when using the native
479
- syntax for generics). The bound itself cannot be parameterized by type variables.
480
- This means that an
481
- actual type substituted (explicitly or implicitly) for the type variable must
482
- be a subtype of the boundary type. Example::
477
+ A type variable may specify an upper bound using ``bound=<type> `` (when using
478
+ the ``TypeVar `` constructor) or using ``: <type> `` (when using the native
479
+ syntax for generics). The bound itself cannot be parameterized by type
480
+ variables. This means that an actual type substituted (explicitly or
481
+ implicitly) for the type variable must be :term: ` assignable ` to the bound.
482
+ Example::
483
483
484
484
from typing import TypeVar
485
485
from collections.abc import Sized
@@ -496,11 +496,10 @@ be a subtype of the boundary type. Example::
496
496
longer({1}, {1, 2}) # ok, return type set[int]
497
497
longer([1], {1, 2}) # ok, return type a supertype of list[int] and set[int]
498
498
499
- An upper bound cannot be combined with type constraints (as used in
500
- ``AnyStr ``, see the example earlier); type constraints cause the
501
- inferred type to be *exactly * one of the constraint types, while an
502
- upper bound just requires that the actual type is a subtype of the
503
- boundary type.
499
+ An upper bound cannot be combined with type constraints (as used in ``AnyStr ``,
500
+ see the example earlier); type constraints cause the inferred type to be
501
+ *exactly * one of the constraint types, while an upper bound just requires that
502
+ the actual type is :term: `assignable ` to the bound.
504
503
505
504
.. _`variance` :
506
505
@@ -523,13 +522,12 @@ introduction to these concepts can be found on `Wikipedia
523
522
<https://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29> `_ and in :pep: `483 `; here we just show how to control
524
523
a type checker's behavior.
525
524
526
- By default generic types declared using the old ``TypeVar `` syntax
527
- are considered *invariant * in all type variables,
528
- which means that values for variables annotated with types like
529
- ``list[Employee] `` must exactly match the type annotation -- no subclasses or
530
- superclasses of the type parameter (in this example ``Employee ``) are
531
- allowed. See below for the behavior when using the built-in generic syntax
532
- in Python 3.12 and higher.
525
+ By default generic types declared using the old ``TypeVar `` syntax are
526
+ considered *invariant * in all type variables, which means that e.g.
527
+ ``list[Manager] `` is neither a supertype nor a subtype of ``list[Employee] ``.
528
+
529
+ See below for the behavior when using the built-in generic syntax in Python
530
+ 3.12 and higher.
533
531
534
532
To facilitate the declaration of container types where covariant or
535
533
contravariant type checking is acceptable, type variables accept keyword
@@ -1926,7 +1924,7 @@ Using a type parameter from an outer scope as a default is not supported.
1926
1924
Bound Rules
1927
1925
^^^^^^^^^^^
1928
1926
1929
- ``T1 ``'s bound must be a subtype of ``T2 ``'s bound.
1927
+ ``T1 ``'s bound must be :term: ` assignable ` to ``T2 ``'s bound.
1930
1928
1931
1929
::
1932
1930
@@ -2022,8 +2020,8 @@ normal subscription rules, non-overridden defaults should be substituted.
2022
2020
Using ``bound `` and ``default ``
2023
2021
"""""""""""""""""""""""""""""""
2024
2022
2025
- If both ``bound `` and ``default `` are passed, ``default `` must be a
2026
- subtype of ``bound ``. If not, the type checker should generate an
2023
+ If both ``bound `` and ``default `` are passed, ``default `` must be
2024
+ :term: ` assignable ` to ``bound ``. If not, the type checker should generate an
2027
2025
error.
2028
2026
2029
2027
::
@@ -2268,7 +2266,8 @@ Use in Attribute Annotations
2268
2266
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2269
2267
2270
2268
Another use for ``Self `` is to annotate attributes. One example is where we
2271
- have a ``LinkedList `` whose elements must be subclasses of the current class.
2269
+ have a ``LinkedList `` whose elements must be :term: `assignable ` to the current
2270
+ class.
2272
2271
2273
2272
::
2274
2273
@@ -2298,8 +2297,8 @@ constructions with subclasses:
2298
2297
def ordinal_value(self) -> str:
2299
2298
return as_ordinal(self.value)
2300
2299
2301
- # Should not be OK because LinkedList[int] is not a subclass of
2302
- # OrdinalLinkedList, # but the type checker allows it.
2300
+ # Should not be OK because LinkedList[int] is not assignable to
2301
+ # OrdinalLinkedList, but the type checker allows it.
2303
2302
xs = OrdinalLinkedList(value=1, next=LinkedList[int](value=2))
2304
2303
2305
2304
if xs.next:
@@ -2469,11 +2468,11 @@ See :pep:`PEP 544
2469
2468
<544#self-types-in-protocols >` for
2470
2469
details on the behavior of TypeVars bound to protocols.
2471
2470
2472
- Checking a class for compatibility with a protocol: If a protocol uses
2473
- `` Self `` in methods or attribute annotations, then a class ``Foo `` is
2474
- considered compatible with the protocol if its corresponding methods and
2475
- attribute annotations use either ``Self `` or ``Foo `` or any of ``Foo ``’s
2476
- subclasses. See the examples below:
2471
+ Checking a class for assignability to a protocol: If a protocol uses `` Self ``
2472
+ in methods or attribute annotations, then a class ``Foo `` is :term: ` assignable `
2473
+ to the protocol if its corresponding methods and attribute annotations use
2474
+ either ``Self `` or ``Foo `` or any of ``Foo ``’s subclasses. See the examples
2475
+ below:
2477
2476
2478
2477
::
2479
2478
@@ -2705,16 +2704,15 @@ by the ``TypeVar`` constructor call. No further inference is needed.
2705
2704
3. Create two specialized versions of the class. We'll refer to these as
2706
2705
``upper `` and ``lower `` specializations. In both of these specializations,
2707
2706
replace all type parameters other than the one being inferred by a dummy type
2708
- instance (a concrete anonymous class that is type compatible with itself and
2709
- assumed to meet the bounds or constraints of the type parameter). In
2710
- the ``upper `` specialized class, specialize the target type parameter with
2711
- an ``object `` instance. This specialization ignores the type parameter's
2712
- upper bound or constraints. In the ``lower `` specialized class, specialize
2713
- the target type parameter with itself (i.e. the corresponding type argument
2714
- is the type parameter itself).
2715
-
2716
- 4. Determine whether ``lower `` can be assigned to ``upper `` using normal type
2717
- compatibility rules. If so, the target type parameter is covariant. If not,
2707
+ instance (a concrete anonymous class that is assumed to meet the bounds or
2708
+ constraints of the type parameter). In the ``upper `` specialized class,
2709
+ specialize the target type parameter with an ``object `` instance. This
2710
+ specialization ignores the type parameter's upper bound or constraints. In the
2711
+ ``lower `` specialized class, specialize the target type parameter with itself
2712
+ (i.e. the corresponding type argument is the type parameter itself).
2713
+
2714
+ 4. Determine whether ``lower `` can be assigned to ``upper `` using normal
2715
+ assignability rules. If so, the target type parameter is covariant. If not,
2718
2716
determine whether ``upper `` can be assigned to ``lower ``. If so, the target
2719
2717
type parameter is contravariant. If neither of these combinations are
2720
2718
assignable, the target type parameter is invariant.
@@ -2737,9 +2735,8 @@ To determine the variance of ``T1``, we specialize ``ClassA`` as follows:
2737
2735
upper = ClassA[object, Dummy, Dummy]
2738
2736
lower = ClassA[T1, Dummy, Dummy]
2739
2737
2740
- We find that ``upper `` is not assignable to ``lower `` using normal type
2741
- compatibility rules defined in :pep: `484 `. Likewise, ``lower `` is not assignable
2742
- to ``upper ``, so we conclude that ``T1 `` is invariant.
2738
+ We find that ``upper `` is not assignable to ``lower ``. Likewise, ``lower `` is
2739
+ not assignable to ``upper ``, so we conclude that ``T1 `` is invariant.
2743
2740
2744
2741
To determine the variance of ``T2 ``, we specialize ``ClassA `` as follows:
2745
2742
0 commit comments