Skip to content
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

Narrowing dict | Literal does not work in Mypy 1.5.0 #15860

Closed
zroug opened this issue Aug 12, 2023 · 4 comments
Closed

Narrowing dict | Literal does not work in Mypy 1.5.0 #15860

zroug opened this issue Aug 12, 2023 · 4 comments
Labels
bug mypy got something wrong

Comments

@zroug
Copy link

zroug commented Aug 12, 2023

Bug Report

When using Mypy 1.5.0, expressions like dict | Literal[""] don't narrow correctly:

To Reproduce

from typing import Literal

x: Literal[""] | dict
y: Literal[""] | list

if x == "":
    reveal_type(x)

if y == "":
    reveal_type(y)

Mypy 1.4.1
Mypy 1.5.0

Expected Behavior

Both revealed types should be Literal[""]. (As in Mypy 1.4.1.)

Actual Behavior

Mypy 1.5.0 does not narrow the revealed type for x. y gets narrowed correctly.

Your Environment

See Playground links.

@zroug zroug added the bug mypy got something wrong label Aug 12, 2023
@atugushev
Copy link
Contributor

It appears to be fixed in the master branch.

@zroug
Copy link
Author

zroug commented Aug 15, 2023

Oh great, when I created the issue it wasn't fixed on master yet, but now it seems to be fixed indeed. I don't know if you want to keep this issue open for visibility until this is released or not, so feel free to close it as you see fit.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Aug 16, 2023

Looks like the original regression was in a typeshed sync 59a6c5e. Specifically, the addition of __eq__ to Mapping in python/typeshed#10297

diff --git a/mypy/typeshed/stdlib/typing.pyi b/mypy/typeshed/stdlib/typing.pyi
index db042dc44..08d7da5e8 100644
--- a/mypy/typeshed/stdlib/typing.pyi
+++ b/mypy/typeshed/stdlib/typing.pyi
@@ -631,6 +631,7 @@ class Mapping(Collection[_KT], Generic[_KT, _VT_co]):
     def keys(self) -> KeysView[_KT]: ...
     def values(self) -> ValuesView[_VT_co]: ...
     def __contains__(self, __key: object) -> bool: ...
+    def __eq__(self, __other: object) -> bool: ...

This recently got fixed by #15837, but perhaps unintentionally

@ilevkivskyi
Copy link
Member

I don't think we need to keep this open. There is little chance #15837 can be included in 1.5.1 (it is quite a major change), so you will need to wait for 1.6.0

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

4 participants