Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tinche opened this issue Nov 26, 2023 · 2 comments
Closed

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

Tinche opened this issue Nov 26, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@Tinche
Copy link
Contributor

Tinche commented Nov 26, 2023

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.

@Tinche Tinche added the bug mypy got something wrong label Nov 26, 2023
@AlexWaygood
Copy link
Member

Duplicate of #16489.

The behaviour change is as a result of this typeshed change: python/typeshed#10679. The new behaviour is more accurate, and the change was made to fix a false negative that was reported by a user to typeshed: python/typeshed#10678.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Nov 26, 2023
@Tinche
Copy link
Contributor Author

Tinche commented Nov 26, 2023

Cool, thanks. Wish we had a frozendict!

I tried searching for duplicates but I couldn't find any. GitHub issue search could be better :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants