Skip to content

[ty] Support for custom _generate_next_value_ in enums#25196

Merged
charliermarsh merged 3 commits into
astral-sh:mainfrom
thejchap:thejchap/gennextvalue
May 17, 2026
Merged

[ty] Support for custom _generate_next_value_ in enums#25196
charliermarsh merged 3 commits into
astral-sh:mainfrom
thejchap:thejchap/gennextvalue

Conversation

@thejchap
Copy link
Copy Markdown
Contributor

@thejchap thejchap commented May 16, 2026

Summary

astral-sh/ty#876

This PR adds support for custom _generate_next_value_ methods on enums.

When set, this return type will take precedence over inferred auto() member types unless an explicit _value_ annotation is set.

Falls back to Any if custom hooks (like __init__) are detected.

References:

Test Plan

New mdtests

@astral-sh-bot astral-sh-bot Bot requested a review from charliermarsh May 16, 2026 07:12
@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label May 16, 2026
@thejchap thejchap changed the title [ty] Support for custom _generate_next_value_ in enums [ty] Support for custom _generate_next_value_ in enums May 16, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 16, 2026

Typing conformance results

No changes detected ✅

Current numbers
The 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.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 16, 2026

Memory usage report

Summary

Project Old New Diff Outcome
prefect 685.54MB 685.59MB +0.01% (46.76kB)
sphinx 256.18MB 256.19MB +0.01% (13.22kB)
trio 115.59MB 115.59MB +0.00% (3.84kB)
flake8 47.42MB 47.42MB +0.00% (1.16kB)

Significant changes

Click to expand detailed breakdown

prefect

Name Old New Diff Outcome
enum_metadata 2.88MB 2.92MB +1.59% (46.76kB)

sphinx

Name Old New Diff Outcome
enum_metadata 728.48kB 741.70kB +1.81% (13.22kB)

trio

Name Old New Diff Outcome
enum_metadata 240.12kB 243.96kB +1.60% (3.84kB)

flake8

Name Old New Diff Outcome
enum_metadata 67.61kB 68.77kB +1.72% (1.16kB)

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 89ca803ca6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/ty_python_semantic/src/types/enums.rs Outdated
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 16, 2026

ecosystem-analyzer results

No diagnostic changes detected ✅

Large timing changes:

Project Old Time New Time Change
pandas-stubs 9.73s 14.72s +51%

Full report with detailed diff (timing results)

@thejchap thejchap force-pushed the thejchap/gennextvalue branch from 89ca803 to 3f8aeba Compare May 16, 2026 07:20
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3f8aeba876

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/ty_python_semantic/src/types/enums.rs Outdated
@charliermarsh charliermarsh force-pushed the thejchap/gennextvalue branch from e756d3a to b12a65b Compare May 17, 2026 16:50
@charliermarsh charliermarsh merged commit 3fcc982 into astral-sh:main May 17, 2026
59 checks passed
charliermarsh added a commit that referenced this pull request May 17, 2026
…25210)

## Summary

One oversight in my changes to
#25196: if a custom `__new__` is
defined, we should treat the alias value as `Any`, rather than relying
on `_generate_next_value_`. This matches Pyright, for example.

E.g., given:

```python
class E(Enum):
    @staticmethod
    def _generate_next_value_(...) -> Literal["x"]:
        return "x"

    def __new__(cls, value: str):
        obj = object.__new__(cls)
        obj._value_ = object()
        return obj

    A = auto()
    B = auto()
```

Prior to this change, we treated `B` as an alias of `A`; but if
`__new__` is defined, we don't attempt to detect the alias.
thejchap added a commit to thejchap/ruff that referenced this pull request May 23, 2026
…5196)

## Summary

astral-sh/ty#876

This PR adds support for custom `_generate_next_value_` methods on
enums.

When set, this return type will take precedence over inferred `auto()`
member types unless an explicit `_value_` annotation is set.

Falls back to `Any` if custom hooks (like `__init__`) are detected.

References:
- https://typing.python.org/en/latest/spec/enums.html#member-values
-
https://docs.python.org/3/library/enum.html#enum.Enum._generate_next_value_

## Test Plan

New mdtests

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
thejchap pushed a commit to thejchap/ruff that referenced this pull request May 23, 2026
…stral-sh#25210)

## Summary

One oversight in my changes to
astral-sh#25196: if a custom `__new__` is
defined, we should treat the alias value as `Any`, rather than relying
on `_generate_next_value_`. This matches Pyright, for example.

E.g., given:

```python
class E(Enum):
    @staticmethod
    def _generate_next_value_(...) -> Literal["x"]:
        return "x"

    def __new__(cls, value: str):
        obj = object.__new__(cls)
        obj._value_ = object()
        return obj

    A = auto()
    B = auto()
```

Prior to this change, we treated `B` as an alias of `A`; but if
`__new__` is defined, we don't attempt to detect the alias.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants