-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Generic argument is not being identified #18933
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
Comments
Minimized: def some[T]() -> None:
x: list[T]
reveal_type(x) |
This is funny! Yes, type vars are now bound in the function body if typevar is used in argument types or return types (or comes from parent scope - class or function). It's a bug to not bind them by PEP695 syntax ("name-defined" is wrong error code). However, your usage (and especially the simplified snippet in the previous comment) is still invalid - semantically, from typing import TypeVar
T = TypeVar("T")
def foo() -> None:
x: T # E: Type variable "__main__.T" is unbound [valid-type] \
# N: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) \
# N: (Hint: Use "T" in function signature to bind "T" inside a function) where mypy produces a correct error. IMO the cleanest solution would be to emit exactly the same error in the OP case. |
Thanks for responding @sterliakov. I tried a slightly modified version of @A5rocks' snippet (assigned Pyright ( Mypy ( Is Pyright wrong and should I raise an issue there? PS: I am aware I might have repeated what you both mentioned with different phrasing, but if I am planning to raise this in Pyright, I believe a single comment with the entire context could be useful. PPS: This might be tangential and I can move this part to discussion (or any other relevant forum), do you mind helping me understand how I can express my original use case in mypy if this is intended behavior? I want |
No, I think this should work with PEP 695 syntax. The user is explicitly binding the type variable in the function and mypy should respect that intent even if there's no arguments for it (maybe there should be an error about the @Alc-Alc the reason this doesn't work for def f():
def g():
x: T Which function is generic in but also, what @sterliakov is pointing out is that your function does not take any argument for You probably mean something like this: from typing import cast, reveal_type
from collections.abc import Callable
class Repo[T, U]: ...
def some[U, **P](func: Callable[P, U]) -> Callable[P, U]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> U:
reveal_type(cast(Repo[object, U], args[0]))
return func(*args, **kwargs)
return wrapper or even better: def some[T, U, **P](func: Callable[Concatenate[Repo[T, U], P], U]) -> Callable[Concatenate[Repo[T, U], P], U]:
def wrapper(repo: Repo[T, U], /, *args: P.args, **kwargs: P.kwargs) -> U:
reveal_type(repo)
return func(repo, *args, **kwargs)
return wrapper (all above code is untested) |
Bug Report
The code in this mypy playground raises the following error.
However, as seen in this pyright playground there is no error, which leads me to believe that the snippet is well defined.
This one is probably related but the revealed types do not seem to match as well (the
?
in the type name seems to be rather strange).mypy
pyright
To Reproduce (this is the same code as in the mypy playground)
Expected Behavior
No errors from mypy, similar behavior to pyright.
Actual Behavior
I am not sure if mypy is misdiagnosing this, if you feel this is intended behavior I can try raising an issue with pyright.
Your Environment
mypy.ini
(and other config files): N/AThank You
Edit: Simplified the example a bit by removing the bounds and awaitables.
The text was updated successfully, but these errors were encountered: