Skip to content

Commit 41a8509

Browse files
ilevkivskyigvanrossum
authored andcommitted
Conventions for variance terminology (#257)
Fixes #211
1 parent bbbc35e commit 41a8509

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

python2/test_typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ def test_covariance_tuple(self):
827827

828828
def test_covariance_sequence(self):
829829
# Check covariance for Sequence (which is just a generic class
830-
# for this purpose, but using a covariant type variable).
830+
# for this purpose, but using a type variable with covariant=True).
831831
self.assertIsSubclass(typing.Sequence[Manager],
832832
typing.Sequence[Employee])
833833
self.assertNotIsSubclass(typing.Sequence[Employee],

python2/typing.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,10 @@ def longest(x: A, y: A) -> A:
385385
and issubclass(bytes, A) are true, and issubclass(int, A) is
386386
false. (TODO: Why is this needed? This may change. See #136.)
387387
388-
Type variables may be marked covariant or contravariant by passing
389-
covariant=True or contravariant=True. See PEP 484 for more
390-
details. By default type variables are invariant.
388+
Type variables defined with covariant=True or contravariant=True
389+
can be used do declare covariant or contravariant generic types.
390+
See PEP 484 for more details. By default generic types are invariant
391+
in all type variables.
391392
392393
Type variables can be introspected. e.g.:
393394
@@ -406,7 +407,7 @@ def __new__(cls, name, *constraints, **kwargs):
406407
contravariant = kwargs.get('contravariant', False)
407408
self = super(TypeVar, cls).__new__(cls, name, (Final,), {})
408409
if covariant and contravariant:
409-
raise ValueError("Bivariant type variables are not supported.")
410+
raise ValueError("Bivariant types are not supported.")
410411
self.__covariant__ = bool(covariant)
411412
self.__contravariant__ = bool(contravariant)
412413
if constraints and bound is not None:
@@ -1055,7 +1056,7 @@ def __subclasscheck__(self, cls):
10551056
if cls is Any:
10561057
return True
10571058
if isinstance(cls, GenericMeta):
1058-
# For a class C(Generic[T]) where T is co-variant,
1059+
# For a covariant class C(Generic[T]),
10591060
# C[X] is a subclass of C[Y] iff X is a subclass of Y.
10601061
origin = self.__origin__
10611062
if origin is not None and origin is cls.__origin__:
@@ -1389,7 +1390,7 @@ class MutableSet(AbstractSet[T]):
13891390
__extra__ = collections_abc.MutableSet
13901391

13911392

1392-
# NOTE: Only the value type is covariant.
1393+
# NOTE: It is only covariant in the value type.
13931394
class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co]):
13941395
__extra__ = collections_abc.Mapping
13951396

@@ -1523,11 +1524,11 @@ def __new__(cls, *args, **kwds):
15231524

15241525

15251526
# Internal type variable used for Type[].
1526-
CT = TypeVar('CT', covariant=True, bound=type)
1527+
CT_co = TypeVar('CT_co', covariant=True, bound=type)
15271528

15281529

15291530
# This is not a real generic class. Don't use outside annotations.
1530-
class Type(type, Generic[CT]):
1531+
class Type(type, Generic[CT_co]):
15311532
"""A special construct usable to annotate class objects.
15321533
15331534
For example, suppose we have the following classes::

src/test_typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ def test_covariance_tuple(self):
852852

853853
def test_covariance_sequence(self):
854854
# Check covariance for Sequence (which is just a generic class
855-
# for this purpose, but using a covariant type variable).
855+
# for this purpose, but using a type variable with covariant=True).
856856
self.assertIsSubclass(typing.Sequence[Manager],
857857
typing.Sequence[Employee])
858858
self.assertNotIsSubclass(typing.Sequence[Employee],

src/typing.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,10 @@ def longest(x: A, y: A) -> A:
382382
and issubclass(bytes, A) are true, and issubclass(int, A) is
383383
false. (TODO: Why is this needed? This may change. See #136.)
384384
385-
Type variables may be marked covariant or contravariant by passing
386-
covariant=True or contravariant=True. See PEP 484 for more
387-
details. By default type variables are invariant.
385+
Type variables defined with covariant=True or contravariant=True
386+
can be used do declare covariant or contravariant generic types.
387+
See PEP 484 for more details. By default generic types are invariant
388+
in all type variables.
388389
389390
Type variables can be introspected. e.g.:
390391
@@ -399,7 +400,7 @@ def __new__(cls, name, *constraints, bound=None,
399400
covariant=False, contravariant=False):
400401
self = super().__new__(cls, name, (Final,), {}, _root=True)
401402
if covariant and contravariant:
402-
raise ValueError("Bivariant type variables are not supported.")
403+
raise ValueError("Bivariant types are not supported.")
403404
self.__covariant__ = bool(covariant)
404405
self.__contravariant__ = bool(contravariant)
405406
if constraints and bound is not None:
@@ -1038,7 +1039,7 @@ def __subclasscheck__(self, cls):
10381039
if cls is Any:
10391040
return True
10401041
if isinstance(cls, GenericMeta):
1041-
# For a class C(Generic[T]) where T is co-variant,
1042+
# For a covariant class C(Generic[T]),
10421043
# C[X] is a subclass of C[Y] iff X is a subclass of Y.
10431044
origin = self.__origin__
10441045
if origin is not None and origin is cls.__origin__:
@@ -1440,7 +1441,7 @@ class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
14401441
pass
14411442

14421443

1443-
# NOTE: Only the value type is covariant.
1444+
# NOTE: It is only covariant in the value type.
14441445
class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
14451446
extra=collections_abc.Mapping):
14461447
pass
@@ -1577,11 +1578,11 @@ def __new__(cls, *args, **kwds):
15771578

15781579

15791580
# Internal type variable used for Type[].
1580-
CT = TypeVar('CT', covariant=True, bound=type)
1581+
CT_co = TypeVar('CT_co', covariant=True, bound=type)
15811582

15821583

15831584
# This is not a real generic class. Don't use outside annotations.
1584-
class Type(type, Generic[CT], extra=type):
1585+
class Type(type, Generic[CT_co], extra=type):
15851586
"""A special construct usable to annotate class objects.
15861587
15871588
For example, suppose we have the following classes::

0 commit comments

Comments
 (0)