Repository navigation
Closed
Labels
Description
This is not an issue in 0.91, but it occurs in master. Given a metaclass that overrides __or__, the correct type is only maintained in the immediate result. If the result of the __or__ is stored then it will turn to builtins.object.
from typing import Union
class MyUnion(type): ...
class Meta(type):
def __or__(self, other) -> MyUnion: ... # type: ignore
class Foo(metaclass=Meta): ...
reveal_type(Foo | Foo) # line 12
isinstance(object(), Foo | Foo)
Bar = Foo | Foo
reveal_type(Bar) # line 14
isinstance(object(), Bar) # line 15Output in 0.91 (the behavior I expect):
test2.py:12: note: Revealed type is "test2.MyUnion"
test2.py:16: note: Revealed type is "test2.MyUnion"
Output in master:
test2.py:12: note: Revealed type is "test2.MyUnion"
test2.py:16: note: Revealed type is "builtins.object"
test2.py:17: error: Parameterized generics cannot be used with class or instance checks
test2.py:17: error: Argument 2 to "isinstance" has incompatible type "object"; expected "Union[type, UnionType, Tuple[Union[type, UnionType, Tuple[Any, ...]], ...]]"
Edit:
It occurs to me as I re-read PEP 604 that it doesn't specify that OR'd types may create an alias which can be used with isinstance, so maybe my example using it that way is not perfect, but I think the circumstances are still valid.
Edit Edit:
Reading through the PEP 604 discussion thread on typing-sig it seems that aliases composed of unions are to work with isinstance, so even simpler cases are currently broken:
class A: ...
class B: ...
C = A | B
reveal_type(C) # Revealed type is "builtins.object"
isinstance(object(), C) # error: Parameterized generics cannot be used with class or instance checks
# error: Argument 2 to "isinstance" has incompatible type "object"; expected "Union[type, UnionType, Tuple[Union[type, UnionType, Tuple[Any, ...]], ...]]"Semi-related, this is getting through:
isinstance(object(), int | list[int]) # no error (should be "error: Parameterized generics cannot be used with class or instance checks")Reacted by Alexandru Dreptu and brent-moffit
Activity
esoma commentedon Dec 1, 2021
Bisecting show this behavior change occurred from 56618b9
hunterC90 commentedon Feb 22, 2024
Looks like this should have the label
topic-pep-604.ilevkivskyi commentedon Feb 8, 2025
The current behavior is better:
and no error is given. The remaining inconsistency is because right hand side of a type alias is considered type context (i.e. like a type annotation), not runtime context. IMO this is OK, if this is still problematic for you, please feel free to re-open.