Skip to content

Django models.TextChoices is not-indexable? #1048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
junha6316 opened this issue Nov 12, 2021 · 3 comments
Open

Django models.TextChoices is not-indexable? #1048

junha6316 opened this issue Nov 12, 2021 · 3 comments
Labels
bug cat: stubs and 3p type stubs and third-party types

Comments

@junha6316
Copy link

I made project with django and I applied type-hinting by using pytype. But pytype gives me type-error like this

type_test.py", line 11, in <module>: class ReportCategoryChoices is not indexable [not-indexable]
  ('ReportCategoryChoices' does not subclass Generic)

For more details, see https://google.github.io/pytype/errors.html#not-indexable
ninja: build stopped: subcommand failed.

type_test.py

category = ReportCategoryChoices['A']

and here is the ReportCategoryClass

class ReportCategoryChoices(models.TextChoices):
     A = "A", _("a")
     B = "B", _("b")
     C = "C", _("c")

# type of ReportCategoryChoices: <class 'django.db.models.enums.ChoicesMeta'>

in django ReportCategoryClass is Indexable. why did pytype say ReportCategory is not indexable?

@rchen152 rchen152 added bug cat: stubs and 3p type stubs and third-party types labels Nov 19, 2021
@rchen152
Copy link
Contributor

Thanks for the report, and sorry for taking so long to respond - I somehow missed the notification for this. This looks like another consequence of not supporting #151 - pytype doesn't have stubs for django, so types are treated as Any, and class ReportCategoryChoices(Any) isn't seen as indexable.

I'm going to leave this open as a separate issue, since (1) django is a widely used library and we should verify that this works once pytype has type information available and (2) there are possibly other ways to resolve this (such as allowing anything that inherits from Any to be treated as indexable).

@junha6316
Copy link
Author

junha6316 commented Nov 22, 2021

@rchen152
Thank you for replying the issue I reported! I left some code for someone who wants to solve this problem temporarily.
redeclare explicitly your TextChoices type is Choices

from django.db.models.enums import Choices

class ReportCategoryChoices(models.TextChoices):
     A = "A", _("a")
     B = "B", _("b")
     C = "C", _("c")

ReportCategoryChoices: Type[Choices] = ReportCategoryChoices

@smkent
Copy link

smkent commented Nov 9, 2022

I have also encountered this issue. Providing enum.Enum as a redundant base class appears to satisfy pytype.

$ pytype --version
2022.08.23

bad.py:

from django.db.models import TextChoices


class Choices(TextChoices):
    ONE = 1
    TWO = 2


print(Choices["ONE"])
$ pytype bad.py
Computing dependencies
Analyzing 1 sources with 0 local dependencies
ninja: Entering directory `.pytype'
[1/1] check bad
FAILED: [redacted]/.pytype/pyi/bad.pyi
/usr/local/bin/python -m pytype.single --imports_info [redacted]/.pytype/imports/bad.imports --module-name bad --platform linux -V 3.10 -o [redacted]/.pytype/pyi/bad.pyi --analyze-annotated --nofail --quick [redacted]/bad.py
File "[redacted]/bad.py", line 9, in <module>: class Choices is not indexable [not-indexable]
  ('Choices' does not subclass Generic)

For more details, see https://google.github.io/pytype/errors.html#not-indexable
ninja: build stopped: subcommand failed.
Leaving directory '.pytype'

good.py:

from enum import Enum

from django.db.models import TextChoices


class Choices(TextChoices, Enum):
    ONE = 1
    TWO = 2


print(Choices["ONE"])
$ pytype good.py
Computing dependencies
Analyzing 1 sources with 0 local dependencies
ninja: Entering directory `.pytype'
ninja: no work to do.
Leaving directory '.pytype'
Success: no errors found

pablosnt added a commit to pablosnt/rekono that referenced this issue May 15, 2025
pablosnt added a commit to pablosnt/rekono that referenced this issue May 15, 2025
* Replace Optional typing by | None

* Replace get_project_field method by project_field attribute

* Allow the execution of integrations per finding and per execution

* Allow BaseInput parsing in a declarative way

* Fix flake8 and pytype issues

* Fix create_finding typing

* Fix typing

* Workaround to fix issues reported by pytype on TextChoices

* Fix typing of Gitleaks executor on Gitleaks parser

* Workaround to fix issues reported by pytype on TextChoices

* Fix issues on cmseek and gitleaks executor and/or parsers

* Disable pytype when accessing dynamic attributes of CONFIG class

* Try to fix pytype issue when calling lower over an string from a dict

* Disable pytype when accessing dynamic attributes of CONFIG class

* Fix typing issue

* Fix typing issue on load_report_as_json method

* Fix some typing issues

* Fix return type from load_report_as_json

* Ignore pytype attribute error

* Fix method signature

* Fix return type

* Remove mypy ignores

* Remove flake8 ignores

* Fix pytype and flake8 issues

* Fix typing

* Fix typing

* Fix typing

* Fix typing

* Fix typing

* Fix typing

* Fix typing

* exclude unit tests from strict typing check

* fix pytype execution on cicd

* fix typing in telegram BaseMixin

* fix typing issues

* fix typing issues

* remove tests exclusion

* ignore pytype false positives in tests

* fix typing issues

* always return a model for _get_model method

* fix some issues on unit tests

* test if unit tests work without extending Enum in all the Django choices (google/pytype#1048)

* try another approach to avoid error google/pytype#1048 while not breaking the code

* fix unit tests for defectdojo integration

* fix unit tests for tool executor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cat: stubs and 3p type stubs and third-party types
Projects
None yet
Development

No branches or pull requests

3 participants