Issue 44632: Union with TypeVar does not work as intended
Created on 2021-07-14 05:33 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 27139 | merged | serhiy.storchaka, 2021-07-14 15:56 | |
| PR 27143 | merged | miss-islington, 2021-07-14 17:09 | |
| Messages (6) | |||
|---|---|---|---|
| msg397466 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * ![]() |
Date: 2021-07-14 05:33 | |
>>> import typing
>>> int | typing.T
int | ~T
>>> typing.T | int
typing.Union[~T, int]
>>> T2 = TypeVar('T2')
>>> int | T2
typing.Union[int, ~T2]
>>> T2 | int
typing.Union[~T2, int]
There is a support of TypeVar in type.__or__, but it does not work with user defined TypeVars, only with internal TypeVars defined in the typing module.
|
|||
| msg397482 - (view) | Author: Guido van Rossum (gvanrossum) * ![]() |
Date: 2021-07-14 14:10 | |
The code for recognizing TypeVars must be wrong. Is it also wrong in e.g. list[T]? |
|||
| msg397486 - (view) | Author: Ken Jin (kj) * ![]() |
Date: 2021-07-14 15:27 | |
Oh this is a fun one :). > The code for recognizing TypeVars must be wrong. Is it also wrong in e.g. list[T]? list[T] is correct. I was pretty puzzled since I thought their code is nearly the same, but then I noticed a subtle difference -- GenericAlias checks for type(o).__module__ == "typing", while Union checks for o.__module__ == "typing", and o.__module__ is wherever the user defines it. |
|||
| msg397487 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * ![]() |
Date: 2021-07-14 15:37 | |
Good question. It works correctly in list[T]. There is a tiny difference between implementations of the check in genericaliasobject.c and unionobject.c. |
|||
| msg397497 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * ![]() |
Date: 2021-07-14 17:09 | |
New changeset a158b20019b50e3ece6e4743ec4e6ae8d818b690 by Serhiy Storchaka in branch 'main': bpo-44632: Fix support of TypeVar in the union type (GH-27139) https://github.com/python/cpython/commit/a158b20019b50e3ece6e4743ec4e6ae8d818b690 |
|||
| msg397526 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * ![]() |
Date: 2021-07-15 07:25 | |
New changeset cc1a47c849a206441c9b370b6ca954862a523082 by Miss Islington (bot) in branch '3.10': bpo-44632: Fix support of TypeVar in the union type (GH-27139) (GH-27143) https://github.com/python/cpython/commit/cc1a47c849a206441c9b370b6ca954862a523082 |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:47 | admin | set | github: 88798 |
| 2021-07-15 07:26:44 | serhiy.storchaka | set | status: open -> closed resolution: fixed stage: patch review -> resolved |
| 2021-07-15 07:25:40 | serhiy.storchaka | set | messages: + msg397526 |
| 2021-07-14 17:09:24 | miss-islington | set | nosy:
+ miss-islington pull_requests: + pull_request25685 |
| 2021-07-14 17:09:18 | serhiy.storchaka | set | messages: + msg397497 |
| 2021-07-14 15:56:30 | serhiy.storchaka | set | keywords:
+ patch stage: patch review pull_requests: + pull_request25681 |
| 2021-07-14 15:37:44 | serhiy.storchaka | set | messages: + msg397487 |
| 2021-07-14 15:27:27 | kj | set | messages: + msg397486 |
| 2021-07-14 14:10:30 | gvanrossum | set | messages: + msg397482 |
| 2021-07-14 11:05:35 | serhiy.storchaka | link | issue44633 dependencies |
| 2021-07-14 11:05:14 | serhiy.storchaka | link | issue44636 dependencies |
| 2021-07-14 05:33:24 | serhiy.storchaka | create | |
