diff --git a/pyrefly/lib/alt/class/pydantic_lax.rs b/pyrefly/lib/alt/class/pydantic_lax.rs index ecf3bd01b..33922af40 100644 --- a/pyrefly/lib/alt/class/pydantic_lax.rs +++ b/pyrefly/lib/alt/class/pydantic_lax.rs @@ -167,7 +167,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { fn expand_type_for_lax_mode(&self, ty: &Type) -> Type { match ty { - Type::None => ty.clone(), + Type::None | Type::Literal(_) | Type::LiteralString(_) => ty.clone(), Type::Type(box inner) => Type::Type(Box::new(self.expand_type_for_lax_mode(inner))), // Tuple types: convert to Iterable[T] where T is a union of expanded element types Type::Tuple(tuple) => self diff --git a/pyrefly/lib/test/pydantic/dataclasses.rs b/pyrefly/lib/test/pydantic/dataclasses.rs index 0eaf6f11b..0260ec130 100644 --- a/pyrefly/lib/test/pydantic/dataclasses.rs +++ b/pyrefly/lib/test/pydantic/dataclasses.rs @@ -29,3 +29,18 @@ class A: A(x='0') "#, ); + +pydantic_testcase!( + test_dataclass_literal_strictness, + r#" +from typing import Literal +from pydantic.dataclasses import dataclass + +@dataclass +class Example: + value: Literal["MyLiteral"] + +Example(value="MyLiteral") +Example(value=2) # E: Argument `Literal[2]` is not assignable to parameter `value` with type `Literal['MyLiteral']` in function `Example.__init__` + "#, +); diff --git a/pyrefly/lib/test/pydantic/strictness.rs b/pyrefly/lib/test/pydantic/strictness.rs index 0c7b6ea54..343d03761 100644 --- a/pyrefly/lib/test/pydantic/strictness.rs +++ b/pyrefly/lib/test/pydantic/strictness.rs @@ -175,7 +175,6 @@ reveal_type(Model10.__init__) # E: revealed type: (self: Model10, *, u: LaxUUID ); pydantic_testcase!( - bug = "An error should be raised here", test_lax_mode_coercion_literals, r#" from typing import Literal @@ -184,7 +183,7 @@ from pydantic import BaseModel class Model1(BaseModel): status: Literal[1] -m = Model1(status="1") +m = Model1(status="1") # E: Argument `Literal['1']` is not assignable to parameter `status` with type `Literal[1]` in function `Model1.__init__` "#, );