FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Type not narrowed correctly for `if X in collection` · Issue #3229 · python/mypy · GitHub

Repository navigation

Type not narrowed correctly for if X in collection #3229

Description

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

Activity

  1. ilevkivskyi commented on Apr 24, 2017

    Member

    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. elazarg commented on Apr 25, 2017

    Contributor

    Related #2357 #1203

  3. frexvahi commented on Aug 28, 2019

    Author

    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.

  4. added a commit that references this issue on Jun 7, 2024
  5. added a commit that references this issue on Jul 24, 2024
    ed0cd4a
  6. reopened this on Oct 2, 2024
  7. JukkaL commented on Oct 2, 2024

    Collaborator

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

  8. Jordandev678 commented on Oct 9, 2024

    Contributor

    @JukkaL want me to pick this up?

  9. JukkaL commented on Oct 10, 2024

    Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL