-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Incorrect mypy behavor when "type unpacking". #15997
Comments
Might be related to #13337. |
The code in this sample contains a bug. The type variable Here's the corrected code, which type checks without error in mypy: from typing import ParamSpec, TypeVar, TypeAlias, Callable
P = ParamSpec("P")
T2 = TypeVar("T2")
CallableOrString: TypeAlias = Callable[P, T2] | str
def test(var: CallableOrString[[int, int], str]):
if isinstance(var, str):
return var
return var(1, 2) |
Hey @erictraut Indeed, this is an error on my part, but the error was unhelpful, would it be possible to improve it? And also it is weird that
Again, your code works correctly on runtime and TypeAlias is correctly resolved there, but it doesn't work with mypy. |
My bad, your code works. I just was running it improperly. |
Mypy is behaving as expected here. The one area for improvement is that we could perhaps improve the error message, maybe to something like "tuple not supported here". If someone wants to pick that up, feel free to open a PR, but closing this issue as the type checker behavior is correct. |
I actually also had another problem with this, but it turned out that it was #13337 (which still is an issue for me). |
Bug Report
When running provided script, mypy incorrectly flags it as
Syntax error in type annotation
even though it works perfectly fine in the interpreter.To Reproduce
https://gist.github.com/mypy-play/6d44ea039080875ea31e83c4479fd7da
https://mypy-play.net/?mypy=master&python=3.10&gist=6d44ea039080875ea31e83c4479fd7da
Expected Behavior
Should work like in interpreter:
Mypy should resolve
CallableOrString[(int, int), str]
totyping.Union[typing.Callable[[int, int], str], str]
.Actual Behavior
Your Environment
mypy file.py
ormypy -c [code]
mypy.ini
(and other config files): [none]The text was updated successfully, but these errors were encountered: