-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Union requires two or more type arguments" reported for non-type annotation usage of Union
(e.g. introspection)
#2014
Comments
The same issue appears with |
The issue with |
fixed in 2021.11.0 |
This is still reported when tup = int, float
Union[tup] # Union requires two or more type arguments
Union[int] # same |
This isn't valid. It doesn't crash at runtime, but I'll also note that the use of |
and what is a valid way to create union out of tuple of arguments? |
There isn't a valid way to do this in the Python type system. Could you describe your use case in more detail? You can use a type alias that refers to a union to define another union, if that helps. |
My use case is the same as the original: runtime introspection. Mypy doesn't error if |
As mentioned above - runtime introspection. My primary goal is "un-Optional" type. I check if type is Union\UnionType, and ensure that there is one last NoneType in get_args(my_type). I worked-around that by following code:
seems to be working fine, and mypy & pyright are happy about that. ps: Union and UnionType(str | int gives UnionType) seems to be different when comes to get_origin, still Union[str, int] == str | int equals True |
Environment data
Expected behaviour
typing.Union
with no subscript is valid when it is used in certain situations other than type annotation, such as for runtime introspection, for example, usingtyping.get_origin
:In such use cases, Pylance should not report issues.
Actual behaviour
Pylance reports "Union requires two or more type arguments."
This seems to be introduced in the latest 2021.10.3 (October 27) version, in this feature:
The text was updated successfully, but these errors were encountered: