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
I believe this should type-check without issue; in particular, by my reading of max()'s type annotations in typeshed, the key callable should be expected to take only those types contained in the iterable. However, mypy disagrees:
max-arg2.py:6: error: Argument "key" to "max" has incompatible type "Callable[[str], str]"; expected "Callable[[str | None], SupportsDunderLT[Any] | SupportsDunderGT[Any]]" [arg-type]
Note that mypy expects the key callable to accept str | None even though strs is an Iterable[str]. Moreover, if the default argument is omitted and the return type of best_str() is changed to str, then mypy accepts the code.
Your Environment
Mypy version used: 1.6.0
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): None
Python version used: 3.11.6
The text was updated successfully, but these errors were encountered:
default is a red herring. This has nothing to do with any fancy inference logic, it is plain old overusing of outer context. I bet if you will rewrite it as temp = max(...); return temp it will work. A while ago I added a special-casing for this for assignments, see #14151, I guess we may want to have matching special-casing for return.
Consider the following code:
I believe this should type-check without issue; in particular, by my reading of
max()
's type annotations in typeshed, thekey
callable should be expected to take only those types contained in the iterable. However, mypy disagrees:Note that mypy expects the
key
callable to acceptstr | None
even thoughstrs
is anIterable[str]
. Moreover, if thedefault
argument is omitted and the return type ofbest_str()
is changed tostr
, then mypy accepts the code.Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: