mypy
mypy copied to clipboard
False "incompatible type overloaded function" for map(sorted, ...)
Bug Report
Mypy gives arg-type error when calling map with sorted as the mapping function.
To Reproduce
x = list(map(sorted, [[1, 3, 2], [2, 5, 4, 0]]))
assert x == [[1, 2, 3], [0, 2, 4, 5]]
Expected Behavior
Mypy should not complain.
Actual Behavior
Mypy errors with
error: Argument 1 to "map" has incompatible type overloaded function; expected "Callable[[List[int]], List[_LT]]" [arg-type]
Your Environment
- Mypy version used: 0.790
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.6.9
This seems to be a regression from mypy 0.780 to 0.790.
The case I ran into is extremely similar:
import typing
def test(a: typing.Dict[int, typing.List[int]]) -> typing.List[typing.List[int]]:
return sorted(map(sorted, a.values()))
Recently had this issue with:
map(sum, [1, 2])
Causing the same error (but with sum)
i'm using mypy 0.910 with python 3.9 and getting a simliar error.
min(dict_var, key=dict_var.get)
yields:
python Argument "key" to "min" has incompatible type overloaded function; expected "Callable[[str], SupportsLessThan]"
where my 'supports less than' object is a datetime object which does support less than
and the code works per my existing unit tests
I'm getting same but with Python 3.7 and mypy 0.971:
dct: Dict[str, int] = {'a': 0, 'b': 1}
result: List[str] = list(sorted(dct, key=dct.get))
error: Argument "key" to "sorted" has incompatible type overloaded function; expected "Callable[[str], Union[SupportsDunderLT, SupportsDunderGT]]"
@huntfx dct.get is typed as being able to return None and a list containing Nones and ints is not sortable.
@huntfx You can use key=lambda x: dct.get(x) or -1
@victor-paltz Thanks, though I do like to avoid using lambda when more efficient ways are possible. In the end I just used key=dct.__getitem__ instead.
I have this same problem with min:
size_to_copy = map(min, (1, 2), (3, 4))
gives
error: Argument 1 to "map" has incompatible type overloaded function; expected "Callable[[int, int], SupportsRichComparisonT]" [arg-type]
mypy 1.5.1, Python 3.10.2
Your example works with the --new-type-inference flag (which will likely be made the default in mypy 1.7)