Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-call @root_validator #114

Closed
wants to merge 2 commits into from

Conversation

slafs
Copy link
Contributor

@slafs slafs commented Aug 8, 2023

In Pydantic v1 having a plain decorator like @root_validator (without ()) was common and worked fine.

In v2, @model_validator needs a mode argument.

Add a test and adjust implementation, so that the program replaces

@root_validator
def foo(cls, values):
    ...

with:

@model_validator(mode="after")
@classmethod
def foo(cls, values):
    ...

The implementation maybe is not beautiful, but seems to work 馃槄.

While working on this I noticed that having a @model_validator() (without args) in v2 produces an error. One has to provide the mode arg.
I can open a separate PR for this,
but this change most likely will need a separate visit method
for ROOT_VALIDATOR_DECORATOR,
because mode in field_validator can be omitted.

In Pydantic v1 having a plain decorator like `@root_validator`
(without `()`) was common and worked fine.

In v2, `@model_validator` needs a `mode` argument.

Add a test and adjust implementation, so that the program
replaces
```py
@root_validator
def foo(cls, values):
    ...
```
with:
```py
@model_validator(mode="after")
@classmethod
def foo(cls, values):
    ...
```
@Kludex
Copy link
Member

Kludex commented Aug 8, 2023

I can open a separate PR for this

Thanks 馃檹

Comment on lines 173 to 174
else:
raise RuntimeError("decorator as attribute is not implemented yet.") # this should never happen ATM
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't have this conditional. Can you add a xfail test for which this would happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. Removed RuntimeError and moved the Name case under else.

Can you add a xfail test for which this would happen?

I think the test is already there in test_import_pydantic, no?

Comment on lines +171 to +175
else:
# Assuming node.decorator matches Name()
# (once ROOT_VALIDATOR_DECORATOR supports Attribute() this needs a separate case).
# Changing `@root_validator` (Name, not Call) into `@model_validator(...)` (Call)
decorator = cst.Call(func=cst.Name(new_name), args=self._args)
Copy link
Member

Choose a reason for hiding this comment

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

But it shouldn't be an else, should it? Does the test you mention fails differently now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I make it an elif (like before) without the else branch, mypy complains about decorator potentially being unbound.

Does the test you mention fails differently now?

Well, the test fails the same everytime ATM, because we never reach that case due to what ROOT_VALIDATOR_DECORATOR is matching (it's not matching decorators as attributes now, so those cases won't be processed even).

@slafs slafs closed this May 27, 2024
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.

None yet

2 participants