Skip to content

Non overlapping inclusion check for literal types #11313

Open

Description

@sami-evangelista

Right now mypy spots an error on the first if condition but it would be great if it could also complain on the second one which seems a better expression of the first test:

from typing import Literal

literal_t = Literal['a', 'b', 'c']
x: literal_t = 'b'
if x == 'a' or x == 'd': ... 
if x in ('a', 'd'): ...

mypy version:

$ mypy --version
mypy 0.920+dev.bbea3b56297d3d966b9a98900c218df6616fec85

Activity

  1. sobolevn commented on Oct 12, 2021

    @sobolevn
    Member

    in check is not support right now, as far as I remember. It could be a cool feature. And quite easy one.
    We can just translate x in ('a', 'b') expression into x == 'a' or x == 'b'. And everything should work the same way.

    I will look into it.

  2. sobolevn commented on Nov 9, 2021

    @sobolevn
    Member

    Turned out it is not as easy. Simple "translate" did not work. Several reasons:

    1. Because of how type narrowing is done in mypy
    2. Because in is not identical to two ==. There might be __contains__ defined, but not __eq__. Or they might be incompatible
  3. intgr commented on Aug 19, 2022

    @intgr
    Contributor

    This issue title is misleading, should be renamed to mention in operator, nothing to do with literal types.

  4. sterliakov commented on Dec 20, 2024

    @sterliakov
    Collaborator

    Similar/duplicate of #3229?

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