You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
String representation for types created with typing.NewType(…) are opaque and unappealing #79144
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-12-27.11:29:52.903>created_at=<Date2018-10-12.03:03:15.563>labels= ['3.7', '3.8', 'type-bug', 'library']
title='String representation for types created with typing.NewType(\xe2\x80\xa6) are opaque and unappealing'updated_at=<Date2021-12-27.11:29:52.903>user='https://github.com/fish2000'
When creating a new type with typing.NewType(…) the result is a function defined on-the-fly with no reasonable __repr__ definition – the string representation of the type looks something like e.g. “<function NewType..new_type at 0x108ae6950>” which isn’t very useful or aesthetically appealing.
You can determine the module of the caller by using sys._getframe().
fish2000 commented on Oct 12, 2018
fish2000mannequin
MannequinAuthor
More actions
The proposed solution in the PR replaces the identity-function return value of NewType(…) with a callable class instance that adds an explicit __repr__ function – which that function cobbles together the string representations of the supplied type and the new types’ name into a sensible and stylistically consistent “repr” output.
In this capacity the __name__ vs. __qualname__ consideration would appear to be an implementation detail – in this case, calculating a __qualname__ value, á la PEP-3155, doesn’t seem to be possible nor advantageous, in this specific case.
On the other hand, adding __module__ seems like it’d be a win – although I am wary about introducing a dependency on a function that starts with an underscore. How reliable and/or portable is sys._getframe() ?
There are disadvantages of using a callable class. First, creating a class is 10-100 times slower than creating a function. Second, calling it is slower than calling a function.
sys._getframe() is already used in the typing module (and also in collections and enum).
OK, so now we have two "competing" PRs. I think I like Serhiy's PR a bit more, since it is simpler. I would propose to look at benchmarks and then decide. Are there any other factors I am missing for choosing one or other approach?
PR 9951 is simpler, but it makes invoking NewType() slower, because retrieving the module from the caller frame is slow in comparison with other operations.
What other operations should be supported? Should the NewType() result be pickleable? Currently it is not, in contrary to other typing types. If it should be pickleable, should it be pickled by name (resulting in restoring the same instance on unpickling, but failing if it is not accessible by name) or by its arguments (resulting in creating a new instance when unpickled)? Should it support comparison and what values should be treated as equal?
If make NewType a class, I would make the repr looking like a call:
Serhiy, thanks for benchmarks and good suggestions!
If make NewType a class, I would make the repr looking like a call
This is a nice idea, it indeed looks better. We can probably also use _type_repr() helper for the argument (for consistency).
Should the NewType() result be pickleable?
This is not required by PEP-484, but I could imagine this may be useful.
If it should be pickleable, should it be pickled by name (resulting in restoring the same instance on unpickling, but failing if it is not accessible by name) or by its arguments (resulting in creating a new instance when unpickled)?
Logically, they should behave like class objects, so if we are going to make them pickleable, they should be pickled by full name.
Should it support comparison and what values should be treated as equal?
Again, since they should behave like class objects, I think we don't need any dedicated __eq__, they should be just compared by identity.
From performance point of view, both PRs look good. Maybe we can even combine both, so that we have both fine-tuned repr and pickleability. I understand this means we will get slower benchmarks for both creation and instantiation, but I think it is still OK, since it is still much faster than creating a new class.
It looks like Ivan approved your PR and was ready to merge it, but left it up to you for the final decision about incorporating changes from the competing PR. This is just a friendly ping in case you want to get it in for 3.8. Thanks!
Activity
fish2000 commentedon Oct 12, 2018
When creating a new type with
typing.NewType(…)the result is a function defined on-the-fly with no reasonable__repr__definition – the string representation of the type looks something like e.g. “<function NewType..new_type at 0x108ae6950>” which isn’t very useful or aesthetically appealing.serhiy-storchaka commentedon Oct 12, 2018
For repr you need to set __qualname__, not just __name__. It may be worth to set also __module__ for pickleability and better help.
serhiy-storchaka commentedon Oct 12, 2018
You can determine the module of the caller by using sys._getframe().
fish2000 commentedon Oct 12, 2018
The proposed solution in the PR replaces the identity-function return value of
NewType(…)with a callable class instance that adds an explicit__repr__function – which that function cobbles together the string representations of the supplied type and the new types’ name into a sensible and stylistically consistent “repr” output.In this capacity the
__name__vs.__qualname__consideration would appear to be an implementation detail – in this case, calculating a__qualname__value, á la PEP-3155, doesn’t seem to be possible nor advantageous, in this specific case.On the other hand, adding
__module__seems like it’d be a win – although I am wary about introducing a dependency on a function that starts with an underscore. How reliable and/or portable issys._getframe()?serhiy-storchaka commentedon Oct 12, 2018
There are disadvantages of using a callable class. First, creating a class is 10-100 times slower than creating a function. Second, calling it is slower than calling a function.
sys._getframe() is already used in the typing module (and also in collections and enum).
serhiy-storchaka commentedon Oct 18, 2018
PR 9951 is a simpler way. It just sets __qualname__ (and __module__ for completeness).
ilevkivskyi commentedon Oct 21, 2018
OK, so now we have two "competing" PRs. I think I like Serhiy's PR a bit more, since it is simpler. I would propose to look at benchmarks and then decide. Are there any other factors I am missing for choosing one or other approach?
serhiy-storchaka commentedon Oct 28, 2018
PR 9951 is simpler, but it makes invoking NewType() slower, because retrieving the module from the caller frame is slow in comparison with other operations.
PR 9808:
1000000 loops, best of 5: 316 nsec per loop
PR 9951:
500000 loops, best of 5: 634 nsec per loop
PR 9951 without setting __module__ and stripping components before dot in __name__:
1000000 loops, best of 5: 316 nsec per loop
In contrary, calling the resulting type is slower with PR 9808:
PR 9808:
2000000 loops, best of 5: 105 nsec per loop
PR 9951:
5000000 loops, best of 5: 74.1 nsec per loop
What other operations should be supported? Should the NewType() result be pickleable? Currently it is not, in contrary to other typing types. If it should be pickleable, should it be pickled by name (resulting in restoring the same instance on unpickling, but failing if it is not accessible by name) or by its arguments (resulting in creating a new instance when unpickled)? Should it support comparison and what values should be treated as equal?
If make NewType a class, I would make the repr looking like a call:
ilevkivskyi commentedon Oct 28, 2018
Serhiy, thanks for benchmarks and good suggestions!
This is a nice idea, it indeed looks better. We can probably also use
_type_repr()helper for the argument (for consistency).This is not required by PEP-484, but I could imagine this may be useful.
Logically, they should behave like class objects, so if we are going to make them pickleable, they should be pickled by full name.
Again, since they should behave like class objects, I think we don't need any dedicated
__eq__, they should be just compared by identity.From performance point of view, both PRs look good. Maybe we can even combine both, so that we have both fine-tuned repr and pickleability. I understand this means we will get slower benchmarks for both creation and instantiation, but I think it is still OK, since it is still much faster than creating a new class.
csabella commentedon May 23, 2019
Serhiy,
It looks like Ivan approved your PR and was ready to merge it, but left it up to you for the final decision about incorporating changes from the competing PR. This is just a friendly ping in case you want to get it in for 3.8. Thanks!
uriyyo commentedon Jul 23, 2021
NewTypewas converted into callable class at scope of https://bugs.python.org/issue44353Currently
reprofNewTypeis: