You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pyrefly reports false positives for Pydantic: Field(strict=True) on BaseModel and RootModel[list[StrictInt]] #5762
needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.pydanticIssues related to support for Pydantictypechecking
Pyrefly reports false-positive bad-argument-type errors when Pydantic strictness applies only to the outer container or only to the inner elements:
On BaseModel, Field(strict=True) enforces strictness only on the outer container (list), while elements remain lax and accept coercion (e.g., ["1"] -> [1]).
On RootModel[list[StrictInt]], StrictInt constrains only the elements, while the outer list remains lax and accepts other iterables like tuples ((1,) -> [1]).
Expected behavior
Pyrefly should accept M(x=["1"]) and R((1,)) without errors.
Repro
frompydanticimportBaseModel, Field, RootModel, StrictIntclassM(BaseModel):
x: list[int] =Field(strict=True)
classR(RootModel[list[StrictInt]]):
pass# Valid in Pydantic (coerced to [1]), rejected by PyreflyM(x=["1"])
R((1,))
# Control case: outer type is a tuple, not a listM(x=(1,)) # Rejected by both Pydantic and Pyrefly
Detailed
on Pydantic (2.13.5, 2.14.4):
M(x=["1"]) -> OK (x == [1])
R((1,)) -> OK (root == [1])
M(x=(1,)) -> ValidationError (list_type)
onPyrefly (1.5.0, default preset):
M(x=["1"]) -> bad-argument-type
R((1,)) -> bad-argument-type
M(x=(1,)) -> bad-argument-type (expected)
Related
#5649 (handles shallow vs. deep Field(strict=True) strictness on RootModel).
Note: this might be difficult to implement, Pyrefly models Pydantic strictness at the class field level (a flat representation), not at the type level (which would be quite invasive).
It starts to get into territory where it's debatable how far we're willing to push for Pydantic compatibility. We could maybe just eliminate the strictness flag and replace it with some representation that mimics the structure of the type if we really want to support this.
We might also want to just not enforce strictness in mixed cases; that would err on the side of avoiding false positives, while also not erroring on overly lax uses of mixed-strictness fields.
needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.pydanticIssues related to support for Pydantictypechecking
Activity
stroxler commentedon Nov 25, 2026
Note: this might be difficult to implement, Pyrefly models Pydantic strictness at the class field level (a flat representation), not at the type level (which would be quite invasive).
It starts to get into territory where it's debatable how far we're willing to push for Pydantic compatibility. We could maybe just eliminate the strictness flag and replace it with some representation that mimics the structure of the type if we really want to support this.
We might also want to just not enforce strictness in mixed cases; that would err on the side of avoiding false positives, while also not erroring on overly lax uses of mixed-strictness fields.