Skip to content

[ty] Model int and str enum value normalization#26349

Draft
charliermarsh wants to merge 15 commits into
mainfrom
charlie/normalize-built-in-enum-mixin-values
Draft

[ty] Model int and str enum value normalization#26349
charliermarsh wants to merge 15 commits into
mainfrom
charlie/normalize-built-in-enum-mixin-values

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 25, 2026

Copy link
Copy Markdown
Member

Summary

Enum data types construct a member's _value_ before aliases are registered. Prior to this change, we used the declared right-hand side for both .value inference and alias detection, so constructor normalization could be lost and runtime aliases could remain separate canonical members.

This PR models the normalization performed when an enum's data type is the known built-in int or str:

from enum import Enum

class Number(int, Enum):
    FROM_BOOL = False
    FROM_INT = 0

reveal_type(Number.FROM_BOOL.value)  # Literal[0]
reveal_type(Number.FROM_INT)  # Literal[Number.FROM_BOOL]

We apply the same model through IntEnum, StrEnum, and memberless parent enum bases, recursively normalize union-valued members, and normalize generated auto() values. Aliased declarations still contribute their raw right-hand sides to the history used by a following _generate_next_value_ call.

User-defined data types remain opaque for .value inference and alias detection, even when they inherit from int or str without overriding methods. This avoids assuming that arbitrary construction, attribute access, equality, or hashing behavior matches the built-in scalar.

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

astral-sh-bot Bot commented Jun 25, 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 25, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-argument-type 0 7 0
Total 0 7 0

Raw diff:

schemathesis (https://github.com/schemathesis/schemathesis)
- src/schemathesis/generation/overrides.py:75:59 error[invalid-argument-type] Argument to function `lookup_parameter` is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/generation/dictionaries.py:112:73 error[invalid-argument-type] Argument to function `_find_parameter_binding` is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/generation/dictionaries.py:115:97 error[invalid-argument-type] Argument to function `_find_parameter_binding` is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/generation/dictionaries.py:230:21 error[invalid-argument-type] Argument is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/specs/openapi/extra_data_source.py:181:9 error[invalid-argument-type] Argument is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/specs/openapi/extra_data_source.py:199:9 error[invalid-argument-type] Argument is incorrect: Expected `str`, found `Literal["query", "header", "path", "cookie", "body"] | None`
- src/schemathesis/specs/openapi/extra_data_source.py:645:38 error[invalid-argument-type] Argument to bound method `list.append` is incorrect: Expected `tuple[str, str]`, found `tuple[Literal["query", "header", "path", "cookie", "body"] | None, str]`

Full report with detailed diff (timing results)

@codspeed-hq

codspeed-hq Bot commented Jun 25, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.57%

❌ 2 regressed benchmarks
✅ 83 untouched benchmarks
⏩ 64 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime pandas 76.3 s 80.3 s -4.96%
WallTime tanjun 2.9 s 3 s -4.18%

Tip

Investigate this regression with the CodSpeed MCP and your agent.


Comparing charlie/normalize-built-in-enum-mixin-values (38fc949) with main (e9719d4)

Open in CodSpeed

Footnotes

  1. 64 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from e443c06 to 99e6ef0 Compare June 25, 2026 02:01
@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/normalize-built-in-enum-mixin-values branch from 99e6ef0 to 70a659b Compare June 25, 2026 02:05
@charliermarsh charliermarsh marked this pull request as ready for review June 25, 2026 22:58
@charliermarsh charliermarsh requested a review from a team as a code owner June 25, 2026 22:58
@astral-sh-bot astral-sh-bot Bot requested a review from sharkdp June 25, 2026 22:58
@charliermarsh charliermarsh added the bug Something isn't working label Jun 25, 2026
@charliermarsh charliermarsh force-pushed the charlie/fix-enum-value-metadata branch from 1599879 to 6ce7e33 Compare June 25, 2026 23:02
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from 70a659b to 579b61b Compare June 25, 2026 23:03
Base automatically changed from charlie/fix-enum-value-metadata to main June 25, 2026 23:14
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from 579b61b to 193d068 Compare June 25, 2026 23:14
@charliermarsh charliermarsh marked this pull request as draft June 25, 2026 23:22
@charliermarsh charliermarsh changed the title [ty] Normalize built-in enum mixin values [ty] Model int and str enum value normalization Jun 26, 2026
@charliermarsh charliermarsh force-pushed the charlie/normalize-built-in-enum-mixin-values branch from 2f27190 to 532e2cd Compare June 26, 2026 00:49
@charliermarsh charliermarsh removed the request for review from sharkdp June 26, 2026 00:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant