mypy icon indicating copy to clipboard operation
mypy copied to clipboard

False "incompatible type overloaded function" for map(sorted, ...)

Open gsakkis opened this issue 4 years ago • 7 comments

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

gsakkis avatar Nov 28 '20 19:11 gsakkis

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()))

dgrunwald avatar May 18 '21 14:05 dgrunwald

Recently had this issue with: map(sum, [1, 2]) Causing the same error (but with sum)

GameDungeon avatar Jun 29 '22 03:06 GameDungeon

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

sonderyugen avatar Jul 08 '22 13:07 sonderyugen

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 avatar Aug 03 '22 13:08 huntfx

@huntfx dct.get is typed as being able to return None and a list containing Nones and ints is not sortable.

hauntsaninja avatar Aug 03 '22 17:08 hauntsaninja

@huntfx You can use key=lambda x: dct.get(x) or -1

victor-paltz avatar Sep 06 '22 14:09 victor-paltz

@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.

huntfx avatar Sep 06 '22 18:09 huntfx

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

levsa avatar Sep 20 '23 14:09 levsa

Your example works with the --new-type-inference flag (which will likely be made the default in mypy 1.7)

hauntsaninja avatar Sep 20 '23 17:09 hauntsaninja