GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Narrowing inconsistency between == and "in" · Issue #17864 · python/mypy

Narrowing inconsistency between == and "in" #17864

New issue
Copy link
New issue
Copy link
Closed
#17865
Closed
Narrowing inconsistency between == and "in"#17864
#17865
Copy link
Labels
bugmypy got something wrong

Description

@JukkaL
JukkaL
opened on Oct 2, 2024
Issue body actions

Type narrowing behaves inconsistently:

def f(x: str | int) -> None:
    if x == "x":
        reveal_type(x)  # str | int
    if x in ["x"]:
        reveal_type(x)  # str

Both of these should infer the same type. This is a regression that was introduced in #17344 (cc @Jordandev678).

Also, unlike with ==, narrowing using "in" doesn't consider the possibility of a custom __eq__ method, so it can infer the wrong type:

class C:
    def __init__(self, x: int) -> None:
        self.x = x

    def __eq__(self, other: object) -> bool:
        if isinstance(other, C) and other.x == self.x:
            return True
        return NotImplemented

class D(C):
    pass

def f(x: C) -> None:
    if x in [D(5)]:
        print(type(x))  # C
        reveal_type(x)  # D

f(C(5))

I'm going to revert to #17344 since fixing the logic may be non-trivial, and there is also another related bug (#17841).

The original PR can be submitted again with a fix that uses the same logic we use for == narrowing. If there is desire to support more narrowing use cases, this should be addressed in a separate issue/PR, and both == and "in" narrowing should remain consistent.

Activity

  1. JukkaL
    added
    bugmypy got something wrong
    on Oct 2, 2024
  2. JukkaL
    added a commit that references this issue on Oct 2, 2024

    Revert "Narrow based on collection containment (#17344)"

    4aade6a
  3. JukkaL
    closed this as completedin #17865on Oct 2, 2024
  4. JukkaL
    closed this as completedin 329e38eon Oct 2, 2024
  5. pull
    added a commit that references this issue on Oct 2, 2024

    Revert "Narrow based on collection containment (python#17344)" (pytho…

    329e38e
  6. hauntsaninja
    added a commit that references this issue on Oct 5, 2024

    Add regression test cases (#17869)

    4e4826f
  7. hauntsaninja
    added 2 commits that reference this issue on Jan 16, 2026

    Rework narrowing logic for equality and identity (#20492)

    ad30be8

    Narrow types based on collection containment (#20602)

    b91f53c
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

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.