You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Misleading warning for generic argument type on wrong return type #12156
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.
Looking at this, I would expect Mypy to complain that the declared return value of foo() (being int) 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 be int, thus complaining about the argument type instead.
a.py: note: In function "foo":
a.py:15: error: Argument 2 to "coalesce" has incompatible type "None"; expected "int" [arg-type]
return coalesce(42, None)
^
Found 1 error in 1 file (checked 1 source file)
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):
changed the title [-]Misleading warning for argument type on wrong return type[/-][+]Misleading warning forgeneric argument type on wrong return type[/+]on Feb 9, 2022
changed the title [-]Misleading warning forgeneric argument type on wrong return type[/-][+]Misleading warning for generic argument type on wrong return type[/+]on Feb 9, 2022
This is presumably a consequence of mypy's "type context" heuristic: it expects you to return an int, so it typechecks the coalesce() call in a way that sets the return type to int.
if you first put the result of coalesce in a variable, I believe you'll get the result you expect.
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.
Activity
[-]Misleading warning for argument type on wrong return type[/-][+]Misleading warning forgeneric argument type on wrong return type[/+][-]Misleading warning forgeneric argument type on wrong return type[/-][+]Misleading warning for generic argument type on wrong return type[/+]JelleZijlstra commentedon Feb 9, 2022
This is presumably a consequence of mypy's "type context" heuristic: it expects you to return an int, so it typechecks the
coalesce()call in a way that sets the return type toint.if you first put the result of
coalescein a variable, I believe you'll get the result you expect.NiklasRosenstein commentedon Feb 23, 2022
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.
Extend special case for context-based typevar inference to typevar un…