Skip to content

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes #2156

Adjusted Pydantic lax conversion to preserve literal types so Literal[...] fields stay strict even when lax mode is enabled. This prevents converting Literal into Any, which is why pydantic dataclasses were silently accepting bad values.

Test Plan

update test, add a test

@asukaminato0721 asukaminato0721 marked this pull request as ready for review January 19, 2026 12:40
@meta-cla meta-cla bot added the cla signed label Jan 19, 2026
Copilot AI review requested due to automatic review settings January 19, 2026 12:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes the handling of typing.Literal types in Pydantic's lax mode. Previously, Literal types were being converted to Any when lax mode was enabled, causing the type checker to incorrectly accept invalid values. The fix ensures that Literal types remain strict even in lax mode, which aligns with Pydantic's runtime behavior.

Changes:

  • Modified the lax mode type expansion logic to preserve Literal and LiteralString types
  • Updated an existing test to expect an error now that the bug is fixed
  • Added a new test for dataclass Literal field validation

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pyrefly/lib/alt/class/pydantic_lax.rs Added pattern matching for Type::Literal(_) and Type::LiteralString(_) to preserve these types during lax mode expansion
pyrefly/lib/test/pydantic/strictness.rs Removed bug annotation and added expected error message for literal type mismatch test
pyrefly/lib/test/pydantic/dataclasses.rs Added new test case validating that Literal fields in Pydantic dataclasses correctly reject invalid values

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing support for typing.Literal in pydantic

1 participant