Skip to content

fix: show setter type and accessors on property assignment hover - #5572

Closed
Aayushyaash wants to merge 4 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-hover-property-setter-assignment
Closed

Aayushyaash wants to merge 4 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-hover-property-setter-assignment

Conversation

@Aayushyaash

Copy link
Copy Markdown
Contributor

Summary

When hovering over a property or descriptor assignment target (e.g. a.x = 1), Pyrefly previously returned no hover information, even though hovering over property reads (a.x) worked correctly.

This happened because assignment checking returned None for property and descriptor setters to prevent getter types from leaking into assignment targets, leaving these targets without hover information.

This PR fixes the issue by:

  1. Separating flow-sensitive attribute narrowing from target type tracing in check_assign_to_attribute_and_infer_narrow, which now returns AttrAssignResult { narrowed, target_type }.
  2. Recording target_type via record_type_trace in solve.rs so hover displays the setter's parameter type, while keeping narrowed as None to prevent instance attributes from being incorrectly narrowed in subsequent read flows.
  3. Extracting setter value parameter types for bound and unbound property and descriptor setters using get_property_setter_value and get_descriptor_setter_value.
  4. Updating hover formatting in hover.rs to display the canonical attribute type for symmetric properties and both getter and setter types under Property Accessors for asymmetric properties and descriptors.

Fixes #5218

  • I am a human, and I fully understand the code I am submitting.

Test Plan

Added tests in pyrefly/lib/test/lsp/write_target_type.rs and pyrefly/lib/test/lsp/hover.rs covering:

  • Returning setter parameter types instead of None for property and descriptor assignments (p.p = "s", d.d = 1).
  • Displaying (attribute) x: int | None for symmetric property assignments.
  • Displaying the setter parameter type as the primary attribute type and both accessors under Property Accessors for asymmetric properties.
  • Preserving getter types on property reads without leaking setter types or RHS assignment literals.
  • Generic property assignments (Box[T].val = 2) and custom descriptor assignments (h.attr = [1, 2]).

Validation

cargo check
cargo test -p pyrefly test_property_and_descriptor_target
cargo test -p pyrefly hover_on_property_assignment_target
cargo test -p pyrefly write_target_type
python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema --no-extension

@meta-cla meta-cla Bot added the cla signed label Nov 7, 2026
@github-actions github-actions Bot added the size/l label Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

✅ This PR meets all requirements from our pull request checklist.

Note: this comment updates automatically after changes to the PR. Updates may take up to half an hour.

@meta-codesync

meta-codesync Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D130432254. (Because this pull request was imported automatically, there will not be any future comments.)

@Aayushyaash
Aayushyaash marked this pull request as draft November 7, 2026 16:02
@github-actions

This comment has been minimized.

@Aayushyaash
Aayushyaash marked this pull request as ready for review November 7, 2026 16:26
@github-actions
github-actions Bot requested a review from rchen152 November 8, 2026 09:06

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR! I think the direction is good, but we can simplify how we get the getter and setter types.

Comment thread pyrefly/lib/alt/attr.rs Resolved
Comment thread pyrefly/lib/alt/class/class_field.rs Resolved
Comment thread pyrefly/lib/alt/attr.rs Resolved
@Aayushyaash

Copy link
Copy Markdown
Contributor Author

Thanks for the review and feedback! I've made the changes based on your suggestions and rebased the PR on the latest main.

Simplified the property and descriptor handling by reusing the existing type trace instead of resolving the setter type again. Consolidated setter value parameter extraction into a single helper that defaults to implicit Any when the parameter can't be resolved. Fixed the hover type for declared attributes when an assignment doesn't narrow the type, and added a @cached_property test case.

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this is looking better.

Comment thread pyrefly/lib/alt/class/class_field.rs Resolved
Comment thread pyrefly/lib/alt/class/class_field.rs Resolved
@Aayushyaash
Aayushyaash force-pushed the fix-hover-property-setter-assignment branch from ede5a9e to b0321ac Compare November 18, 2026 05:18
@github-actions github-actions Bot added size/m and removed size/m labels Nov 18, 2026
@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@Aayushyaash

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

I overlooked removing index - 1 fallback when unifying the helper in previous review. I've now removed it so missing value parameters default to implicit Any. I've also removed the leftover wrapper and updated descriptor call sites to pass index 2, keeping check_attr_set_and_infer_narrow consistent and added the dataclass regression test.

@meta-codesync

meta-codesync Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 4ac3141.

@Aayushyaash
Aayushyaash deleted the fix-hover-property-setter-assignment branch November 19, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hover on property with setter during assignment shows nothing

2 participants