[ty] implement auto() for StrEnum (#20524)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary
see discussion here:
https://github.com/astral-sh/ty/issues/876#issuecomment-3310130167

https://docs.python.org/3/library/enum.html#enum.StrEnum

> Note Using
[auto](https://docs.python.org/3/library/enum.html#enum.auto) with
[StrEnum](https://docs.python.org/3/library/enum.html#enum.StrEnum)
results in the lower-cased member name as the value.

## Test Plan
- new mdtest
- also, added a test to assert the (already correct) behavior for
`IntEnum`

---------

Co-authored-by: David Peter <mail@david-peter.de>
This commit is contained in:
justin 2025-09-23 06:22:59 -04:00 committed by GitHub
parent 036f3616a1
commit ef4df34652
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 84 additions and 8 deletions

View file

@ -267,6 +267,11 @@ reveal_type(Color.red)
### Using `auto()`
```toml
[environment]
python-version = "3.11"
```
```py
from enum import Enum, auto
from ty_extensions import enum_members
@ -277,6 +282,50 @@ class Answer(Enum):
# revealed: tuple[Literal["YES"], Literal["NO"]]
reveal_type(enum_members(Answer))
reveal_type(Answer.YES.value) # revealed: Literal[1]
reveal_type(Answer.NO.value) # revealed: Literal[2]
```
Usages of `auto()` can be combined with manual value assignments:
```py
class Mixed(Enum):
MANUAL_1 = -1
AUTO_1 = auto()
MANUAL_2 = -2
AUTO_2 = auto()
reveal_type(Mixed.MANUAL_1.value) # revealed: Literal[-1]
reveal_type(Mixed.AUTO_1.value) # revealed: Literal[1]
reveal_type(Mixed.MANUAL_2.value) # revealed: Literal[-2]
reveal_type(Mixed.AUTO_2.value) # revealed: Literal[2]
```
When using `auto()` with `StrEnum`, the value is the lowercase name of the member:
```py
from enum import StrEnum, auto
class Answer(StrEnum):
YES = auto()
NO = auto()
reveal_type(Answer.YES.value) # revealed: Literal["yes"]
reveal_type(Answer.NO.value) # revealed: Literal["no"]
```
Using `auto()` with `IntEnum` also works as expected:
```py
from enum import IntEnum, auto
class Answer(IntEnum):
YES = auto()
NO = auto()
reveal_type(Answer.YES.value) # revealed: Literal[1]
reveal_type(Answer.NO.value) # revealed: Literal[2]
```
Combining aliases with `auto()`: