Skip to content

For min/max functions a default value changes the key function type #6460

Closed
@kaste

Description

@kaste

Related but maybe not the same #3354

Given

from typing import List, Tuple
l: List[Tuple[int, str]] = []
v = max(l, key=lambda item: item[0], default=None)
reveal_type(v)

mypy complains about the item[0] usage in the key function:

Value of type "Optional[Tuple[int, str]]" is not indexable
Revealed type is 'Union[Tuple[builtins.int, builtins.str], None]'

Also interesting that it shows two different representations for the inferred type.
For user ergonomics the revealed type should be 'Optional[Tuple[int, str]]' as well, no?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions