8000 test: Add tests documenting init helper method limitation by kimasplund · Pull Request #559 · facebook/pyrefly · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

test: Add tests documenting init helper method limitation #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kimasplund 8000
Copy link

Summary

This PR adds tests documenting the current limitation where helper methods called from __init__ are not recognized as valid attribute-defining methods.

Issue #550: False Positive for Initialization Helper Methods

Pyrefly reports "implicitly-defined-attribute" errors when attributes are set in helper methods called from __init__, which is a common pattern for organizing initialization logic.

Current Behavior

Pyrefly only recognizes these methods as valid for attribute definition:

  • __init__, __init_subclass__, __new__
  • For test classes: setUp, setUpClass, and related methods

Helper methods called from these recognized methods are not tracked.

Tests Added

The test file issue_550_init_helpers.rs includes:

  • Basic init helper pattern
  • Conditional init helpers
  • Nested init helpers
  • Test showing errors when helper is NOT called from init

Future Enhancement

A full fix would require flow analysis to:

  1. Track which methods are called from __init__
  2. Mark those methods as part of the construction process
  3. Allow attribute assignments in those methods

Related to #550 - This PR documents the current limitation to guide future implementation.

This commit adds test cases for four reported issues:

1. Issue facebook#547: Dict narrowing after None checks - Already fixed in current version
   - Tests confirm proper type narrowing for Optional[Dict] after is not None checks
   - Tests cover attribute chains, early returns, and nested narrowing

2. Issue facebook#548: Pydantic Field overload resolution
   - Tests document expected behavior with positional arguments
   - Issue stems from Pydantic's use of sentinel values in type stubs
   - Workaround: use keyword arguments (default=value)

3. Issue facebook#549: False positives on union types with Any
   - Added type_contains_any() helper to check for Any in unions
   - When Any is present in a union, allow any attribute access without errors
   - Partial fix: works for explicit unions, Dict[str, Any] needs separate handling

4. Issue facebook#550: Init helper methods
   - Tests document the limitation that helper methods aren't recognized
   - Full fix requires flow analysis to track methods called from __init__

These tests improve code coverage and document both working features
and known limitations, providing a foundation for future improvements.
These tests document that Pyrefly currently doesn't recognize helper
methods called from __init__ as valid attribute-defining methods.

A full fix would require flow analysis to track which methods are
called during construction.

Related to facebook#550
@kimasplund
Copy link
Author

Related to #550

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.

This PR also contains the changes from 557 and 558

@facebook-github-bot
Copy link
Contributor

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

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.

3 participants
0