8000 Alternative (simpler) repr for generics by ilevkivskyi · Pull Request #296 · python/typing · GitHub
[go: up one dir, main page]

Skip to content

Alternative (simpler) repr for generics #296

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 8 commits into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Small fix and tests
  • Loading branch information
ilevkivskyi committed Oct 10, 2016
commit b42f304d203a0abcf12fa0f6ac8d343ff9a48340
31 changes: 26 additions & 5 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,9 @@ 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')
Expand All @@ -547,7 +547,28 @@ class C(Generic[T]):
self.assertNotEqual(Z, Y[T])

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

def test_new_repr(self):
T = TypeVar('T')
U = TypeVar('U', covariant=True)
S = TypeVar('S')

self.assertEqual(repr(List), 'typing.List[~T]')
self.assertEqual(repr(List[T]), 'typing.List[~T]')
self.assertEqual(repr(List[U]), 'typing.List[+U]')
self.assertEqual(repr(List[S][T][int]), 'typing.List[int]')
self.assertEqual(repr(List[int]), 'typing.List[int]')

def test_new_repr_complex(self):
T = TypeVar('T')
TS = TypeVar('TS')

self.assertEqual(repr(typing.Mapping[T, TS][TS, T]), 'typing.Mapping[~TS, ~T]')
self.assertEqual(repr(List[Tuple[T, TS]][int, T]),
'typing.List[typing.Tuple[int, ~T]]')
self.assertEqual(repr(List[Tuple[T, T]][List[int]]),
'typing.List[typing.Tuple[typing.List[int], typing.List[int]]]')

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

class Y(C[int]):
pass
Expand Down
6 changes: 4 additions & 2 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,8 +1119,10 @@ def __repr__(self):

def _argrepr(self):
if self.__origin__ is None:
return '[%s]' % (
', '.join(_type_repr(p) for p in self.__parameters__))
if self.__parameters__:
return '[%s]' % (
', '.join(_type_repr(p) for p in self.__parameters__))
return ''
r = self.__origin__._argrepr()
for i, arg in enumerate(self.__args__):
r = stdlib_re.sub(stdlib_re.escape(
Expand Down
31 changes: 26 additions & 5 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,9 @@ 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')
Expand All @@ -574,7 +574,28 @@ class C(Generic[T]):
self.assertNotEqual(Z, Y[T])

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

def test_new_repr(self):
T = TypeVar('T')
U = TypeVar('U', covariant=True)
S = TypeVar('S')

self.assertEqual(repr(List), 'typing.List[~T]')
self.assertEqual(repr(List[T]), 'typing.List[~T]')
self.assertEqual(repr(List[U]), 'typing.List[+U]')
self.assertEqual(repr(List[S][T][int]), 'typing.List[int]')
self.assertEqual(repr(List[int]), 'typing.List[int]')

def test_new_repr_complex(self):
T = TypeVar('T')
TS = TypeVar('TS')

self.assertEqual(repr(typing.Mapping[T, TS][TS, T]), 'typing.Mapping[~TS, ~T]')
self.assertEqual(repr(List[Tuple[T, TS]][int, T]),
'typing.List[typing.Tuple[int, ~T]]')
self.assertEqual(repr(List[Tuple[T, T]][List[int]]),
'typing.List[typing.Tuple[typing.List[int], typing.List[int]]]')

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

class Y(C[int]):
pass
Expand Down
6 changes: 4 additions & 2 deletions src/typing.py
Original file line number Diff line number Diff line change
61A7 Expand Up @@ -1010,8 +1010,10 @@ def __repr__(self):

def _argrepr(self):
if self.__origin__ is None:
return '[%s]' % (
', '.join(_type_repr(p) for p in self.__parameters__))
if self.__parameters__:
return '[%s]' % (
', '.join(_type_repr(p) for p in self.__parameters__))
return ''
r = self.__origin__._argrepr()
for i, arg in enumerate(self.__args__):
r = stdlib_re.sub(stdlib_re.escape(
Expand Down
0