-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Misleading warning for generic argument type on wrong return type #12156
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
This is presumably a consequence of mypy's "type context" heuristic: it expects you to return an int, so it typechecks the if you first put the result of |
Hey @JelleZijlstra , you are right, that is a functioning workaround. There are others, but the issue is not that it can't be worked around but that when encountered it is confusing at first sight to understand what is happening. Working around it after the culprit is identified is not big of a deal. |
…ions in return position (#18976) * Fixes #17221. * Fixes #17654. * Fixes #17553. * Fixes #17536. * Fixes #16659. * Fixes #16267. * Fixes #15755. * Fixes #15150. * Fixes #14664. * Incidentally improves error message in #12156. * Fixes #12092. * Fixes #11985. * Improves #11455 (but the problem with union `TypeVar | SomeFixedType` reported in comments there remains). * Fixes #10426. When using context, we can perform some overly optimistic inference when return type is `T1 | T2`. This breaks in important case of `builtins.min` when `default` and `key` are passed, essentially making them always incompatible. This is not the most principled approach, but let's see the primer results. This resolves quite a few issues (some of them duplicates, but some - substantially different), `min` problem was a very popular one... Diff run: sterliakov/mypy-issues#30
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
It appears Mypy propagates types into the type arguments of a function call if the types on the left side are know, slightly obscuring the actual error.
To Reproduce
Run
mypy a.py
withExpected Behavior
Looking at this, I would expect Mypy to complain that the declared return value of
foo()
(beingint
) does not match what is actually returned (int | None
).Actual Behavior
It appears that Mypy propagates the return type into the
coalesce()
type arguments instead and expects them both to beint
, thus complaining about the argument type instead.I find this rather unintuitive. My suggestion would be to make Mypy complain about the type of the returned value instead of the argument type.
Your Environment
Mypy version used: 0.931
Mypy command-line flags: /
Mypy configuration options from
pyproject.toml
(and other config files):Python version used: 3.10.2
Operating system and version: WSL 2, Ubuntu 20
The text was updated successfully, but these errors were encountered: