Skip to content

Mapping.get overload has covariant type as argument #9555

Closed as not planned
Closed as not planned

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?

Activity

  1. srittau commented on Jan 17, 2023

    @srittau
    Collaborator

    This was previously tried in #2810, but then reverted in #2817, which has extensive discussion about this.

  2. randolf-scholz commented on Jan 17, 2023

    @randolf-scholz
    ContributorAuthor

    @srittau Looking at the example given in the PR

    m: Mapping[str, List[str]]
    x = m.get('x', [])
    reveal_type(x)  # list[<nothing>] instead of list[str]

    it seems that the issue is that there is a reasonable expectation that for Mapping.get, if _T is a subtype of _VT_co, then the return value should be interpreted as _VT_co. It seems that to do this correctly, some kind of conditional @overload would be required

    @overload  # if _T ≤ _VT_co
    def get(self, __key: _KT, default: _T) -> _VT_co: ... 
    @overload  # else
    def get(self, __key: _KT, default: _T) -> _T: ... 
  3. srittau commented on Jan 17, 2023

    @srittau
    Collaborator

    @randolf-scholz If you want to write an exploratory PR that would certainly be welcome. We have a much better test infrastructure nowadays that allows us to judge the impact of any changes.

  4. randolf-scholz commented on Jan 17, 2023

    @randolf-scholz
    ContributorAuthor

    @srittau I think with the current typing system it's unfortunately impossible. One would need something like TypeScript's Conditional Types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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