Skip to content

Possible regression in 1.7 with dict unions (PEP 584) #16572

@Tinche

Description

@Tinche

There's a change for sure, but I'm having trouble figuring out if the old or new behavior is correct.

I skimmed the Mypy changelog but didn't find much about this.

Quick reproducer:

from collections.abc import Mapping

a: Mapping[str, str] = {}

{"a": "a"} | a

On 1.6: no error.
On 1.7:

a05.py:5: error: No overload variant of "__or__" of "dict" matches argument type "Mapping[str, str]"  [operator]
a05.py:5: note: Possible overload variants:
a05.py:5: note:     def __or__(self, dict[str, str], /) -> dict[str, str]
a05.py:5: note:     def [_T1, _T2] __or__(self, dict[_T1, _T2], /) -> dict[str | _T1, str | _T2]

The PEP gives the following pseudocode for __or__:

def __or__(self, other):
    if not isinstance(other, dict):
        return NotImplemented
    new = dict(self)
    new.update(other)
    return new

So I guess the new behavior might be more correct? It feels less useful though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions