Skip to content

Commit 73dafc6

Browse files
authored
Improve types for django.db.models.enums.* (#1819)
1 parent fb5f251 commit 73dafc6

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

django-stubs/db/models/enums.pyi

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,62 @@
11
import enum
22
import sys
3-
from typing import Any
3+
from typing import Any, TypeVar
4+
5+
from typing_extensions import Self
6+
7+
_Self = TypeVar("_Self")
48

59
if sys.version_info >= (3, 11):
6-
enum_property = enum.property
10+
_enum_property = enum.property
711
else:
8-
enum_property = property
12+
_enum_property = property
913

1014
class ChoicesMeta(enum.EnumMeta):
11-
names: list[str]
12-
choices: list[tuple[Any, str]]
13-
labels: list[str]
14-
values: list[Any]
15+
# There's a contradiction between mypy and PYI019 regarding metaclasses. Where mypy
16+
# disallows 'typing_extensions.Self' on metaclasses, while PYI019 try to enforce
17+
# 'typing_extensions.Self' for '__new__' methods.. We've chosen to ignore the
18+
# linter and trust mypy.
19+
def __new__(
20+
metacls: type[_Self], classname: str, bases: tuple[type, ...], classdict: enum._EnumDict, **kwds: Any
21+
) -> _Self: ... # noqa: PYI019
1522
def __contains__(self, member: Any) -> bool: ...
23+
@property
24+
def names(self) -> list[str]: ...
25+
@property
26+
def choices(self) -> list[tuple[Any, str]]: ...
27+
@property
28+
def labels(self) -> list[str]: ...
29+
@property
30+
def values(self) -> list[Any]: ...
1631

1732
class Choices(enum.Enum, metaclass=ChoicesMeta):
1833
@property
1934
def label(self) -> str: ...
20-
@enum_property
35+
@_enum_property
2136
def value(self) -> Any: ...
2237
@property
2338
def do_not_call_in_templates(self) -> bool: ...
2439

25-
# fake
40+
# fake, to keep simulate class properties
2641
class _IntegerChoicesMeta(ChoicesMeta):
27-
names: list[str]
28-
choices: list[tuple[int, str]]
29-
labels: list[str]
30-
values: list[int]
42+
@property
43+
def choices(self) -> list[tuple[int, str]]: ...
44+
@property
45+
def values(self) -> list[int]: ...
3146

3247
class IntegerChoices(int, Choices, metaclass=_IntegerChoicesMeta):
33-
@enum_property
48+
def __new__(cls, value: int) -> Self: ...
49+
@_enum_property
3450
def value(self) -> int: ...
3551

36-
# fake
52+
# fake, to keep simulate class properties
3753
class _TextChoicesMeta(ChoicesMeta):
38-
names: list[str]
39-
choices: list[tuple[str, str]]
40-
labels: list[str]
41-
values: list[str]
54+
@property
55+
def choices(self) -> list[tuple[str, str]]: ...
56+
@property
57+
def values(self) -> list[str]: ...
4258

4359
class TextChoices(str, Choices, metaclass=_TextChoicesMeta):
44-
@enum_property
60+
def __new__(cls, value: str) -> Self: ...
61+
@_enum_property
4562
def value(self) -> str: ...

scripts/stubtest/allowlist_todo.txt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ django.contrib.gis.db.models.ImageField.attr_class
436436
django.contrib.gis.db.models.ImageField.contribute_to_class
437437
django.contrib.gis.db.models.ImageField.descriptor_class
438438
django.contrib.gis.db.models.ImageField.formfield
439-
django.contrib.gis.db.models.IntegerChoices.__new__
440439
django.contrib.gis.db.models.IntegerField.class_lookups
441440
django.contrib.gis.db.models.IntegerField.formfield
442441
django.contrib.gis.db.models.IntegerField.validators
@@ -512,7 +511,6 @@ django.contrib.gis.db.models.Subquery.empty_result_set_value
512511
django.contrib.gis.db.models.Subquery.external_aliases
513512
django.contrib.gis.db.models.Subquery.get_external_cols
514513
django.contrib.gis.db.models.Subquery.subquery
515-
django.contrib.gis.db.models.TextChoices.__new__
516514
django.contrib.gis.db.models.TextField.formfield
517515
django.contrib.gis.db.models.TimeField.class_lookups
518516
django.contrib.gis.db.models.TimeField.formfield
@@ -1149,7 +1147,6 @@ django.db.models.ImageField.attr_class
11491147
django.db.models.ImageField.contribute_to_class
11501148
django.db.models.ImageField.descriptor_class
11511149
django.db.models.ImageField.formfield
1152-
django.db.models.IntegerChoices.__new__
11531150
django.db.models.IntegerField.class_lookups
11541151
django.db.models.IntegerField.formfield
11551152
django.db.models.IntegerField.validators
@@ -1224,7 +1221,6 @@ django.db.models.Subquery.empty_result_set_value
12241221
django.db.models.Subquery.external_aliases
12251222
django.db.models.Subquery.get_external_cols
12261223
django.db.models.Subquery.subquery
1227-
django.db.models.TextChoices.__new__
12281224
django.db.models.TextField.formfield
12291225
django.db.models.TimeField.class_lookups
12301226
django.db.models.TimeField.formfield
@@ -1268,14 +1264,6 @@ django.db.models.constraints.UniqueConstraint.__init__
12681264
django.db.models.constraints.UniqueConstraint.contains_expressions
12691265
django.db.models.constraints.UniqueConstraint.validate
12701266
django.db.models.deletion.Collector.__init__
1271-
django.db.models.enums.ChoicesMeta.__new__
1272-
django.db.models.enums.ChoicesMeta.choices
1273-
django.db.models.enums.ChoicesMeta.labels
1274-
django.db.models.enums.ChoicesMeta.names
1275-
django.db.models.enums.ChoicesMeta.values
1276-
django.db.models.enums.IntegerChoices.__new__
1277-
django.db.models.enums.TextChoices.__new__
1278-
django.db.models.enums.enum_property
12791267
django.db.models.expressions.BaseExpression.contains_aggregate
12801268
django.db.models.expressions.BaseExpression.contains_column_references
12811269
django.db.models.expressions.BaseExpression.contains_over_clause

0 commit comments

Comments
 (0)