Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] better support for enum.property#25681

Merged
carljm merged 6 commits into
mainfrom
cjm/fix-ty-issue-3681
Jun 8, 2026
Merged

[ty] better support for enum.property#25681
carljm merged 6 commits into
mainfrom
cjm/fix-ty-issue-3681

Conversation

@carljm

@carljm carljm commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Treat enum.property as a known class with property like behavior so we get accurate types from its __get__ method, instead of just Any.

Closes astral-sh/ty#3681

Testing

Added mdtests

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

astral-sh-bot Bot commented Jun 5, 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 92.23%. The percentage of expected errors that received a diagnostic held steady at 87.42%. The number of fully passing files held steady at 92/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 5, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 5, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@carljm carljm marked this pull request as ready for review June 6, 2026 00:07
@astral-sh-bot astral-sh-bot Bot requested a review from charliermarsh June 6, 2026 00:08
Comment thread crates/ty_python_semantic/src/types/enums.rs Outdated

@charliermarsh charliermarsh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Codex says we get this wrong because constructor calls aren't preserving the nominal type:

from enum import Enum, property as enum_property

def get(value: Enum) -> str:
    return value.name

descriptor = enum_property(get)

retained: enum_property = descriptor
reveal_type(descriptor.name)  # revealed: str
reveal_type(descriptor.clsname)  # revealed: str
reveal_type(descriptor.member)  # revealed: Enum | None

(retained produces an invalid-assignment, and all three member accesses produce
unresolved-attribute.)

I defer to you on what's worth supporting though!

@carljm

carljm commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

It looks like the fix for the top-level comment requires storing an extra field in PropertyInstanceType, to record the fallback instance type for the property. The fix makes sense to me, but I'd like to evaluate its performance and ecosystem impact separately from this fix, so I'll land this and push that up as a separate PR.

@carljm carljm enabled auto-merge (squash) June 8, 2026 21:49
@carljm carljm merged commit 6425b7b into main Jun 8, 2026
58 checks passed
@carljm carljm deleted the cjm/fix-ty-issue-3681 branch June 8, 2026 21:51
@codspeed-hq

codspeed-hq Bot commented Jun 8, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 4.57%

⚡ 1 improved benchmark
✅ 66 untouched benchmarks
⏩ 60 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime static_frame 23.2 s 22.1 s +4.57%

Tip

Curious why this is faster? Use the CodSpeed MCP and ask your agent.


Comparing cjm/fix-ty-issue-3681 (1323622) with main (c963f8c)

Open in CodSpeed

Footnotes

  1. 60 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

Copy link
Copy Markdown
Member

Makes sense, thanks!

carljm added a commit that referenced this pull request Jun 11, 2026
## Summary

This is a follow-up to #25681 that preserves the nominal class of
synthesized property instances created by `enum.property`.

Previously, `PropertyInstanceType` always fell back to
`builtins.property`. That discarded the `enum.property` identity, so
assignments to `enum.property` failed and enum-specific members such as
`name`, `clsname`, and `member` were unavailable. The originating known
class is now retained through accessor replacement, type
transformations, member lookup, descriptor dispatch, relations, `super`,
and display.

The regression coverage also checks precise getter and setter behavior.
A TODO documents the remaining limitation that constructing a subclass
of `enum.property` currently collapses the result to `enum.property`.

## Test Plan

Added/updated mdtests.
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.

support precise types from enum.property getter

2 participants