Skip to content

Mapping.get overload has covariant type as argument #9555

Closed as not planned
Closed as not planned
@randolf-scholz

Description

@randolf-scholz

typeshed/stdlib/typing.pyi

Lines 576 to 589 in 2b9f200

class Mapping(Collection[_KT], Generic[_KT, _VT_co]):
# TODO: We wish the key type could also be covariant, but that doesn't work,
# see discussion in https://github.com/python/typing/pull/273.
@abstractmethod
def __getitem__(self, __key: _KT) -> _VT_co: ...
# Mixin methods
@overload
def get(self, __key: _KT) -> _VT_co | None: ...
@overload
def get(self, __key: _KT, default: _VT_co | _T) -> _VT_co | _T: ...
def items(self) -> ItemsView[_KT, _VT_co]: ...
def keys(self) -> KeysView[_KT]: ...
def values(self) -> ValuesView[_VT_co]: ...
def __contains__(self, __o: object) -> bool: ...

Shouldn't the second overload simply be

@overload 
def get(self, __key: _KT, default: _T) -> _VT_co | _T: ... 

Why is there a covariant variable being used as a function input? Is there any case where this simplified signature would be incompatible with the current signature?

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