Repository navigation
Respect model-level strict config in synthesized RootModel __init__ - #5649
Open
soaring0616 wants to merge 1 commit into
Open
soaring0616 wants to merge 1 commit into
soaring0616 wants to merge 1 commit into
Conversation
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
For a concrete root type (not
Anyor a type variable), Pyrefly widens therootparameter of the synthesizedRootModel.__init__toAnywhen strict mode is disabled (pydantic.rs#L165-L169). This allows valid Pydantic runtime coercions (e.g., passingbytestoRootModel[str]) without triggering false positives, preservingrootto the concrete type only when strict mode is enabled.Previously,
has_strictwas computed inget_pydantic_root_model_type_via_mro(L131-L133), which only captures strictness written on the base class type argument (such asRootModel[StrictInt]), including when inherited from a parent class. As a result, model-levelstrict=Truefrommodel_configor a class keyword was ignored even though it is already resolved intodataclass.kws.strict.This change takes
dataclass.kws.strictinto account when synthesizingRootModel.__init__, reusing thedataclassconfig already available there. This change also adds a test case covering themodel_config, class keyword, inherited, and lax cases.Fixes #5242
What changed
pyrefly/lib/alt/class/dataclass.rs:L156:160: Passeddataclass.kws.strictalongsidehas_strictwhen callingget_pydantic_root_model_init.pyrefly/lib/test/pydantic/root_model.rs:L353:373: Addedtest_root_model_config_strictwith test classes coveringmodel_config(class Text), class keyword (class Text2), inherited strictness (class Child), and the default lax behavior (class Lax).Test Plan
cargo test -p pyrefly --lib -- test::pydanticshowstest result: ok. 174 passed; 0 failed; 0 ignoredin my local machine-
test_root_model_config_strictto fail withexpected 3 errors, but got 0.test.py): On macOS debug builds,test::cycles::deep_phi_chain_term_exprhits a pre-existing stack overflow (reproduced without this change); the suite passes withPYREFLY_STACK_SIZE=33554432.cargo fmt --checkandcargo clippypass with no warnings.AI disclosure