From 80dc44788a8d3c499433dd86d1a7fbcab77f9d9b Mon Sep 17 00:00:00 2001 From: soaring0616 <30642533+soaring0616@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:44:39 +0800 Subject: [PATCH] Respect model-level strict config in synthesized RootModel __init__ Fixes #5242 --- pyrefly/lib/alt/class/dataclass.rs | 6 +++++- pyrefly/lib/test/pydantic/root_model.rs | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pyrefly/lib/alt/class/dataclass.rs b/pyrefly/lib/alt/class/dataclass.rs index d4bbc1e50f..9e6deaca5b 100644 --- a/pyrefly/lib/alt/class/dataclass.rs +++ b/pyrefly/lib/alt/class/dataclass.rs @@ -153,7 +153,11 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { let init_method = if let Some((root_model_type, has_strict)) = self.get_pydantic_root_model_type_via_mro(cls, metadata) { - self.get_pydantic_root_model_init(cls, root_model_type, has_strict) + self.get_pydantic_root_model_init( + cls, + root_model_type, + has_strict || dataclass.kws.strict, + ) } else if metadata.is_pydantic_model() { // Pydantic models with RootModel fields need type expansion let transform_type: &dyn Fn(Type) -> Type = &|ty: Type| { diff --git a/pyrefly/lib/test/pydantic/root_model.rs b/pyrefly/lib/test/pydantic/root_model.rs index 2186f8c112..70fc0d9f9c 100644 --- a/pyrefly/lib/test/pydantic/root_model.rs +++ b/pyrefly/lib/test/pydantic/root_model.rs @@ -350,3 +350,24 @@ def serialize(d: InnerModelDict) -> OuterModel: return OuterModel(data=d) "#, ); + +pydantic_testcase!( + test_root_model_config_strict, + r#" +from pydantic import ConfigDict, RootModel + +class Text(RootModel[str]): + model_config = ConfigDict(strict=True) + +class Text2(RootModel[str], strict=True): ... + +class Child(Text): ... + +class Lax(RootModel[str]): ... + +Text(b"hello") # E: Argument `Literal[b'hello']` is not assignable to parameter `root` with type `str` +Text2(b"hello") # E: Argument `Literal[b'hello']` is not assignable to parameter `root` with type `str` +Child(b"hello") # E: Argument `Literal[b'hello']` is not assignable to parameter `root` with type `str` +Lax(b"hello") +"#, +);