Skip to content

Improve MutableMapping.update's overloads to reflect relationship between key type and **kwargs #12456

Open
@cfriedland5

Description

@cfriedland5

The current overloads for MutableMapping.update() allow the use of keyword arguments even if the key type isn't str. I think it's possible to put a type hint on self so that keyword arguments only pass type checking when the key type is str, like this.

    # **kwargs aren't allowed for general _KT
    @overload
    def update(self, m: SupportsKeysAndGetItem[_KT, _VT], /) -> None: ...
    @overload
    def update(self, m: Iterable[tuple[_KT, _VT]], /) -> None: ...
    # **kwargs are allowed when _KT is str
    @overload
    def update(self: MutableMapping[str, _VT], m: SupportsKeysAndGetItem[_KT, _VT], /, **kwargs: _VT) -> None: ...
    @overload
    def update(self: MutableMapping[str, _VT], m: Iterable[tuple[_KT, _VT]], /, **kwargs: _VT) -> None: ...
    @overload
    def update(self: MutableMapping[str, _VT], **kwargs: _VT) -> None: ...

See the current overloads for MutableMapping.update here:

typeshed/stdlib/typing.pyi

Lines 749 to 754 in 3db7f01

@overload
def update(self, m: SupportsKeysAndGetItem[_KT, _VT], /, **kwargs: _VT) -> None: ...
@overload
def update(self, m: Iterable[tuple[_KT, _VT]], /, **kwargs: _VT) -> None: ...
@overload
def update(self, **kwargs: _VT) -> None: ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: improvementImprove/refactor existing annotations, other stubs issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions