Skip to content

test: Add tests documenting Pydantic Field behavior #558

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

Summary

This PR adds tests documenting the expected behavior for Pydantic Field overload resolution.

Issue #548: Pydantic Field Overload Resolution Failure

The issue reports that Pyrefly fails to match Pydantic Field overloads when using positional default arguments.

Root Cause

The issue stems from how Pydantic uses sentinel values (_Unset/PydanticUndefined) in its type stubs. These sentinel values make parameters appear as having defaults when they should be treated as required from a caller's perspective.

Tests Added

The test file issue_548_pydantic_field.rs includes:

  • Tests showing expected usage with positional arguments
  • Tests demonstrating TypeVar inference with different types
  • Documentation of the workaround (using keyword arguments)

Workaround

Users can work around this issue by using keyword arguments:

# Instead of: Field(5432, ge=1)
# Use: Field(default=5432, ge=1)

Related to #548 - This PR documents the current behavior and workaround rather than fixing the underlying issue.

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 the expected behavior for Pydantic Field overload
resolution and confirm that using keyword arguments works as a workaround.

The issue stems from how Pydantic uses sentinel values in type stubs,
which causes overload matching to fail with positional arguments.

Related to facebook#548
@kimasplund
Copy link
Author

Related to #548

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.

Same here, this PR includes the changes from the other PRs.

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