8000 Revamped generic class behavior to conform to updated PEP 484 by gvanrossum · Pull Request #195 · python/typing · GitHub
[go: up one dir, main page]

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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Revamp of generic classes. Dict[str, Tuple[S, T]] is now valid.
Still need to backport to python2.
  • Loading branch information
Guido van Rossum committed Mar 24, 2016
commit abefbe4a72654e7e385944848b0158969ee33d2e
78 changes: 61 additions & 17 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def get(self, key: XK, default: XV = None) -> XV:
...


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

def __init__(self):
self.store = {}
Expand Down Expand Up @@ -540,6 +540,7 @@ def test_supports_abs(self):
assert not issubclass(str, typing.SupportsAbs)

def test_supports_round(self):
issubclass(float, typing.SupportsRound)
assert issubclass(float, typing.SupportsRound)
assert issubclass(int, typing.SupportsRound)
assert not issubclass(str, typing.SupportsRound)
Expand All @@ -557,13 +558,16 @@ class GenericTests(TestCase):

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

def test_init(self):
T = TypeVar('T')
Expand All @@ -575,9 +579,32 @@ 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 @@ -632,20 +659,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 @@ -673,15 +700,14 @@ 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):

class G(Generic):
pass
G = Generic

class Visitor(G[T]):

Expand Down Expand Up @@ -730,6 +756,24 @@ def foo(x: T):

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 @@ -1286,15 +1330,15 @@ def stuff(a: TextIO) -> str:
return a.readline()

a = stuff.__annotations__['a']
assert a.__parameters__ == (str,)
assert a.__parameters__ == ()

def test_binaryio(self):

def stuff(a: BinaryIO) -> bytes:
return a.readline()

a = stuff.__annotations__['a']
assert a.__parameters__ == (bytes,)
assert a.__parameters__ == ()

def test_io_submodule(self):
from typing.io import IO, TextIO, BinaryIO, __all__, __name__
Expand Down
Loading
0