Skip to content

Skip attribute suggestions for partial union failures#2149

Closed
sgavriil01 wants to merge 1 commit intofacebook:mainfrom
sgavriil01:fix-union-attribute-suggestion
Closed

Skip attribute suggestions for partial union failures#2149
sgavriil01 wants to merge 1 commit intofacebook:mainfrom
sgavriil01:fix-union-attribute-suggestion

Conversation

@sgavriil01
Copy link
Contributor

When an attribute exists on some union members but not others, skip suggestions to avoid misleading guidance. For example, x.split() on str | None should not suggest rsplit since it also doesn't exist on None.

Fixes #2108

Summary

Modified type_of_attr_get in pyrefly/lib/alt/attr.rs to detect partial union failures (where an attribute exists on some union members but not others) and skip suggestions in those cases. This prevents misleading suggestions like recommending rsplit when split fails on str | None.

The fix checks if found and not_found are both non-empty before suggesting, indicating the attribute exists on some union members but not all.

Suggestions still work correctly when the attribute is missing on all union members or when there's no union involved.

Test Plan

Added 5 comprehensive test cases in pyrefly/lib/test/attributes.rs:

  • test_union_attribute_missing_no_suggestion - Core fix (str | None)
  • test_union_attribute_missing_no_suggestion_three_types - Three-type union
  • test_union_attribute_missing_no_suggestion_mostly_have_it - Two have it, one doesn't
  • test_union_both_missing_should_suggest - All missing (should still suggest)
  • test_union_all_have_attribute_no_error - All have it (no error)

All tests pass (cargo test test_union). Manually verified with release build that the fix works as expected.

When an attribute exists on some union members but not others, skip
suggestions to avoid misleading guidance. For example, x.split() on
str | None should not suggest rsplit since it also doesn't exist on None.

Fixes facebook#2108
@meta-codesync
Copy link

meta-codesync bot commented Jan 21, 2026

@grievejia has imported this pull request. If you are a Meta employee, you can view this in D91089458.

Copy link
Contributor

@yangdanny97 yangdanny97 left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

Copy link
Member

@samwgoldman samwgoldman left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

Copy link
Member

@samwgoldman samwgoldman left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

Copy link
Contributor

@rchen152 rchen152 left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync
Copy link

meta-codesync bot commented Jan 22, 2026

@grievejia merged this pull request in 9faa964.

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.

Pyrefly suggests attributes from wrong type in union

5 participants