8000 Add Type[C] "implementation" (#224) · python/typing@09f9cab · GitHub
[go: up one dir, main page]

Skip to content

Commit 09f9cab

Browse files
committed
Add Type[C] "implementation" (#224)
1 parent c532807 commit 09f9cab

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

python2/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):

python2/typing.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ def __subclasscheck__(self, cls):
454454

455455

456456
# Some unconstrained type variables. These are used by the container types.
457+
# (These are not for export.)
457458
T = TypeVar('T') # Any type.
458459
KT = TypeVar('KT') # Key type.
459460
VT = TypeVar('VT') # Value type.
@@ -463,6 +464,7 @@ def __subclasscheck__(self, cls):
463464
T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
464465

465466
# A useful type variable with constraints. This represents string types.
467+
# (This one *is* for export!)
466468
AnyStr = TypeVar('AnyStr', bytes, unicode)
467469

468470

@@ -1518,6 +1520,36 @@ def __new__(cls, *args, **kwds):
15181520
return super(Generator, cls).__new__(cls, *args, **kwds)
15191521

15201522

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

src/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( 7802 ):
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):

src/typing.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ def __subclasscheck__(self, cls):
447447

448448

449449
# Some unconstrained type variables. These are used by the container types.
450+
# (These are not for export.)
450451
T = TypeVar('T') # Any type.
451452
KT = TypeVar('KT') # Key type.
452453
VT = TypeVar('VT') # Value type.
@@ -456,6 +457,7 @@ def __subclasscheck__(self, cls):
456457
T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
457458

458459
# A useful type variable with constraints. This represents string types.
460+
# (This one *is* for export!)
459461
AnyStr = TypeVar('AnyStr', bytes, str)
460462

461463

@@ -1572,6 +1574,35 @@ def __new__(cls, *args, **kwds):
15721574
return super().__new__(cls, *args, **kwds)
15731575

15741576

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

0 commit comments

Comments
 (0)
0