Skip to content

Unsupported operand types depending on number of enum.Flag values #9642

@astrojuanlu

Description

@astrojuanlu

Bug Report

Depending on the number of enum.Flag possible values, I get a MyPy "Unsupported operand types" error.

To Reproduce

$ cat mypy_flags_simple.py 
from enum import Flag, auto


class MyStatus(Flag):
    A = auto()
    B = auto()
    C = auto()  # If commented out, no error!


def check_status(s: MyStatus) -> str:
    if s is MyStatus.A:
        return "A"
    elif s & MyStatus.B:
        return "B"
    else:
        return "Other"


if __name__ == "__main__":
    check_status(MyStatus.B)

Expected Behavior

Not give an error.

Actual Behavior

$ mypy mypy_flags_simple.py --pretty --show-error-codes
mypy_flags_simple.py:13: error: Unsupported operand types for & ("Literal[MyStatus.C]" and "Literal[MyStatus.B]")  [operator]
        elif s & MyStatus.B:
                 ^
mypy_flags_simple.py:13: note: Left operand is of type "Union[Literal[MyStatus.B], Literal[MyStatus.C]]"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: --pretty --show-error-codes
  • Mypy configuration options from mypy.ini (and other config files): (None)
  • Python version used: 3.8.5
  • Operating system and version: Linux Mint 20

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions