Skip to content

Pyrefly reports false positives for Pydantic: Field(strict=True) on BaseModel and RootModel[list[StrictInt]] #5762

Backlog

Description

@soaring0616

Summary

Pyrefly reports false-positive bad-argument-type errors when Pydantic strictness applies only to the outer container or only to the inner elements:

  1. On BaseModel, Field(strict=True) enforces strictness only on the outer container (list), while elements remain lax and accept coercion (e.g., ["1"] -> [1]).
  2. 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

from pydantic import BaseModel, Field, RootModel, StrictInt

class M(BaseModel):
    x: list[int] = Field(strict=True)

class R(RootModel[list[StrictInt]]):
    pass

# Valid in Pydantic (coerced to [1]), rejected by Pyrefly
M(x=["1"]) 
R((1,)) 

# Control case: outer type is a tuple, not a list
M(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).

Activity

  1. added
    pydantic
    on Nov 25, 2026
  2. added
    needs-discussion
    on Nov 25, 2026
  3. stroxler commented on Nov 25, 2026

    @stroxler
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-discussionpydantictypechecking

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions