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 — Type not narrowed correctly for `if X in collection` · Issue #3229 · python/mypy

Type not narrowed correctly for if X in collection #3229

New issue
Copy link
New issue
Copy link
Closed
#20602
#17344
Closed
Type not narrowed correctly for if X in collection#3229
#20602
#17344
Copy link
Labels
featurepriority-1-normaltopic-type-narrowingConditional type narrowing / binder

Description

@frexvahi
frexvahi
opened on Apr 24, 2017
Issue body actions
def foobar(foo: Union[str, float]):
    if foo in ['fizz', 'buzz']:
        reveal_type(foo)
    else:
        pass

The revealed type is Union[builtins.str, builtins.float] but should be builtins.str

Reacted by Nicholas Larus-Stone, Anton Agestam, Allison Karlitskaya, wyattscarpenter, darkdragon-001 and Marc Bresson

Activity

  1. ilevkivskyi commented on Apr 24, 2017

    @ilevkivskyi
    ilevkivskyi
    on Apr 24, 2017
    Member
    More actions

    Than you for reporting this! I have also encountered situations where this could be useful. It is not super easy to implement, but I think this is a reasonable feature to support (especially for invariant collections, where the type is known precisely).

    As a workaround, you could add assert isinstance(foo, str) in the if-branch, mypy will recognize this.

  2. ilevkivskyi
    added
    feature
    priority-1-normal
    on Apr 24, 2017
  3. elazarg commented on Apr 25, 2017

    @elazarg
    elazarg
    on Apr 25, 2017
    Contributor
    More actions

    Related #2357 #1203

  4. frexvahi commented on Aug 28, 2019

    @frexvahi
    frexvahi
    on Aug 28, 2019
    Author
    More actions

    I just realised: there might also have to be a special case for testing if a float is in a set of ints, or any other situation where a == b can be true even though type(a) == type(b) is not.

  5. AlexWaygood
    added
    topic-type-narrowingConditional type narrowing / binder
    on Aug 29, 2022
  6. Jordandev678
    added a commit that references this issue on Jun 7, 2024

    Add test case from python#3229

    3783f1d
  7. hauntsaninja
    closed this as completedin #17344on Jul 24, 2024
  8. hauntsaninja
    added a commit that references this issue on Jul 24, 2024

    Narrow based on collection containment (#17344)

    This issue will close once commit ed0cd4a is merged into the 'master' branch.
    ed0cd4a
  9. JukkaL
    reopened this on Oct 2, 2024
  10. JukkaL commented on Oct 2, 2024

    @JukkaL
    JukkaL
    on Oct 2, 2024
    Collaborator
    More actions

    Reopening, since the original implementation was reverted due to some issues (#17864).

  11. Jordandev678 commented on Oct 9, 2024

    @Jordandev678
    Jordandev678
    on Oct 9, 2024
    Contributor
    More actions

    @JukkaL want me to pick this up?

  12. JukkaL commented on Oct 10, 2024

    @JukkaL
    JukkaL
    on Oct 10, 2024
    Collaborator
    More actions

    @Jordandev678 Sure! The original PR can be resubmitted once the issues have been addressed.

    Reacted by Jordandev678
  13. hauntsaninja
    closed this as completedin #20602on Jan 22, 2026
  14. ilevkivskyi
    added a commit that references this issue on Jan 22, 2026

    Narrow types based on collection containment (python#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

    featurepriority-1-normaltopic-type-narrowingConditional type narrowing / binder

    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.