From a1952c4a0a21b14887bd536fc494d15516667b37 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 24 Aug 2016 13:33:22 -0700 Subject: [PATCH] Type should not inherit from type. (#267) Fixes #266. --- python2/test_typing.py | 13 +++++++++++++ python2/typing.py | 2 +- src/test_typing.py | 12 ++++++++++++ src/typing.py | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/python2/test_typing.py b/python2/test_typing.py index 6cff13c3..d72a5896 100644 --- a/python2/test_typing.py +++ b/python2/test_typing.py @@ -1159,6 +1159,19 @@ def new_user(user_class): joe = new_user(BasicUser) + def test_type_optional(self): + A = Optional[Type[BaseException]] + + def foo(a): + # type: (A) -> Optional[BaseException] + if a is None: + return None + else: + return a() + + assert isinstance(foo(KeyboardInterrupt), KeyboardInterrupt) + assert foo(None) is None + class NewTypeTests(BaseTestCase): diff --git a/python2/typing.py b/python2/typing.py index 138521bd..03632eaf 100644 --- a/python2/typing.py +++ b/python2/typing.py @@ -1529,7 +1529,7 @@ def __new__(cls, *args, **kwds): # This is not a real generic class. Don't use outside annotations. -class Type(type, Generic[CT_co]): +class Type(Generic[CT_co]): """A special construct usable to annotate class objects. For example, suppose we have the following classes:: diff --git a/src/test_typing.py b/src/test_typing.py index 72afe670..1f5e72fe 100644 --- a/src/test_typing.py +++ b/src/test_typing.py @@ -1426,6 +1426,18 @@ def new_user(user_class: Type[U]) -> U: joe = new_user(BasicUser) + def test_type_optional(self): + A = Optional[Type[BaseException]] + + def foo(a: A) -> Optional[BaseException]: + if a is None: + return None + else: + return a() + + assert isinstance(foo(KeyboardInterrupt), KeyboardInterrupt) + assert foo(None) is None + class NewTypeTests(BaseTestCase): diff --git a/src/typing.py b/src/typing.py index 5573a1fb..b628ba3d 100644 --- a/src/typing.py +++ b/src/typing.py @@ -1605,7 +1605,7 @@ def __new__(cls, *args, **kwds): # This is not a real generic class. Don't use outside annotations. -class Type(type, Generic[CT_co], extra=type): +class Type(Generic[CT_co], extra=type): """A special construct usable to annotate class objects. For example, suppose we have the following classes::