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 with TypeVar does not work as intended #88798
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=<Date2021-07-15.07:26:44.915>created_at=<Date2021-07-14.05:33:24.289>labels= ['interpreter-core', 'type-bug', '3.10', '3.11']
title='Union with TypeVar does not work as intended'updated_at=<Date2021-07-15.07:26:44.915>user='https://github.com/serhiy-storchaka'
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.
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.
Good question. It works correctly in list[T]. There is a tiny difference between implementations of the check in genericaliasobject.c and unionobject.c.
Activity
serhiy-storchaka commentedon Jul 14, 2021
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.
gvanrossum commentedon Jul 14, 2021
The code for recognizing TypeVars must be wrong. Is it also wrong in e.g. list[T]?
Fidget-Spinner commentedon Jul 14, 2021
Oh this is a fun one :).
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.
serhiy-storchaka commentedon Jul 14, 2021
Good question. It works correctly in list[T]. There is a tiny difference between implementations of the check in genericaliasobject.c and unionobject.c.
serhiy-storchaka commentedon Jul 14, 2021
New changeset a158b20 by Serhiy Storchaka in branch 'main':
bpo-44632: Fix support of TypeVar in the union type (GH-27139)
a158b20
serhiy-storchaka commentedon Jul 15, 2021
New changeset cc1a47c by Miss Islington (bot) in branch '3.10':
bpo-44632: Fix support of TypeVar in the union type (GH-27139) (GH-27143)
cc1a47c