Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jan-spurny opened this issue May 7, 2024 · 2 comments · May be fixed by #18976
Labels
bug mypy got something wrong

Comments

@jan-spurny
Copy link

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

  • Mypy version used: 1.8.0
  • Mypy command-line flags: none required for reproducting the bug
  • Mypy configuration options from mypy.ini (and other config files): (no config)
  • Python version used: 3.11
@jan-spurny jan-spurny added the bug mypy got something wrong label May 7, 2024
@sterliakov
Copy link
Collaborator

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

@gsakkis
Copy link

gsakkis commented Apr 26, 2025

Got bitten by this too; the weird thing is that assigning the expression to a (non annotated) name and returning the name works but returning the expression directly errors with Argument "key" to "min" has incompatible type "Callable[[Path], Any]"; expected "Callable[[Path | None], SupportsDunderLT[Any] | SupportsDunderGT[Any]]" [arg-type]

from pathlib import Path
from typing import Callable, Any, Iterable

def min_path_good(paths: Iterable[Path], key: Callable[[Path], Any]) -> Path | None:
    path = min(paths, key=key, default=None)
    return path

def min_path_bad(paths: Iterable[Path], key: Callable[[Path], Any]) -> Path | None:
    return min(paths, key=key, default=None)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
3 participants