python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.42k stars 2.82k forks source link

wrongly reported typing error in `min` with `key` lambda function and default value when result is Optional #17221

Open jan-spurny opened 5 months ago

jan-spurny commented 5 months ago

Bug Report

When using key function in min builtin function, default None value and returning it from a function which has an Optional type, mypy reports a problem where I believe there is none.

To Reproduce

from dataclasses import dataclass
from typing import List

@dataclass
class X:
    x: int

def get_min(vals: List[X]) -> X | None:
    return min(vals, key = lambda tsync: tsync.x, default=None)

result = get_min([X(1), X(2), X(3)])

Expected Behavior

No errors reported.

Actual Behavior

$ mypy b.py --pretty
b.py:11: error: Item "None" of "X | None" has no attribute "x"  [union-attr]
        return min(vals, key = lambda tsync: tsync.x, default=None)
                                             ^~~~~~~
Found 1 error in 1 file (checked 1 source file)

The error goes away if I remove the default=None or when the min is not in a function with X | None return type:

from dataclasses import dataclass
from typing import List

@dataclass
class X:
    x: int

def get_min_1(vals: List[X]) -> X | None:
    return min(vals, key = lambda tsync: tsync.x, default=None)

def get_min_2(vals: List[X]) -> X | None:
    return min(vals, key = lambda tsync: tsync.x) # <- this is fine

vals = [X(1), X(2), X(3)]

result1 = get_min_1(vals)
result2 = get_min_2(vals)
result3 = min(vals, key = lambda tsync: tsync.x, default=None) # <- this is fine

Here, mypy complains only about get_min_1:

$ mypy a.py --pretty
a.py:11: error: Item "None" of "X | None" has no attribute "x"  [union-attr]
        return min(vals, key = lambda tsync: tsync.x, default=None)
                                             ^~~~~~~
Found 1 error in 1 file (checked 1 source file)

Your Environment

sterliakov commented 4 months ago

This is not a stub bug as I expected initially when got bitten by this. min is defined correctly with two different typevars. This is a strange failure of inference.

The following snippet demonstrates the weird inconsistency:

from __future__ import annotations

def foo(x: list[list[int]]) -> list[int] | None:
    reveal_type(min(x, key=len, default=None))  # N: Revealed type is "Union[builtins.list[builtins.int], None]"
    return min(x, key=len, default=None)  # E: Argument "key" to "min" has incompatible type "Callable[[Sized], int]"; expected "Callable[[list[int] | None], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]

foos: list[list[int]]
reveal_type(min(foos, key=len, default=None))  # N: Revealed type is "Union[builtins.list[builtins.int], None]"
min_foo: list[int] | None = min(foos, key=len, default=None)  # E: Argument "key" to "min" has incompatible type "Callable[[Sized], int]"; expected "Callable[[list[int] | None], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]

Note that return line produces an arg-type error, but the same expression (!) on the previous line has type assignable to return type. The same happens outside of the function with and without output types.

min is defined in typeshed as

@overload
def min(
    arg1: SupportsRichComparisonT, arg2: SupportsRichComparisonT, /, *_args: SupportsRichComparisonT, key: None = None
) -> SupportsRichComparisonT: ...
@overload
def min(arg1: _T, arg2: _T, /, *_args: _T, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
@overload
def min(iterable: Iterable[SupportsRichComparisonT], /, *, key: None = None) -> SupportsRichComparisonT: ...
@overload
def min(iterable: Iterable[_T], /, *, key: Callable[[_T], SupportsRichComparison]) -> _T: ...
@overload
def min(iterable: Iterable[SupportsRichComparisonT], /, *, key: None = None, default: _T) -> SupportsRichComparisonT | _T: ...
@overload
def min(iterable: Iterable[_T1], /, *, key: Callable[[_T1], SupportsRichComparison], default: _T2) -> _T1 | _T2: ...

The relevant overload is the last one.

Here's a playground link: https://mypy-play.net/?mypy=master&python=3.11&flags=strict&gist=8b209a7e7c9ddf0c03f3f6dfb0153316