Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created April 3, 2024 20:01
Show Gist options
  • Save mypy-play/97023b90be45766db2844730e59d218c to your computer and use it in GitHub Desktop.
Save mypy-play/97023b90be45766db2844730e59d218c to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import overload, Any, Iterable, TypeVar, Union
from _typeshed import SupportsKeysAndGetItem
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")
class MyDict(dict[_KT, _VT]):
def __init__(self, *args: Any, **kwargs: Any):
...
@overload
def __ior__(self: MyDict[_KT, _VT], other: "SupportsKeysAndGetItem[_KT, _VT]", /) -> MyDict[_KT, _VT]:
...
@overload
def __ior__(self: MyDict[_KT, _VT], other: Iterable[tuple[_KT, _VT]], /) -> MyDict[_KT, _VT]:
...
@overload
def __ior__(self: MyDict[_KT, _VT], other: "SupportsKeysAndGetItem[_T1, _T2]", /) -> MyDict[Union[_KT, _T1], Union[_VT, _T2]]:
...
def __ior__(self, other, /):
self.update(other)
return self
@overload
def __or__(self: MyDict[_KT, _VT], other: dict[_KT, _VT], /) -> MyDict[_KT, _VT]:
...
@overload
def __or__(self: MyDict[_KT, _VT], other: dict[_T1, _T2], /) -> MyDict[Union[_KT, _T1], Union[_VT, _T2]]:
...
def __or__(self, other, /):
new = MyDict(self)
new.update(other)
return new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment