8000 Update our copies of typing.py to the latest from the python/typing r… · python/mypy@cd2f20c · GitHub
[go: up one dir, main page]

Skip to content

Commit cd2f20c

Browse files
author
Guido van Rossum
committed
Update our copies of typing.py to the latest from the python/typing repo.
1 parent b9dcb7c commit cd2f20c

File tree

4 files changed

+128
-2
lines changed

4 files changed

+128
-2
lines changed

lib-typing/2.7/test_typing.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import Callable
1515
from typing import Generic
1616
from typing import cast
17+
from typing import Type
1718
from typing import NamedTuple
1819
from typing import IO, TextIO, BinaryIO
1920
from typing import Pattern, Match
@@ -1110,6 +1111,36 @@ def __len__(self):
11101111
self.assertIsSubclass(MMC, typing.Mapping)
11111112

11121113

1114+
class TypeTests(BaseTestCase):
1115+
1116+
def test_type_basic(self):
1117+
1118+
class User(object): pass
1119+
class BasicUser(User): pass
1120+
class ProUser(User): pass
1121+
1122+
def new_user(user_class):
1123+
# type: (Type[User]) -> User
1124+
return user_class()
1125+
1126+
joe = new_user(BasicUser)
1127+
1128+
def test_type_typevar(self):
1129+
1130+
class User(object): pass
1131+
class BasicUser(User): pass
1132+
class ProUser(User): pass
1133+
1134+
global U
1135+
U = TypeVar('U', bound=User)
1136+
1137+
def new_user(user_class):
1138+
# type: (Type[U]) -> U
1139+
return user_class()
1140+
1141+
joe = new_user(BasicUser)
1142+
1143+
11131144
class NamedTupleTests(BaseTestCase):
11141145

11151146
def test_basics(self):

lib-typing/2.7/typing.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
'Callable',
2121
'Generic',
2222
'Optional',
23+
'Tuple',
24+
'Type',
2325
'TypeVar',
2426
'Union',
25-
'Tuple',
2627

2728
# ABCs (from collections.abc).
2829
'AbstractSet', # collections.abc.Set.
@@ -454,6 +455,7 @@ def __subclasscheck__(self, cls):
454455

455456

456457
# Some unconstrained type variables. These are used by the container types.
458+
# (These are not for export.)
457459
T = TypeVar('T') # Any type.
458460
KT = TypeVar('KT') # Key type.
459461
VT = TypeVar('VT') # Value type.
@@ -463,6 +465,7 @@ def __subclasscheck__(self, cls):
463465
T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
464466

465467
# A useful type variable with constraints. This represents string types.
468+
# (This one *is* for export!)
466469
AnyStr = TypeVar('AnyStr', bytes, unicode)
467470

468471

@@ -1518,6 +1521,37 @@ def __new__(cls, *args, **kwds):
15181521
return super(Generator, cls).__new__(cls, *args, **kwds)
15191522

15201523

1524+
# Internal type variable used for Type[].
1525+
CT = TypeVar('CT', covariant=True, bound=type)
1526+
1527+
1528+
# This is not a real generic class. Don't u 628C se outside annotations.
1529+
class Type(type, Generic[CT]):
1530+
"""A special construct usable to annotate class objects.
1531+
1532+
For example, suppose we have the following classes::
1533+
1534+
class User: ... # Abstract base for User classes
1535+
class BasicUser(User): ...
1536+
class ProUser(User): ...
1537+
class TeamUser(User): ...
1538+
1539+
And a function that takes a class argument that's a subclass of
1540+
User and returns an instance of the corresponding class::
1541+
1542+
U = TypeVar('U', bound=User)
1543+
def new_user(user_class: Type[U]) -> U:
1544+
user = user_class()
1545+
# (Here we could write the user object to a database)
1546+
return user
1547+
1548+
joe = new_user(BasicUser)
1549+
1550+
At this point the type checker knows that joe has type BasicUser.
1551+
"""
1552+
__extra__ = type
1553+
1554+
15211555
def NamedTuple(typename, fields):
15221556
"""Typed version of namedtuple.
15231557

lib-typing/3.2/test_typing.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import cast
1616
from typing import get_type_hints
1717
from typing import no_type_check, no_type_check_decorator
18+
from typing import Type
1819
from typing import NamedTuple
1920
from typing import IO, TextIO, BinaryIO
2021
from typing import Pattern, Match
@@ -1373,6 +1374,33 @@ def manager():
13731374
self.assertNotIsInstance(42, typing.ContextManager)
13741375

13751376

1377+
class TypeTests(BaseTestCase):
1378+
1379+
def test_type_basic(self):
1380+
1381+
class User: pass
1382+
class BasicUser(User): pass
1383+
class ProUser(User): pass
1384+
1385+
def new_user(user_class: Type[User]) -> User:
1386+
return user_class()
1387+
1388+
joe = new_user(BasicUser)
1389+
1390+
def test_type_typevar(self):
1391+
1392+
class User: pass
1393+
class BasicUser(User): pass
1394+
class ProUser(User): pass
1395+
1396+
U = TypeVar('U', bound=User)
1397+
1398+
def new_user(user_class: Type[U]) -> U:
1399+
return user_class()
1400+
1401+
joe = new_user(BasicUser)
1402+
1403+
13761404
class NamedTupleTests(BaseTestCase):
13771405

13781406
def test_basics(self):

lib-typing/3.2/typing.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
'Callable',
2020
'Generic',
2121
'Optional',
22+
'Tuple',
23+
'Type',
2224
'TypeVar',
2325
'Union',
24-
'Tuple',
2526

2627
# ABCs (from collections.abc).
2728
'AbstractSet', # collections.abc.Set.
@@ -447,6 +448,7 @@ def __subclasscheck__(self, cls):
447448

448449

449450
# Some unconstrained type variables. These are used by the container types.
451+
# (These are not for export.)
450452
T = TypeVar('T') # Any type.
451453
KT = TypeVar('KT') # Key type.
452454
VT = TypeVar('VT') # Value type.
@@ -456,6 +458,7 @@ def __subclasscheck__(self, cls):
456458
T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
457459

458460
# A useful type variable with constraints. This represents string types.
461+
# (This one *is* for export!)
459462
AnyStr = TypeVar('AnyStr', bytes, str)
460463

461464

@@ -1572,6 +1575,36 @@ def __new__(cls, *args, **kwds):
15721575
return super().__new__(cls, *args, **kwds)
15731576

15741577

1578+
# Internal type variable used for Type[].
1579+
CT = TypeVar('CT', covariant=True, bound=type)
1580+
1581+
1582+
# This is not a real generic class. Don't use outside annotations.
1583+
class Type(type, Generic[CT], extra=type):
1584+
"""A special construct usable to annotate class objects.
1585+
1586+
For example, suppose we have the following classes::
1587+
1588+
class User: ... # Abstract base for User classes
1589+
class BasicUser(User): ...
1590+
class ProUser(User): ...
1591+
class TeamUser(User): ...
1592+
1593+
And a function that takes a class argument that's a subclass of
1594+
User and returns an instance of the corresponding class::
1595+
1596+
U = TypeVar('U', bound=User)
1597+
def new_user(user_class: Type[U]) -> U:
1598+
user = user_class()
1599+
# (Here we could write the user object to a database)
1600+
return user
1601+
1602+
joe = new_user(BasicUser)
1603+
1604+
At this point the type checker knows that joe has type BasicUser.
1605+
"""
1606+
1607+
15751608
def NamedTuple(typename, fields):
15761609
"""Typed version of namedtuple.
15771610

0 commit comments

Comments
 (0)
0