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

Skip to content

Revamped generic class behavior to conform to updated PEP 484 #195

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 9 commits into from
Apr 4, 2016
114 changes: 100 additions & 14 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_basic_constrained(self):
def test_constrained_error(self):
with self.assertRaises(TypeError):
X = TypeVar('X', int)
X

def test_union_unique(self):
X = TypeVar('X')
Expand Down Expand Up @@ -316,6 +317,7 @@ def test_union_instance_type_error(self):
def test_union_str_pattern(self):
# Shouldn't crash; see http://bugs.python.org/issue25390
A = Union[str, Pattern]
A


class TypeVarUnionTests(TestCase):
Expand Down Expand Up @@ -478,7 +480,7 @@ def get(self, key, default=None):
pass


class MySimpleMapping(SimpleMapping):
class MySimpleMapping(SimpleMapping[XK, XV]):

def __init__(self):
self.store = {}
Expand Down Expand Up @@ -533,14 +535,17 @@ def test_protocol_instance_type_error(self):
class GenericTests(TestCase):

def test_basics(self):
X = SimpleMapping[unicode, Any]
Y = SimpleMapping[XK, unicode]
X[unicode, unicode]
Y[unicode, unicode]
X = SimpleMapping[str, Any]
assert X.__parameters__ == ()
with self.assertRaises(TypeError):
X[unicode]
with self.assertRaises(TypeError):
X[int, unicode]
X[unicode, unicode]
Y = SimpleMapping[XK, unicode]
assert Y.__parameters__ == (XK,)
Y[unicode]
with self.assertRaises(TypeError):
Y[unicode, bytes]
Y[unicode, unicode]

def test_init(self):
T = TypeVar('T')
Expand All @@ -552,9 +557,33 @@ def test_init(self):

def test_repr(self):
self.assertEqual(repr(SimpleMapping),
__name__ + '.' + 'SimpleMapping[~XK, ~XV]')
__name__ + '.' + 'SimpleMapping<~XK, ~XV>')
self.assertEqual(repr(MySimpleMapping),
__name__ + '.' + 'MySimpleMapping[~XK, ~XV]')
__name__ + '.' + 'MySimpleMapping<~XK, ~XV>')

def test_chain_repr(self):
T = TypeVar('T')
S = TypeVar('S')

class C(Generic[T]):
pass

X = C[Tuple[S, T]]
assert X == C[Tuple[S, T]]
assert X != C[Tuple[T, S]]

Y = X[T, int]
assert Y == X[T, int]
assert Y != X[S, int]
assert Y != X[T, str]

Z = Y[str]
assert Z == Y[str]
assert Z != Y[int]
assert Z != Y[T]

assert str(Z).endswith(
'.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]')

def test_dict(self):
T = TypeVar('T')
Expand Down Expand Up @@ -609,20 +638,20 @@ class C(Generic[T]):
assert C.__module__ == __name__
if not PY32:
assert C.__qualname__ == 'GenericTests.test_repr_2.<locals>.C'
assert repr(C).split('.')[-1] == 'C[~T]'
assert repr(C).split('.')[-1] == 'C<~T>'
X = C[int]
assert X.__module__ == __name__
if not PY32:
assert X.__qualname__ == 'C'
assert repr(X).split('.')[-1] == 'C[int]'
assert repr(X).split('.')[-1] == 'C<~T>[int]'

class Y(C[int]):
pass

assert Y.__module__ == __name__
if not PY32:
assert Y.__qualname__ == 'GenericTests.test_repr_2.<locals>.Y'
assert repr(Y).split('.')[-1] == 'Y[int]'
assert repr(Y).split('.')[-1] == 'Y'

def test_eq_1(self):
assert Generic == Generic
Expand Down Expand Up @@ -650,10 +679,40 @@ class A(Generic[T, VT]):
class B(Generic[KT, T]):
pass

class C(A, Generic[KT, VT], B):
class C(A[T, VT], Generic[VT, T, KT], B[KT, T]):
pass

assert C.__parameters__ == (T, VT, KT)
assert C.__parameters__ == (VT, T, KT)

def test_nested(self):

G = Generic

class Visitor(G[T]):

a = None

def set(self, a):
self.a = a

def get(self):
return self.a

def visit(self):
return self.a

V = Visitor[typing.List[int]]

class IntListVisitor(V):

def append(self, x):
self.a.append(x)

a = IntListVisitor()
a.set([])
a.append(1)
a.append(42)
assert a.get() == [1, 42]

def test_type_erasure(self):
T = TypeVar('T')
Expand All @@ -679,6 +738,24 @@ def foo(x):

foo(42)

def test_implicit_any(self):
T = TypeVar('T')

class C(Generic[T]):
pass

class D(C):
pass

assert D.__parameters__ == ()

with self.assertRaises(Exception):
D[int]
with self.assertRaises(Exception):
D[Any]
with self.assertRaises(Exception):
D[T]


class VarianceTests(TestCase):

Expand Down Expand Up @@ -988,6 +1065,15 @@ def test_basics(self):
assert Emp._fields == ('name', 'id')
assert Emp._field_types == dict(name=str, id=int)

def test_pickle(self):
global Emp # pickle wants to reference the class by name
Emp = NamedTuple('Emp', [('name', str), ('id', int)])
jane = Emp('jane', 37)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
z = pickle.dumps(jane, proto)
jane2 = pickle.loads(z)
self.assertEqual(jane2, jane)


class IOTests(TestCase):

Expand Down
Loading