Skip to content

itertools.product called on a Flag enum is inferred as object rather than class name #18684

Description

@jatkinson1000

Bug Report

When defining a class based on Flag from the enum package I would expect items of the class to be of the type of the class when I iterate over them using itertools functions.
However, instead mypy raises an error that they are instead of type object.

To Reproduce

See playground link here: https://mypy-play.net/?mypy=latest&python=3.12&gist=5dc24e94f25872e5c5d8ac032c4d8309

Expand for code in playground to reproduce.
from enum import Flag, auto
import itertools


class Test_Flag_1(Flag):
    TF1a = auto()
    TF1b = auto()
    TF1c = auto()

class Test_Flag_2(Flag):
    TF2a = auto()
    TF2b = auto()
    TF2c = auto()

def test_func_1(flagitem: Test_Flag_1) -> None:
    print(flagitem.name)

def test_func_2(flagitem: Test_Flag_2) -> None:
    print(flagitem.name)


if __name__ == "__main__":
    for flagitem1, flagitem2 in itertools.product(
        Test_Flag_1, Test_Flag_2
    ):
        test_func_1(flagitem1)
        test_func_2(flagitem2)

Expected Behavior

I would expect this to pass fine through mypy.

Actual Behavior

Instead items of the Test_Flag_1 or Test_Flag_2 subclasses of Flag when iterated over using itertools.product are picked up by mypy as type object instead of type Test_Flag_1 or Test_Flag_2.

(venv) [user@machine ~]$ mypy mypy_minimal.py
mypy_minimal.py:26: error: Argument 1 to "test_func_1" has incompatible type "object"; expected "Test_Flag_1"  [arg-type]
mypy_minimal.py:27: error: Argument 1 to "test_func_2" has incompatible type "object"; expected "Test_Flag_2"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15.0 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13.1 (also reproduced with 3.12 in the playground link)

Full pip list:

Package           Version
----------------- -------
mypy              1.15.0
mypy-extensions   1.0.0
typing-extensions 4.12.2

Activity

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