This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients fish2000, levkivskyi, serhiy.storchaka
Date 2018-10-28.16:13:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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])
Date User Action Args
2018-10-28 16:13:09serhiy.storchakasetrecipients: + serhiy.storchaka, levkivskyi, fish2000
2018-10-28 16:13:09serhiy.storchakasetmessageid: <>
2018-10-28 16:13:09serhiy.storchakalinkissue34963 messages
2018-10-28 16:13:09serhiy.storchakacreate