Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pyrefly/lib/alt/class/dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
21 changes: 21 additions & 0 deletions pyrefly/lib/test/pydantic/root_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
"#,
);
Loading