Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Prev Previous commit
Next Next commit
Restructured test_typing a bit. ann_modules still imported
  • Loading branch information
ilevkivskyi committed Sep 9, 2016
commit b1d3d1e3f844286361c8c95718110a3bf6a9fcc9
111 changes: 62 additions & 49 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import re
import sys
from unittest import TestCase, main, skipUnless, SkipTest
if sys.version_info[:2] >= (3, 6):
from test import ann_module, ann_module2, ann_module3
from collections import ChainMap
from textwrap import dedent

from typing import Any
from typing import TypeVar, AnyStr
Expand Down Expand Up @@ -971,51 +967,6 @@ def add_right(self, node: 'Node[T]' = None):
right_hints = get_type_hints(t.add_right, globals(), locals())
self.assertEqual(right_hints['node'], Optional[Node[T]])

def test_get_type_hints(self):
gth = get_type_hints
if sys.version_info[:2] >= (3, 6):
self.assertEqual(gth(ann_module), {'x': int, 'y': str})
self.assertEqual(gth(ann_module.C, ann_module.__dict__),
ChainMap({'y': Optional[ann_module.C]}, {}))
self.assertEqual(gth(ann_module2), {})
self.assertEqual(gth(ann_module3), {})
self.assertEqual(repr(gth(ann_module.j_class)), 'ChainMap({}, {})')
self.assertEqual(gth(ann_module.M), ChainMap({'123': 123, 'o': type},
{}, {}))
self.assertEqual(gth(ann_module.D),
ChainMap({'j': str, 'k': str,
'y': Optional[ann_module.C]}, {}))
self.assertEqual(gth(ann_module.Y), ChainMap({'z': int}, {}))
self.assertEqual(gth(ann_module.h_class),
ChainMap({}, {'y': Optional[ann_module.C]}, {}))
self.assertEqual(gth(ann_module.S), ChainMap({'x': str, 'y': str},
{}))
self.assertEqual(gth(ann_module.foo), {'x': int})
self.assertEqual(gth(ann_module2.NTC.meth), {})

def testf(x, y): ...
testf.__annotations__['x'] = 'int'
self.assertEqual(gth(testf), {'x': int})

# interactions with ClassVar
stmt = dedent("""\
class B:
x: ClassVar[Optional['B']] = None
y: int
class C(B):
z: ClassVar['C'] = B()
class G(Generic[T]):
lst: ClassVar[List[T]] = []
""")
if sys.version_info[:2] >= (3, 6):
exec(stmt)
self.assertEqual(gth(B, locals()),
ChainMap({'y': int, 'x': ClassVar[Optional[B]]}, {}))
self.assertEqual(gth(C, locals()),
ChainMap({'z': ClassVar[C]},
{'y': int, 'x': ClassVar[Optional[B]]}, {}))
self.assertEqual(gth(G), ChainMap({'lst': ClassVar[List[T]]},{},{}))

def test_forwardref_instance_type_error(self):
fr = typing._ForwardRef('int')
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -1205,6 +1156,68 @@ def __anext__(self) -> T_a:
if PY35:
exec(PY35_TESTS)

PY36 = sys.version_info[:2] >= (3, 6)

PY36_TESTS = """
from test import ann_module, ann_module2, ann_module3
from collections import ChainMap

class B:
x: ClassVar[Optional['B']] = None
y: int
class C(B):
z: ClassVar['C'] = B()
class G(Generic[T]):
lst: ClassVar[List[T]] = []
"""

if PY36:
exec(PY36_TESTS)

gth = get_type_hints

class GetTypeHintTests(BaseTestCase):
@skipUnless(PY36, 'Python 3.6 required')
def test_get_type_hints_modules(self):
self.assertEqual(gth(ann_module), {'x': int, 'y': str})
self.assertEqual(gth(ann_module2), {})
self.assertEqual(gth(ann_module3), {})

@skipUnless(PY36, 'Python 3.6 required')
def test_get_type_hints_classes(self):
self.assertEqual(gth(ann_module.C, ann_module.__dict__),
ChainMap({'y': Optional[ann_module.C]}, {}))
self.assertEqual(repr(gth(ann_module.j_class)), 'ChainMap({}, {})')
self.assertEqual(gth(ann_module.M), ChainMap({'123': 123, 'o': type},
{}, {}))
self.assertEqual(gth(ann_module.D),
ChainMap({'j': str, 'k': str,
'y': Optional[ann_module.C]}, {}))
self.assertEqual(gth(ann_module.Y), ChainMap({'z': int}, {}))
self.assertEqual(gth(ann_module.h_class),
ChainMap({}, {'y': Optional[ann_module.C]}, {}))
self.assertEqual(gth(ann_module.S), ChainMap({'x': str, 'y': str},
{}))
self.assertEqual(gth(ann_module.foo), {'x': int})

@skipUnless(PY36, 'Python 3.6 required')
def test_respect_no_type_check(self):
self.assertEqual(gth(ann_module2.NTC.meth), {})

def test_previous_behavior(self):
def testf(x, y): ...
testf.__annotations__['x'] = 'int'
self.assertEqual(gth(testf), {'x': int})

@skipUnless(PY36, 'Python 3.6 required')
def test_get_type_hints_ClassVar(self):
self.assertEqual(gth(B, locals()),
ChainMap({'y': int, 'x': ClassVar[Optional[B]]}, {}))
self.assertEqual(gth(C, locals()),
ChainMap({'z': ClassVar[C]},
{'y': int, 'x': ClassVar[Optional[B]]}, {}))
self.assertEqual(gth(G), ChainMap({'lst': ClassVar[List[T]]},{},{}))


class CollectionsAbcTests(BaseTestCase):

Expand Down