Skip to content

Conversation

@asukaminato0721
Copy link
Contributor

@asukaminato0721 asukaminato0721 commented Dec 19, 2025

Summary

part of #1451

Added call-time constraint checking for BaseModel fields: after building the dataclass metadata we now collect each field’s gt/ge/lt/le bounds, map positional slots plus aliases back to field names, infer the argument literals, and report BadArgumentType when a literal violates its declared range, so cases like Model(x=0) or Model(x=15) are caught at the callsite.

Hooked the new checker into AnswersSolver::construct_class, so every BaseModel instantiation is validated immediately after __init__ analysis; since we now reuse the class metadata, this avoids re-fetching it and ensures the diagnostics fire for both positional and keyword arguments.

Exposed iter_fields from the dataclass helper so the Pydantic logic can share the same ordering rules, keeping kw-only handling consistent with the existing initializer synthesis.

fix https://github.com/facebook/pyrefly/pull/1458/changes#r2487290587

Test Plan

Updated the regression test so the Field(gt=0, lt=10) example now expects errors for the invalid calls, reflecting the fix for issue.

@meta-cla meta-cla bot added the cla signed label Dec 19, 2025
@asukaminato0721 asukaminato0721 changed the title able to check call site fix Support types like pydantic.types.PositiveInt #1451 able to check call site Dec 19, 2025
@asukaminato0721 asukaminato0721 marked this pull request as ready for review December 19, 2025 13:55
Copilot AI review requested due to automatic review settings December 19, 2025 13:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements call-time constraint checking for Pydantic BaseModel fields with range constraints (gt, ge, lt, le). The implementation adds validation that catches violations of numeric constraints when literal values are passed to model constructors.

Key Changes

  • Added constraint checking for Pydantic field range validators (gt/ge/lt/le) at call sites
  • Integrated constraint validation into the class construction flow after __init__ analysis
  • Exposed iter_fields as pub(crate) to enable constraint collection logic to share field ordering rules

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
pyrefly/lib/test/pydantic/field.rs Updated test expectations to validate that Model(x=0) and Model(x=15) now correctly report constraint violations
pyrefly/lib/alt/class/pydantic.rs Implemented core constraint checking logic including parameter collection, positional/keyword argument mapping, and violation detection
pyrefly/lib/alt/class/dataclass.rs Made iter_fields pub(crate) to allow Pydantic constraint logic to reuse field iteration
pyrefly/lib/alt/call.rs Integrated constraint checking into construct_class flow and optimized metadata retrieval

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@meta-codesync
Copy link

meta-codesync bot commented Dec 22, 2025

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

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.

2 participants