fix: handle bare @root_validator without parentheses#174
Open
tysoncung wants to merge 1 commit intopydantic:mainfrom
Open
fix: handle bare @root_validator without parentheses#174tysoncung wants to merge 1 commit intopydantic:mainfrom
tysoncung wants to merge 1 commit intopydantic:mainfrom
Conversation
Previously, @root_validator (without parentheses) was silently ignored by the codemod because the matcher only matched @root_validator() (with parentheses as a Call node). This change adds a matcher for the bare decorator form and adds a TODO comment instructing users to manually replace it with @model_validator, consistent with how the tool handles other cases it can't auto-refactor. Also removes the xfail marker from the corresponding test case.
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.
Problem
When
@root_validatoris used without parentheses (i.e., as a bare decorator rather than@root_validator()), the codemod silently ignores it — no migration, no TODO comment, nothing. This is because theROOT_VALIDATOR_DECORATORmatcher only matchesm.Call(func=m.Name("root_validator")), which requires the decorator to be a function call.Closes #134
Solution
Added a
BARE_ROOT_VALIDATOR_DECORATORmatcher form.Decorator(decorator=m.Name("root_validator"))and a corresponding handler that adds a TODO comment instructing the user to manually replace it with@model_validator. This is consistent with how the tool handles other cases it cannot auto-refactor.Changes
bump_pydantic/codemods/validator.py: AddedBARE_ROOT_VALIDATOR_DECORATORmatcher andleave_bare_root_validator_funchandlertests/unit/test_validator.py: Removedxfailmarker fromtest_root_validator_as_cst_nameand updated expected output to match the new TODO comment behaviorTesting
All 12 tests pass (10 passed, 2 xfailed for unrelated features).