8000 Type should not inherit from type. (#267) · python/typing@a1952c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit a1952c4

Browse files
authored
Type should not inherit from type. (#267)
Fixes #266.
1 parent 4a6885a commit a1952c4

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

python2/test_typing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,19 @@ def new_user(user_class):
11591159

11601160
joe = new_user(BasicUser)
11611161

1162+
def test_type_optional(self):
1163+
A = Optional[Type[BaseException]]
1164+
1165+
def foo(a):
1166+
# type: (A) -> Optional[BaseException]
1167+
if a is None:
1168+
return None
1169+
else:
1170+
return a()
1171+
1172+
assert isinstance(foo(KeyboardInterrupt), KeyboardInterrupt)
1173+
assert foo(None) is None
1174+
11621175

11631176
class NewTypeTests(BaseTestCase):
11641177

python2/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ def __new__(cls, *args, **kwds):
15291529

15301530

15311531
# This is not a real generic class. Don't use outside annotations.
1532-
class Type(type, Generic[CT_co]):
1532+
class Type(Generic[CT_co]):
15331533
"""A special construct usable to annotate class objects.
15341534
15351535
For example, suppose we have the following classes::

src/test_typing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,18 @@ def new_user(user_class: Type[U]) -> U:
14261426

14271427
joe = new_user(BasicUser)
14281428

1429+
def test_type_optional(self):
1430+
A = Optional[Type[BaseException]]
1431+
1432+
def foo(a: A) -> Optional[BaseException]:
1433+
if a is None:
1434+
return None
1435+
else:
1436+
return a()
1437+
1438+
assert isinstance(foo(KeyboardInterrupt), KeyboardInterrupt)
1439+
assert foo(None) is None
1440+
14291441

14301442
class NewTypeTests(BaseTestCase):
14311443

src/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ def __new__(cls, *args, **kwds):
16051605

16061606

16071607
# This is not a real generic class. Don't use outside annotations.
1608-
class Type(type, Generic[CT_co], extra=type):
1608+
class Type(Generic[CT_co], extra=type):
16091609
"""A special construct usable to annotate class objects.
16101610
16111611
For example, suppose we have the following classes::

0 commit comments

Comments
 (0)
0