From 0c0763ec5f4aae9f393b21608f220b0b065f7d21 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Tue, 27 Dec 2016 17:48:41 -0800 Subject: [PATCH 1/2] Allow isinstance checks of TypedDict from the typing module This fixes https://github.com/python/mypy/issues/2614 --- extensions/mypy_extensions.py | 2 +- mypy/test/testextensions.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/extensions/mypy_extensions.py b/extensions/mypy_extensions.py index 248da3de4aae..efa3c35e8e03 100644 --- a/extensions/mypy_extensions.py +++ b/extensions/mypy_extensions.py @@ -15,7 +15,7 @@ def _check_fails(cls, other): try: - if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']: + if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools', 'typing']: # Typed dicts are only for static structural subtyping. raise TypeError('TypedDict does not support instance and class checks') except (AttributeError, ValueError): diff --git a/mypy/test/testextensions.py b/mypy/test/testextensions.py index eca45d7e54dd..feb72820c604 100644 --- a/mypy/test/testextensions.py +++ b/mypy/test/testextensions.py @@ -114,6 +114,14 @@ def test_pickle(self): EmpDnew = pickle.loads(ZZ) self.assertEqual(EmpDnew({'name': 'jane', 'id': 37}), jane) + def test_optional(self): + EmpD = TypedDict('EmpD', name=str, id=int) + + def internal_function() -> typing.Optional[EmpD]: + return None + + internal_function() + if __name__ == '__main__': main() From aa4a0965102d4bd3e9331f49f60086f1671cfad6 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Wed, 28 Dec 2016 09:39:06 -0800 Subject: [PATCH 2/2] Add more test cases --- mypy/test/testextensions.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mypy/test/testextensions.py b/mypy/test/testextensions.py index feb72820c604..af3916f98e19 100644 --- a/mypy/test/testextensions.py +++ b/mypy/test/testextensions.py @@ -117,10 +117,8 @@ def test_pickle(self): def test_optional(self): EmpD = TypedDict('EmpD', name=str, id=int) - def internal_function() -> typing.Optional[EmpD]: - return None - - internal_function() + self.assertEqual(typing.Optional[EmpD], typing.Union[None, EmpD]) + self.assertNotEqual(typing.List[EmpD], typing.Tuple[EmpD]) if __name__ == '__main__':