Skip to content

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

Closed

Description

@zroug

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.

Activity

  1. atugushev commented on Aug 15, 2023

    @atugushev
    Contributor

    It appears to be fixed in the master branch.

  2. zroug commented on Aug 15, 2023

    @zroug
    Author

    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.

  3. hauntsaninja commented on Aug 16, 2023

    @hauntsaninja
    Collaborator

    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

  4. ilevkivskyi commented on Aug 16, 2023

    @ilevkivskyi
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions