Skip to content

Conversation

@sgavriil01
Copy link

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
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

1 participant