8000 Various updates to typing.py by gvanrossum · Pull Request #2382 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Various updates to typing.py #2382

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

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 144 additions & 21 deletions lib-typing/2.7/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import sys
from unittest import TestCase, main, SkipTest
from copy import copy, deepcopy

from typing import Any
from typing import TypeVar, AnyStr
Expand Down Expand Up @@ -141,8 +142,9 @@ def test_union_unique(self):
self.assertEqual(Union[X, X], X)
self.assertNotEqual(Union[X, int], Union[X])
self.assertNotEqual(Union[X, int], Union[int])
self.assertEqual(Union[X, int].__union_params__, (X, int))
self.assertEqual(Union[X, int].__union_set_params__, {X, int})
self.assertEqual(Union[X, int].__args__, (X, int))
self.assertEqual(Union[X, int].__parameters__, (X,))
self.assertIs(Union[X, int].__origin__, Union)

def test_union_constrained(self):
A = TypeVar('A', str, bytes)
Expand Down Expand Up @@ -308,8 +310,6 @@ def Elem(*args):
class TupleTests(BaseTestCase):

def test_basics(self):
with self.assertRaises(TypeError):
issubclass(Tuple[int, str], Tuple)
with self.assertRaises(TypeError):
issubclass(Tuple, Tuple[int, str])
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -364,22 +364,6 @@ def test_eq_hash(self):
self.assertNotEqual(Callable[[int], int], Callable[[], int])
self.assertNotEqual(Callable[[int], int], Callable)

def test_cannot_subclass(self):
with self.assertRaises(TypeError):

class C(Callable):
pass

with self.assertRaises(TypeError):

class C(type(Callable)):
pass

with self.assertRaises(TypeError):

class C(Callable[[int], int]):
pass

def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
Callable()
Expand Down Expand Up @@ -506,6 +490,9 @@ def test_basics(self):
Y[unicode, unicode]

def test_generic_errors(self):
T = TypeVar('T')
with self.assertRaises(TypeError):
Generic[T]()
with self.assertRaises(TypeError):
isinstance([], List[int])
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -683,6 +670,124 @@ class D(C, List[T][U][V]): pass
self.assertEqual(C.__orig_bases__, (List[T][U][V],))
self.assertEqual(D.__orig_bases__, (C, List[T][U][V]))

def test_extended_generic_rules_eq(self):
T = TypeVar('T')
U = TypeVar('U')
self.assertEqual(Tuple[T, T][int], Tuple[int, int])
self.assertEqual(typing.Iterable[Tuple[T, T]][T], typing.Iterable[Tuple[T, T]])
with self.assertRaises(TypeError):
Tuple[T, int][()]
with self.assertRaises(TypeError):
Tuple[T, U][T, ...]

self.assertEqual(Union[T, int][int], int)
self.assertEqual(Union[T, U][int, Union[int, str]], Union[int, str])
class Base(object): pass
class Derived(Base): pass
self.assertEqual(Union[T, Base][Derived], Base)
with self.assertRaises(TypeError):
Union[T, int][1]

self.assertEqual(Callable[[T], T][KT], Callable[[KT], KT])
self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]])
with self.assertRaises(TypeError):
Callable[[T], U][..., int]
with self.assertRaises(TypeError):
Callable[[T], U][[], int]

def test_extended_generic_rules_repr(self):
T = TypeVar('T')
self.assertEqual(repr(Union[Tuple, Callable]).replace('typing.', ''),
'Union[Tuple, Callable]')
self.assertEqual(repr(Union[Tuple, Tuple[int]]).replace('typing.', ''),
'Tuple')
self.assertEqual(repr(Callable[..., Optional[T]][int]).replace('typing.', ''),
'Callable[..., Union[int, NoneType]]')
self.assertEqual(repr(Callable[[], List[T]][int]).replace('typing.', ''),
'Callable[[], List[int]]')

def test_generic_forvard_ref(self):
LLT = List[List['T']]
T = TypeVar('T')
self.assertEqual(typing._eval_type(LLT, globals(), locals()), List[List[T]])
TTE = Tuple[T, ...]
self.assertIs(typing._eval_type(TTE, globals(), locals()), Tuple[T, ...])

def test_extended_generic_rules_subclassing(self):
class T1(Tuple[T, KT]): pass
class T2(Tuple[T, ...]): pass
class C1(Callable[[T], T]): pass
class C2(Callable[..., int]):
def __call__(self):
return None

self.assertEqual(T1.__parameters__, (T, KT))
self.assertEqual(T1[int, str].__args__, (int, str))
self.assertEqual(T1[int, T].__origin__, T1)

self.assertEqual(T2.__parameters__, (T,))
with self.assertRaises(TypeError):
T1[int]
with self.assertRaises(TypeError):
T2[int, str]

self.assertEqual(repr(C1[int]).split('.')[-1], 'C1[int]')
self.assertEqual(C2.__parameters__, ())
self.assertIsInstance(C2(), collections_abc.Callable)
self.assertIsSubclass(C2, collections_abc.Callable)
self.assertIsSubclass(C1, collections_abc.Callable)
self.assertIsInstance(T1(), tuple)
self.assertIsSubclass(T2, tuple)
self.assertIsSubclass(Tuple[int, ...], typing.Sequence)
self.assertIsSubclass(Tuple[int, ...], typing.Iterable)

def test_fail_with_bare_union(self):
with self.assertRaises(TypeError):
List[Union]
with self.assertRaises(TypeError):
Tuple[Optional]
with self.assertRaises(TypeError):
ClassVar[ClassVar]
with self.assertRaises(TypeError):
List[ClassVar[int]]

def test_fail_with_bare_generic(self):
T = TypeVar('T')
with self.assertRaises(TypeError):
List[Generic]
with self.assertRaises(TypeError):
Tuple[Generic[T]]
with self.assertRaises(TypeError):
List[typing._Protocol]

def test_type_erasure_special(self):
T = TypeVar('T')
class MyTup(Tuple[T, T]): pass
self.assertIs(MyTup[int]().__class__, MyTup)
self.assertIs(MyTup[int]().__orig_class__, MyTup[int])
class MyCall(Callable[..., T]):
def __call__(self): return None
self.assertIs(MyCall[T]().__class__, MyCall)
self.assertIs(MyCall[T]().__orig_class__, MyCall[T])
class MyDict(typing.Dict[T, T]): pass
self.assertIs(MyDict[int]().__class__, MyDict)
self.assertIs(MyDict[int]().__orig_class__, MyDict[int])
class MyDef(typing.DefaultDict[str, T]): pass
self.assertIs(MyDef[int]().__class__, MyDef)
self.assertIs(MyDef[int]().__orig_class__, MyDef[int])

def test_all_repr_eq_any(self):
objs = (getattr(typing, el) for el in typing.__all__)
for obj in objs:
self.assertNotEqual(repr(obj), '')
self.assertEqual(obj, obj)
if getattr(obj, '__parameters__', None) and len(obj.__parameters__) == 1:
self.assertEqual(obj[Any].__args__, (Any,))
if isinstance(obj, type):
for base in obj.__mro__:
self.assertNotEqual(repr(base), '')
self.assertEqual(base, base)

def test_pickle(self):
global C # pickle wants to reference the class by name
T = TypeVar('T')
Expand All @@ -702,6 +807,24 @@ class C(B[int]):
self.assertEqual(x.foo, 42)
self.assertEqual(x.bar, 'abc')
self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
simples = [Any, Union, Tuple, Callable, ClassVar, List, typing.Iterable]
for s in simples:
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
z = pickle.dumps(s, proto)
x = pickle.loads(z)
self.assertEqual(s, x)

def test_copy_and_deepcopy(self):
T = TypeVar('T')
class Node(Generic[T]): pass
things = [Any, Union[T, int], Tuple[T, int], Callable[..., T], Callable[[int], int],
Tuple[Any, Any], Node[T], Node[int], Node[Any], typing.Iterable[T],
typing.Iterable[Any], typing.Iterable[int], typing.Dict[int, str],
typing.Dict[T, Any], ClassVar[int], ClassVar[List[T]], Tuple['T', 'T'],
Union['T', int], List['T'], typing.Mapping['T', int]]
for t in things:
self.assertEqual(t, deepcopy(t))
self.assertEqual(t, copy(t))

def test_errors(self):
with self.assertRaises(TypeError):
Expand All @@ -724,7 +847,7 @@ class C(Generic[T]):
X = C[int]
self.assertEqual(X.__module__, __name__)
if not PY32:
self.assertEqual(X.__qualname__, 'C')
self.assertTrue(X.__qualname__.endswith('.<locals>.C'))
self.assertEqual(repr(X).split('.')[-1], 'C[int]')

class Y(C[int]):
Expand Down
Loading
0