Skip attribute suggestions for partial union failures #2149
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_getinpyrefly/lib/alt/attr.rsto 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 recommendingrsplitwhensplitfails onstr | None.The fix checks if
foundandnot_foundare 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 uniontest_union_attribute_missing_no_suggestion_mostly_have_it- Two have it, one doesn'ttest_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.