classification
Title: Union with TypeVar does not work as intended
Type: behavior 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, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-07-14 05:33 by serhiy.storchaka, last changed 2021-07-15 07:26 by serhiy.storchaka. 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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
2021-07-15 07:26:44serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-07-15 07:25:40serhiy.storchakasetmessages: + msg397526
2021-07-14 17:09:24miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request25685
2021-07-14 17:09:18serhiy.storchakasetmessages: + msg397497
2021-07-14 15:56:30serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request25681
2021-07-14 15:37:44serhiy.storchakasetmessages: + msg397487
2021-07-14 15:27:27kjsetmessages: + msg397486
2021-07-14 14:10:30gvanrossumsetmessages: + msg397482
2021-07-14 11:05:35serhiy.storchakalinkissue44633 dependencies
2021-07-14 11:05:14serhiy.storchakalinkissue44636 dependencies
2021-07-14 05:33:24serhiy.storchakacreate