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
Union of a type and the typing module function #88808
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=<Date2022-01-23.07:25:17.626>created_at=<Date2021-07-15.07:58:18.901>labels= ['interpreter-core', '3.10', '3.11']
title='Union of a type and the typing module function'updated_at=<Date2022-01-23.07:25:17.625>user='https://github.com/serhiy-storchaka'
It is only reminder that our test for NewType is arbitrary. And it is not compatible with PR 9951. Possible solutions are:
Make NewType a class and add more strict test. Pro -- we can make arbitrary repr, contra -- it has small negative effect on performance. See bpo-34963.
Remove the test for NewType at all and allow or-ing types and arbitrary functions.
Introduce a special attribute to distinguish unionable objects. It will make the code of unionobject.c simpler.
I lean towards option 1 (not excluding 3). Small performance losses seem inevitable.
I think making NewType into a class isn't a good idea, it would be too slow. But I like bpo-34963 (PR 9951).
So then let's do some variation on (3) -- unionable things would include all types (of course), typevars, and things that have a special attribute. We could then set that special attribute on the function returned by NewType (like PR 9951 already does for __name__ and __qualname__).
Python 3.11.0a4+ (heads/main-dirty:ef3ef6fa43, Jan 20 2022, 20:48:25) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> int | typing.cast
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'function'
>>> int | typing.get_type_hints
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'function'
Looks like it was fixed indeed, NewType is now a class. And I cannot reproduce it even on 3.10:
Python 3.10.0 (default, Nov 1 2021, 10:24:06) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> int | typing.cast
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'function'
>>> int | typing.get_type_hints
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'function'
Activity
serhiy-storchaka commentedon Jul 15, 2021
The union type accepts any function from the typing module:
It is a consequence of too lenient check for the typing.NewType() result (which does not work at all with PR 9951).
gvanrossum commentedon Jul 15, 2021
Is it worth being picky about this? The internal data structures are safe. A static checker will complain. What more do you need?
serhiy-storchaka commentedon Jul 15, 2021
It is only reminder that our test for NewType is arbitrary. And it is not compatible with PR 9951. Possible solutions are:
Make NewType a class and add more strict test. Pro -- we can make arbitrary repr, contra -- it has small negative effect on performance. See bpo-34963.
Remove the test for NewType at all and allow or-ing types and arbitrary functions.
Introduce a special attribute to distinguish unionable objects. It will make the code of unionobject.c simpler.
I lean towards option 1 (not excluding 3). Small performance losses seem inevitable.
Fidget-Spinner commentedon Jul 15, 2021
@serhiy
See also: bpo-44353 (https://bugs.python.org/issue44353). We had some discussion there about converting to class but it ended in a deadlock.
gvanrossum commentedon Jul 16, 2021
I think making NewType into a class isn't a good idea, it would be too slow. But I like bpo-34963 (PR 9951).
So then let's do some variation on (3) -- unionable things would include all types (of course), typevars, and things that have a special attribute. We could then set that special attribute on the function returned by NewType (like PR 9951 already does for __name__ and __qualname__).
What to call it? Maybe "__unionable__"?
sobolevn commentedon Jan 22, 2022
It does not happen anymore on
main(3.11):gvanrossum commentedon Jan 23, 2022
Do you recommend to just close this as "fixed"?
sobolevn commentedon Jan 23, 2022
Looks like it was fixed indeed,
NewTypeis now a class. And I cannot reproduce it even on3.10: