[ty] better support for enum.property#25681
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe 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. |
Memory usage reportMemory usage unchanged ✅ |
|
charliermarsh
left a comment
There was a problem hiding this comment.
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!
|
It looks like the fix for the top-level comment requires storing an extra field in |
Merging this PR will improve performance by 4.57%
Performance Changes
Tip Curious why this is faster? Use the CodSpeed MCP and ask your agent. Comparing Footnotes
|
|
Makes sense, thanks! |
## 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.
Summary
Treat
enum.propertyas a known class withpropertylike behavior so we get accurate types from its__get__method, instead of justAny.Closes astral-sh/ty#3681
Testing
Added mdtests