[3.6] bpo-28556: typing.get_type_hints: better globalns for classes a… · python/cpython@1658ec0 (original) (raw)

`@@ -3,7 +3,7 @@

`

3

3

`import pickle

`

4

4

`import re

`

5

5

`import sys

`

6

``

`-

from unittest import TestCase, main, skipUnless, SkipTest

`

``

6

`+

from unittest import TestCase, main, skipUnless, SkipTest, expectedFailure

`

7

7

`from copy import copy, deepcopy

`

8

8

``

9

9

`from typing import Any, NoReturn

`

`@@ -30,6 +30,13 @@

`

30

30

`import collections as collections_abc # Fallback for PY3.2.

`

31

31

``

32

32

``

``

33

`+

try:

`

``

34

`+

import mod_generics_cache

`

``

35

`+

except ImportError:

`

``

36

`+

try to use the builtin one, Python 3.5+

`

``

37

`+

from test import mod_generics_cache

`

``

38

+

``

39

+

33

40

`class BaseTestCase(TestCase):

`

34

41

``

35

42

`def assertIsSubclass(self, cls, class_or_tuple, msg=None):

`

`@@ -836,10 +843,6 @@ def test_subscript_meta(self):

`

836

843

`self.assertEqual(Callable[..., GenericMeta].args, (Ellipsis, GenericMeta))

`

837

844

``

838

845

`def test_generic_hashes(self):

`

839

``

`-

try:

`

840

``

`-

from test import mod_generics_cache

`

841

``

`-

except ImportError: # for Python 3.4 and previous versions

`

842

``

`-

import mod_generics_cache

`

843

846

`class A(Generic[T]):

`

844

847

` ...

`

845

848

``

`@@ -1619,6 +1622,10 @@ def str(self):

`

1619

1622

` def add(self, other):

`

1620

1623

` return 0

`

1621

1624

``

``

1625

`+

class HasForeignBaseClass(mod_generics_cache.A):

`

``

1626

`+

some_xrepr: 'XRepr'

`

``

1627

`+

other_a: 'mod_generics_cache.A'

`

``

1628

+

1622

1629

`async def g_with(am: AsyncContextManager[int]):

`

1623

1630

` x: int

`

1624

1631

` async with am as x:

`

`@@ -1658,9 +1665,19 @@ def test_get_type_hints_modules(self):

`

1658

1665

`self.assertEqual(gth(ann_module2), {})

`

1659

1666

`self.assertEqual(gth(ann_module3), {})

`

1660

1667

``

``

1668

`+

@skipUnless(PY36, 'Python 3.6 required')

`

``

1669

`+

@expectedFailure

`

``

1670

`+

def test_get_type_hints_modules_forwardref(self):

`

``

1671

`+

FIXME: This currently exposes a bug in typing. Cached forward references

`

``

1672

`+

don't account for the case where there are multiple types of the same

`

``

1673

`+

name coming from different modules in the same program.

`

``

1674

`+

mgc_hints = {'default_a': Optional[mod_generics_cache.A],

`

``

1675

`+

'default_b': Optional[mod_generics_cache.B]}

`

``

1676

`+

self.assertEqual(gth(mod_generics_cache), mgc_hints)

`

``

1677

+

1661

1678

`@skipUnless(PY36, 'Python 3.6 required')

`

1662

1679

`def test_get_type_hints_classes(self):

`

1663

``

`-

self.assertEqual(gth(ann_module.C, ann_module.dict),

`

``

1680

`+

self.assertEqual(gth(ann_module.C), # gth will find the right globalns

`

1664

1681

` {'y': Optional[ann_module.C]})

`

1665

1682

`self.assertIsInstance(gth(ann_module.j_class), dict)

`

1666

1683

`self.assertEqual(gth(ann_module.M), {'123': 123, 'o': type})

`

`@@ -1671,8 +1688,15 @@ def test_get_type_hints_classes(self):

`

1671

1688

` {'y': Optional[ann_module.C]})

`

1672

1689

`self.assertEqual(gth(ann_module.S), {'x': str, 'y': str})

`

1673

1690

`self.assertEqual(gth(ann_module.foo), {'x': int})

`

1674

``

`-

self.assertEqual(gth(NoneAndForward, globals()),

`

``

1691

`+

self.assertEqual(gth(NoneAndForward),

`

1675

1692

` {'parent': NoneAndForward, 'meaning': type(None)})

`

``

1693

`+

self.assertEqual(gth(HasForeignBaseClass),

`

``

1694

`+

{'some_xrepr': XRepr, 'other_a': mod_generics_cache.A,

`

``

1695

`+

'some_b': mod_generics_cache.B})

`

``

1696

`+

self.assertEqual(gth(mod_generics_cache.B),

`

``

1697

`+

{'my_inner_a1': mod_generics_cache.B.A,

`

``

1698

`+

'my_inner_a2': mod_generics_cache.B.A,

`

``

1699

`+

'my_outer_a': mod_generics_cache.A})

`

1676

1700

``

1677

1701

`@skipUnless(PY36, 'Python 3.6 required')

`

1678

1702

`def test_respect_no_type_check(self):

`