[ty] Avoid enforcing __new__ with custom metaclasses#25180
Merged
Conversation
__new__ with custom metaclasses
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 89.36%. The percentage of expected errors that received a diagnostic held steady at 85.49%. The number of fully passing files held steady at 88/134. |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
sphinx
trio
flake8
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unused-ignore-comment |
2 | 0 | 0 |
| Total | 2 | 0 | 0 |
Raw diff:
django-stubs (https://github.com/typeddjango/django-stubs)
+ tests/assert_type/db/models/test_enums.py:244:75 warning[unused-ignore-comment] Unused `ty: ignore` directive
+ tests/assert_type/db/models/test_enums.py:309:47 warning[unused-ignore-comment] Unused `ty: ignore` directive
JelleZijlstra
approved these changes
May 16, 2026
thejchap
pushed a commit
to thejchap/ruff
that referenced
this pull request
May 23, 2026
## Summary
This PR makes enum member validation account for custom
`EnumMeta.__new__` methods. Prior to this change, we validated the raw
right-hand side of an enum member against inherited `_value_,`
`__new__,` or `__init__`. But that's too strict for Django’s
`IntegerChoices` pattern...
```python
from django.db import models
class Status(models.IntegerChoices):
GOOD = 1, "I like this"
```
Django’s custom metaclass strips the label out of the member value
before `IntEnum` construction, so the raw `(1,
"I like this")` tuple is _not_ the value that should be checked against
`int.__new__`.
We now detect a custom enum metaclass `__new__` and treat member values
as potentially transformed. In that case, we skip raw-RHS validation
against enum construction hooks, and `.value` / `._value_` falls back to
`Any` (unless the enum class itself declares an explicit `_value_`
annotation).
Pyright has similar behavior here:
https://github.com/microsoft/pyright/blob/63998f4d0720a86447f4c4a04716e34f3e703660/packages/pyright-internal/src/analyzer/enums.ts#L653-L660
But we do differ in this case:
```python
class Status(IntegerChoices):
_value_: int
GOOD = 1, "label"
```
Pyright exposes `Status.GOOD.value` as `int`, but still reports that the
raw tuple is not assignable to `int`. We expose `Status.GOOD.value` as
`int`, and don't report anything.
Closes astral-sh/ty#3468.
anishgirianish
pushed a commit
to anishgirianish/ruff
that referenced
this pull request
May 28, 2026
## Summary
This PR makes enum member validation account for custom
`EnumMeta.__new__` methods. Prior to this change, we validated the raw
right-hand side of an enum member against inherited `_value_,`
`__new__,` or `__init__`. But that's too strict for Django’s
`IntegerChoices` pattern...
```python
from django.db import models
class Status(models.IntegerChoices):
GOOD = 1, "I like this"
```
Django’s custom metaclass strips the label out of the member value
before `IntEnum` construction, so the raw `(1,
"I like this")` tuple is _not_ the value that should be checked against
`int.__new__`.
We now detect a custom enum metaclass `__new__` and treat member values
as potentially transformed. In that case, we skip raw-RHS validation
against enum construction hooks, and `.value` / `._value_` falls back to
`Any` (unless the enum class itself declares an explicit `_value_`
annotation).
Pyright has similar behavior here:
https://github.com/microsoft/pyright/blob/63998f4d0720a86447f4c4a04716e34f3e703660/packages/pyright-internal/src/analyzer/enums.ts#L653-L660
But we do differ in this case:
```python
class Status(IntegerChoices):
_value_: int
GOOD = 1, "label"
```
Pyright exposes `Status.GOOD.value` as `int`, but still reports that the
raw tuple is not assignable to `int`. We expose `Status.GOOD.value` as
`int`, and don't report anything.
Closes astral-sh/ty#3468.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes enum member validation account for custom
EnumMeta.__new__methods. Prior to this change, we validated the raw right-hand side of an enum member against inherited_value_,__new__,or__init__. But that's too strict for Django’sIntegerChoicespattern...Django’s custom metaclass strips the label out of the member value before
IntEnumconstruction, so the raw(1, "I like this")tuple is not the value that should be checked againstint.__new__.We now detect a custom enum metaclass
__new__and treat member values as potentially transformed. In that case, we skip raw-RHS validation against enum construction hooks, and.value/._value_falls back toAny(unless the enum class itself declares an explicit_value_annotation).Pyright has similar behavior here: https://github.com/microsoft/pyright/blob/63998f4d0720a86447f4c4a04716e34f3e703660/packages/pyright-internal/src/analyzer/enums.ts#L653-L660
But we do differ in this case:
Pyright exposes
Status.GOOD.valueasint, but still reports that the raw tuple is not assignable toint. We exposeStatus.GOOD.valueasint, and don't report anything.Closes astral-sh/ty#3468.