ruff
a7ab646e - [ty] Avoid enforcing `__new__` with custom metaclasses (#25180)

Commit
14 days ago
[ty] Avoid enforcing `__new__` with custom metaclasses (#25180) ## 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 https://github.com/astral-sh/ty/issues/3468.
Author
Parents
Loading