Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

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.

$ ./python -m timeit -s "import typing" -- "UserId = typing.NewType('UserId', 
int)"

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:

$ ./python -m timeit -s "import typing" -s "UserId = typing.NewType('UserId', 
int)" -- "UserId(5)"

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:

>>> import typing
>>> UserId = typing.NewType('UserId', int)
>>> UserId
NewType('UserId', <class 'int'>)
>>> UserNames = typing.NewType('UserNames', typing.List[int])
>>> UserNames
NewType('UserNames', typing.List[int])

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue34963>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to