-
Notifications
You must be signed in to change notification settings - Fork 258
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
Conversation
I like this. I've been struggling with the repr() quite a bit (in Python 3.5.0 it was quite different). I don't know if I'll have time to merge today but I hope soon. |
I just found a bug here, with very light QA. from typing import Any, Dict
class C(Dict[Any, Any]):
pass
print(C.__mro__) Gives a traceback:
|
Oops! I played with this on top of #295 most of the time, so I didn't notice this. Thanks! |
@gvanrossum I have a simple patch for http://bugs.python.org/issue27989 (better representation of function signatures by Are you going to merge this or you have some additional comments? |
Sorry, just temporarily ran out of time. Will get to it soon.
|
Not a problem at all. |
return '[%s]' % (', '.join(map(_type_repr, self.__parameters__))) | ||
r = self.__origin__._argrepr() | ||
return par_repr | ||
r = self.__origin__._arg_repr() | ||
for i in range(len(self.__args__)): # replace free parameters with args | ||
par = stdlib_re.escape(_type_repr(self.__origin__.__parameters__[i])) | ||
r = stdlib_re.sub(par + '(?=[,\]])', '{%r}' % i, r) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry for only just now looking at this code in detail. I really don't like the idea that we're taking some other class's _arg_repr()
and then doing a regex substitution on it. Especially since this is just calling itself recursively. There's got to be a better way.
@gvanrossum While working on this, I have found three very minor bugs (I have fixed them right here) and three bigger things that I think are bugs, I will open issues on tracker soon. |
@gvanrossum I forgot to add, I changed the repr for bare generics, now
|
Yeah, that's why the previous repr would return 'List<~T>' (and that was an
improvement over the original that returned 'List[~T]'). I agree that just
plain 'List' is fine too; if we want to know the numbers and names of the
parameters we can write `List.__parameters__`.
|
I'm giving you the benefit of the doubt here -- it looks fine but I'm out of time this week to really understand how this works. I trust it's easy enough to understand when someone finds a bug though. |
@gvanrossum
Currently,
repr
for generics acts in naive way, so that sometimes this leads to cumbersome representations. For example,prints the "whole history"
C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]
. While the newrepr
will print simply the "final result"C[typing.Tuple[str, int]]
. Also for simpler cases,repr(List[T])
would be simplytyping.List[~T]
, nottyping.List<~T>[~T]<~T>
.The new
repr
does not use angular brackets in addition to square brackets (free parameters are anyway marked with~
,+
or-
), see added tests.The implementation might look a bit hacky, but this is done in order to be sure that the
repr
will not interfere with those ofsuper()
and will treat correctly complex cases with repeated occurrences of type variables.