typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

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

Open cfriedland5 opened this issue 1 year ago • 0 comments

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: https://github.com/python/typeshed/blob/3db7f01a31cb60e436ac5e0cc00e10b9a8a2cfe2/stdlib/typing.pyi#L749-L754

cfriedland5 avatar Jul 27 '24 21:07 cfriedland5