-
Notifications
You must be signed in to change notification settings - Fork 263
get_type_hints(): find the right globalns for classes and modules #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,53 @@ | ||
"""Module for testing the behavior of generics across different modules.""" | ||
|
||
from typing import TypeVar, Generic | ||
import sys | ||
from textwrap import dedent | ||
from typing import TypeVar, Generic, Optional | ||
|
||
T = TypeVar('T') | ||
|
||
if sys.version_info[:2] >= (3, 6): | ||
exec(dedent(""" | ||
default_a: Optional['A'] = None | ||
default_b: Optional['B'] = None | ||
|
||
class A(Generic[T]): | ||
pass | ||
T = TypeVar('T') | ||
|
||
|
||
class B(Generic[T]): | ||
class A(Generic[T]): | ||
pass | ||
some_b: 'B' | ||
|
||
|
||
class B(Generic[T]): | ||
class A(Generic[T]): | ||
pass | ||
|
||
my_inner_a1: 'B.A' | ||
my_inner_a2: A | ||
my_outer_a: 'A' # unless somebody calls get_type_hints with localns=B.__dict__ | ||
""")) | ||
else: # This should stay in sync with the syntax above. | ||
__annotations__ = dict( | ||
default_a=Optional['A'], | ||
default_b=Optional['B'], | ||
) | ||
default_a = None | ||
default_b = None | ||
|
||
T = TypeVar('T') | ||
|
||
|
||
class A(Generic[T]): | ||
__annotations__ = dict( | ||
some_b='B' | ||
) | ||
|
||
|
||
class B(Generic[T]): | ||
class A(Generic[T]): | ||
pass | ||
|
||
__annotations__ = dict( | ||
my_inner_a1='B.A', | ||
my_inner_a2=A, | ||
my_outer_a='A' # unless somebody calls get_type_hints with localns=B.__dict__ | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import pickle | ||
import re | ||
import sys | ||
from unittest import TestCase, main, skipUnless, SkipTest | ||
from unittest import TestCase, main, skipUnless, SkipTest, expectedFailure | ||
from copy import copy, deepcopy | ||
|
||
from typing import Any, NoReturn | ||
|
@@ -30,6 +30,13 @@ | |
import collections as collections_abc # Fallback for PY3.2. | ||
|
||
|
||
try: | ||
import mod_generics_cache | ||
except ImportError: | ||
# try to use the builtin one, Python 3.5+ | ||
from test import mod_generics_cache | ||
|
||
|
||
class BaseTestCase(TestCase): | ||
|
||
def assertIsSubclass(self, cls, class_or_tuple, msg=None): | ||
|
@@ -836,10 +843,6 @@ def test_subscript_meta(self): | |
self.assertEqual(Callable[..., GenericMeta].__args__, (Ellipsis, GenericMeta)) | ||
|
||
def test_generic_hashes(self): | ||
try: | ||
from test import mod_generics_cache | ||
except ImportError: # for Python 3.4 and previous versions | ||
import mod_generics_cache | ||
class A(Generic[T]): | ||
... | ||
|
||
|
@@ -1619,6 +1622,10 @@ def __str__(self): | |
def __add__(self, other): | ||
return 0 | ||
|
||
class HasForeignBaseClass(mod_generics_cache.A): | ||
some_xrepr: 'XRepr' | ||
other_a: 'mod_generics_cache.A' | ||
|
||
async def g_with(am: AsyncContextManager[int]): | ||
x: int | ||
async with am as x: | ||
|
@@ -1658,9 +1665,19 @@ def test_get_type_hints_modules(self): | |
self.assertEqual(gth(ann_module2), {}) | ||
self.assertEqual(gth(ann_module3), {}) | ||
|
||
@skipUnless(PY36, 'Python 3.6 required') | ||
@expectedFailure | ||
def test_get_type_hints_modules_forwardref(self): | ||
# FIXME: This currently exposes a bug in typing. Cached forward references | ||
# don't account for the case where there are multiple types of the same | ||
# name coming from different modules in the same program. | ||
mgc_hints = {'default_a': Optional[mod_generics_cache.A], | ||
'default_b': Optional[mod_generics_cache.B]} | ||
self.assertEqual(gth(mod_generics_cache), mgc_hints) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will fix this bug in a separate PR when this one lands. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
OK, just don't forget about it (or open an issue if you want to do this later). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, I was looking at a different issue and noticed this test code behaving non-deterministically depending on when mod_generics_cache was loaded (specifically this test would expectedly fail when I just ran the test_typing module, but unexpectedly succeed when I ran ./python -m test.) Fixing the underlying type hint caching is beyond my scope as a new contributor, but it seems like something like this would make this test behave more reliably, if that seems reasonable to y'all: natgaertner/cpython@0cde103 |
||
|
||
@skipUnless(PY36, 'Python 3.6 required') | ||
def test_get_type_hints_classes(self): | ||
self.assertEqual(gth(ann_module.C, ann_module.__dict__), | ||
self.assertEqual(gth(ann_module.C), # gth will find the right globalns | ||
{'y': Optional[ann_module.C]}) | ||
self.assertIsInstance(gth(ann_module.j_class), dict) | ||
self.assertEqual(gth(ann_module.M), {'123': 123, 'o': type}) | ||
|
@@ -1671,8 +1688,15 @@ def test_get_type_hints_classes(self): | |
{'y': Optional[ann_module.C]}) | ||
self.assertEqual(gth(ann_module.S), {'x': str, 'y': str}) | ||
self.assertEqual(gth(ann_module.foo), {'x': int}) | ||
self.assertEqual(gth(NoneAndForward, globals()), | ||
self.assertEqual(gth(NoneAndForward), | ||
{'parent': NoneAndForward, 'meaning': type(None)}) | ||
self.assertEqual(gth(HasForeignBaseClass), | ||
{'some_xrepr': XRepr, 'other_a': mod_generics_cache.A, | ||
'some_b': mod_generics_cache.B}) | ||
self.assertEqual(gth(mod_generics_cache.B), | ||
{'my_inner_a1': mod_generics_cache.B.A, | ||
'my_inner_a2': mod_generics_cache.B.A, | ||
'my_outer_a': mod_generics_cache.A}) | ||
|
||
@skipUnless(PY36, 'Python 3.6 required') | ||
def test_respect_no_type_check(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only added a bunch of variable annotations here but since this wasn't compatible with < 3.5, I had to uglify this module. Now it's actually easier to see what's going on just by looking at the entire file with the "View" button.