Skip to content

[ty] Correct enum alias detection and scalar constructors#26345

Merged
charliermarsh merged 2 commits into
mainfrom
charlie/fix-enum-value-metadata
Jun 25, 2026
Merged

[ty] Correct enum alias detection and scalar constructors#26345
charliermarsh merged 2 commits into
mainfrom
charlie/fix-enum-value-metadata

Conversation

@charliermarsh

Copy link
Copy Markdown
Member

Summary

Enum member metadata currently uses full Type values when identifying duplicate-value aliases. Literal metadata such as promotability is therefore treated as part of runtime identity, so equivalent values can incorrectly remain separate canonical members.

We also only look for value-transforming __new__ implementations on enum classes. Python's EnumType can instead use a user-defined __new__ from a non-enum data-type mixin to construct the member's scalar payload. Ignoring that constructor can leave .value as the declared literal, skip constructor argument validation, and mark a runtime-reachable equality branch as unreachable.

This keys alias detection by runtime literal kind and payload, and includes user-defined data-type mixin constructors in enum value-construction metadata:

from enum import Enum

class OffsetInt(int):
    def __new__(cls, value: int) -> "OffsetInt":
        return int.__new__(cls, value + 1)

class Offset(OffsetInt, Enum):
    VALUE = 1

reveal_type(Offset.VALUE.value)  # Any

Enum aliases now resolve to the same canonical member even when their inferred literal types differ only in metadata, while transformed scalar-mixin values remain conservative unless we can model the constructor.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 24, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 24, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.47%. The percentage of expected errors that received a diagnostic held steady at 89.19%. The number of fully passing files held steady at 95/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 24, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 24, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@charliermarsh charliermarsh marked this pull request as ready for review June 24, 2026 23:57
@charliermarsh charliermarsh requested a review from a team as a code owner June 24, 2026 23:57
@astral-sh-bot astral-sh-bot Bot requested a review from dhruvmanila June 24, 2026 23:57
@charliermarsh charliermarsh force-pushed the charlie/fix-enum-value-metadata branch from 99d868a to dae9f0d Compare June 25, 2026 00:06
@charliermarsh charliermarsh changed the title [ty] Correct enum member value metadata [ty] Correct enum alias detection and scalar constructors Jun 25, 2026
@charliermarsh charliermarsh force-pushed the charlie/fix-enum-value-metadata branch 2 times, most recently from 65f063a to 1599879 Compare June 25, 2026 02:05
@charliermarsh charliermarsh force-pushed the charlie/fix-enum-value-metadata branch from 1599879 to 6ce7e33 Compare June 25, 2026 23:02
@charliermarsh charliermarsh merged commit 89091c2 into main Jun 25, 2026
63 checks passed
@charliermarsh charliermarsh deleted the charlie/fix-enum-value-metadata branch June 25, 2026 23:14
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.

1 participant