Skip to content

Dictionary accepting Mapping in a merge operation #11264

Closed as not planned
Closed as not planned
@jzazo

Description

@jzazo

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

class dict(MutableMapping[_KT, _VT]):
    ...
        @overload
        def __or__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ...
        @overload
        def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ...

It would be useful in scenarios like the following:

from collections.abc import Mapping
from typing import cast

a = 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