Skip to content

Commit f8fc94f

Browse files
committed
Added Type[C] implementation to typing.py.
1 parent e17e374 commit f8fc94f

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Lib/test/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.py

Lines changed: 33 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,35 @@ 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+
class Type(type, Generic[CT], extra=type):
1583+
"""A generic type usable to annotate class objects.
1584+
1585+
For example, suppose we have the following classes::
1586+
1587+
class User: ... # Abstract base for User classes
1588+
class BasicUser(User): ...
1589+
class ProUser(User): ...
1590+
class TeamUser(User): ...
1591+
1592+
And a function that takes a class argument that's a subclass of
1593+
User and returns an instance of the corresponding class::
1594+
1595+
U = TypeVar('U', bound=User)
1596+
def new_user(user_class: Type[U]) -> U:
1597+
user = user_class()
1598+
# (Here we could write the user object to a database)
1599+
return user
1600+
1601+
joe = new_user(BasicUser)
1602+
1603+
At this point the type checker knows that joe has type BasicUser.
1604+
"""
1605+
1606+
15751607
def NamedTuple(typename, fields):
15761608
"""Typed version of namedtuple.
15771609

0 commit comments

Comments
 (0)