Skip to content

Dictionary accepting Mapping in a merge operation #11264

Description

@jzazo

Hi. I would like to propose that a dictionary accepts a mapping to the or operation. Something like:

classdict(MutableMapping[_KT, _VT]):
...
@overloaddef__or__(self, __value: Mapping[_KT, _VT]) ->dict[_KT, _VT]: ...
@overloaddef__or__(self, __value: Mapping[_T1, _T2]) ->dict[_KT|_T1, _VT|_T2]: ...

It would be useful in scenarios like the following:

fromcollections.abcimportMappingfromtypingimportcasta=cast(Mapping[str, int], {"a": 1})
{"b": 2} |a

At this point, pyright complains with # error: Operator "|" not supported for types "dict[str, int]" and "Mapping[str, int]".
I referenced this problem in this issue. Thanks.

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