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
(🐞) isinstance works with typing.Unions on 3.10+ #7505
Strictly speaking the Union[str, int] object being passed to the union is of type typing._SpecialForm (in the stubs at least — not at runtime) and it's true that the stub doesn't currently allow for the possibility of passing in _SpecialForm objects.
I'm not sure this is really a problem that should be solved in the stubs, though. If we add _SpecialForm to the union of types that's allowed for the second argument, that might mean that type checkers might start allowing things like isinstance(1, Literal[1]), which fails at runtime. Given that it's really only this specific special form that works with isinstance, I think it's probably better for a type checker to special-case it.
@AlexWaygood Actually it's more specifically a typing._UnionGenericAlias to typing.Union which is an instance of _SpecialForm . We could just stub a fake subtype of _SpecialForm only for Unions, and use that to annotate.
In general I'm slightly leery of overcomplicating typing.pyi by reproducing the exact runtime implementation details, but if there's a way to do it simply, then sure!
Activity
srittau commentedon Mar 17, 2022
The stubs look correct to me:
typeshed/stdlib/builtins.pyi
Lines 1161 to 1172 in e8286e7
I believe that his is purely a mypy bug, but someone could correct me here.
KotlinIsland commentedon Mar 17, 2022
@srittau Thanks for the response!
That stub doesn't contain anything about
typing.Union.Could you please elaborate?
AlexWaygood commentedon Mar 17, 2022
Strictly speaking the
Union[str, int]object being passed to the union is of typetyping._SpecialForm(in the stubs at least — not at runtime) and it's true that the stub doesn't currently allow for the possibility of passing in_SpecialFormobjects.I'm not sure this is really a problem that should be solved in the stubs, though. If we add
_SpecialFormto the union of types that's allowed for the second argument, that might mean that type checkers might start allowing things likeisinstance(1, Literal[1]), which fails at runtime. Given that it's really only this specific special form that works withisinstance, I think it's probably better for a type checker to special-case it.KotlinIsland commentedon Mar 17, 2022
@AlexWaygood Actually it's more specifically a
typing._UnionGenericAliastotyping.Unionwhich is an instance of_SpecialForm. We could just stub a fake subtype of_SpecialFormonly forUnions, and use that to annotate.I could make a pr when I get time.
AlexWaygood commentedon Mar 17, 2022
Cool, I'll happily take a look at a PR!
In general I'm slightly leery of overcomplicating
typing.pyiby reproducing the exact runtime implementation details, but if there's a way to do it simply, then sure!KotlinIsland commentedon Mar 17, 2022
Something roughly like this:
KotlinIsland commentedon Mar 17, 2022
@AlexWaygood Here you go 😁 #7508
[-](🐞) `isinstance` works with `typing.Union`s[/-][+](🐞) `isinstance` works with `typing.Union`s on 3.10+[/+]AlexWaygood commentedon Jun 12, 2022
The consensus in #7508 was that this was an unusual and niche use case, so it's probably best to close this as a "wontfix".