-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Using Generic and __new__ in class def, mypy misses incorrect parameter if parameter created inline #14009
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
I think mypy is correct here. The behavior you cited for pyright was due to a bug which was fixed a while ago. Pyright and mypy now agree. |
Why do you think that both pyright and mypy are correct? Why should the 2 calls fun(param=Foo(str))
p = Foo(str)
fun(p) to the function |
I believe it's due to type context (Eric calls the equivalent technique in pyright 'bidirectional inference'). When a particular type is expected, type checkers try to infer that type to minimize errors. However, when the value is assigned to a variable, that assignment is type checked independently and sets the type. Consider code like this: def f(x: list[object]) -> None: pass
y = [1]
f(y)
f([1]) Here mypy shows an error for the first call, but not the second. |
I don't think your example fits in this case, because In my example, you have def fun(param: Foo[int]) -> Foo[str]:
...
fun(param=Foo(str))
p = Foo(str)
fun(p) So I don't see how |
When you call By contrast, if you construct a It's similar to this: a1 = [1, 2, 3]
x1: list[float] = a # Type error
x2: list[float] = [1, 2, 3] # No type error |
@erictraut thanks for the example. It's very subtle, and difficult for people using type checkers to understand. |
Bug Report
This came up in pandas-stubs, issue pandas-dev/pandas-stubs#412
If you have a stub that defines a
Generic
class with__new__()
, with overloads, and the final overload doesn't specify the type of the generic, then a function call that creates an object of the generic class in the function call is not picked up as an error, but it is picked up if stored in another variable.To Reproduce
Two files are needed. First is
gptst2.pyi
:Then the tester code
genparam.py
:Expected Behavior
mypy should report errors on both calls to
fun()
Actual Behavior
mypy does not pick up that the first call to
fun
is incorrect. It does pick up that the second call is incorrect.pyright picks up both as being incorrect.
Output from mypy:
Your Environment
--no-incremental
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: