django-stubs: `TextChoices` subclasses declared inside a class cause "Unable to resolve type of choices property" error

Bug report

What’s wrong

Having an enum that extends TextChoices declared inside another class (e.g. a model), results in the following error when running mypy:

file.py: error: Unable to resolve type of choices property  [misc]

Here’s an example of such definition:


class MyModel:
    class MyEnum(models.TextChoices):
        foo = "foo", "Foo"
        bar = "bar", "Bar"

a = MyEnum.foo

Running mypy against that:

file.py:9: error: Unable to resolve type of choices property  [misc]

How is that should be

No error should happen.

System information

  • OS:
  • python version: 3.12.9
  • django version: 5.1.8
  • mypy version: 1.13.0
  • django-stubs version: 5.2.0
  • django-stubs-ext version: 5.2.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 18 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Sorry, my previous example was illustrative; here’s an actual repro:

~/e/django-stubs> git diff
diff --git a/tests/assert_type/db/models/test_enums.py b/tests/assert_type/db/models/test_enums.py
index 9634a960..9bf2bd98 100644
--- a/tests/assert_type/db/models/test_enums.py
+++ b/tests/assert_type/db/models/test_enums.py
@@ -92,6 +92,10 @@ class VoidChoices(BaseEmptyChoices):
     CHASM = enum.auto()


+class ChoiceWrapper:
+    Suit = Suit
+
+
 # Choice type that has been aliased to test that the plugin resolves types correctly.
 CompassPoint = imported.Direction

@@ -128,6 +132,7 @@ assert_type(Suit.names, list[str])
 assert_type(Suit.labels, list[_StrOrPromise])
 assert_type(Suit.values, list[int])
 assert_type(Suit.choices, list[tuple[int, _StrOrPromise]])
+assert_type(ChoiceWrapper.Suit.choices, list[tuple[int, _StrOrPromise]])
 assert_type(Suit.CLUB, Literal[Suit.CLUB])
 assert_type(Suit.CLUB.name, Literal["CLUB"])
 assert_type(Suit.CLUB.label, _StrOrPromise)

~/e/django-stubs> uv run mypy --strict tests/assert_type/db/models/test_enums.py
tests/assert_type/db/models/test_enums.py:135: error: Expression is of type "Any", not "List[Tuple[int, Union[str, _StrPromise]]]"  [assert-type]
tests/assert_type/db/models/test_enums.py:135: error: Unable to resolve type of choices property  [misc]
Found 2 errors in 1 file (checked 1 source file)

Thanks

@youtux Could you try installing from the master branch? I think this should be fixed by #2646.