Skip to content

fix: handle bare @root_validator without parentheses#174

Open
tysoncung wants to merge 1 commit intopydantic:mainfrom
tysoncung:fix/134-bare-root-validator
Open

fix: handle bare @root_validator without parentheses#174
tysoncung wants to merge 1 commit intopydantic:mainfrom
tysoncung:fix/134-bare-root-validator

Conversation

@tysoncung
Copy link
Copy Markdown

Problem

When @root_validator is 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 the ROOT_VALIDATOR_DECORATOR matcher only matches m.Call(func=m.Name("root_validator")), which requires the decorator to be a function call.

Closes #134

Solution

Added a BARE_ROOT_VALIDATOR_DECORATOR matcher for m.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: Added BARE_ROOT_VALIDATOR_DECORATOR matcher and leave_bare_root_validator_func handler
  • tests/unit/test_validator.py: Removed xfail marker from test_root_validator_as_cst_name and updated expected output to match the new TODO comment behavior

Testing

All 12 tests pass (10 passed, 2 xfailed for unrelated features).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@root_validator will not updated to model_validator

1 participant