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.

classification
Title: Union of a type and the typing module function
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, kj, serhiy.storchaka, sobolevn
Priority: normal Keywords:

Created on 2021-07-15 07:58 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (8)
msg397528 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-07-15 07:58
The union type accepts any function from the typing module:

>>> import typing
>>> int | typing.cast
int | typing.cast
>>> int | typing.get_type_hints
int | typing.get_type_hints

It is a consequence of too lenient check for the typing.NewType() result (which does not work at all with PR 9951).
msg397558 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-07-15 15:14
Is it worth being picky about this? The internal data structures are safe. A static checker will complain. What more do you need?
msg397569 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-07-15 16:47
It is only reminder that our test for NewType is arbitrary. And it is not compatible with PR 9951. Possible solutions are:

1. Make NewType a class and add more strict test. Pro -- we can make arbitrary repr, contra -- it has small negative effect on performance. See issue34963.

2. Remove the test for NewType at all and allow or-ing types and arbitrary functions.

3. 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.
msg397570 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-07-15 16:56
@Serhiy
> 1. Make NewType a class and add more strict test.

See also: issue44353 (https://bugs.python.org/issue44353). We had some discussion there about converting to class but it ended in a deadlock.
msg397632 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-07-16 15:13
I think making NewType into a class isn't a good idea, it would be too slow. But I like issue34963 (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__"?
msg411298 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-22 22:50
It does not happen anymore on `main` (3.11):

```
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' 
```
msg411330 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-23 01:22
Do you recommend to just close this as "fixed"?
msg411351 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-23 07:25
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'
```
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88808
2022-01-23 07:25:17sobolevnsetstatus: open -> closed
resolution: fixed
messages: + msg411351

stage: resolved
2022-01-23 01:22:43gvanrossumsetmessages: + msg411330
2022-01-22 22:50:33sobolevnsetnosy: + sobolevn
messages: + msg411298
2021-07-20 01:32:57kjlinkissue44353 superseder
2021-07-16 15:13:51gvanrossumsetmessages: + msg397632
2021-07-15 16:56:07kjsetmessages: + msg397570
2021-07-15 16:47:27serhiy.storchakasetmessages: + msg397569
2021-07-15 15:14:43gvanrossumsetmessages: + msg397558
2021-07-15 07:58:18serhiy.storchakacreate