-
Notifications
You must be signed in to change notification settings - Fork 27
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Pydantic warnings. #388
Fix Pydantic warnings. #388
Conversation
Pydantic has deprecated validator and should be replaced by field_validator. See https://docs.pydantic.dev/2.8/migration/#validator-and-root_validator-are-deprecated
Hi! Thanks a lot for the contribution. There is one important complication that we have to keep in mind. If field_validator is not available in V1 as I suspect, you should do a little bit of trickery in the existing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make sure we're backwards compatible with pydantic v1.
Hi @PietroPasotti, thank you for your feedback. Would something along these lines be what you requested? There are some parameters that are no longer parameters in pydantic's from pydantic import validator
def field_validator(
field: str,
/,
*fields: str,
mode: str,
check_fields: Optional[bool] = None,
) -> Callable[[Any], Any]:
"""Wraps the pydantic v1 validator decorator with a v2-compatible interface.
See https://docs.pydantic.dev/latest/migration/#changes-to-config.
"""
# Maps mode to value for `pre`
# See https://docs.pydantic.dev/latest/concepts/validators/#before-after-wrap-and-plain-validators
modes = {
"after": False,
"before": True,
"plain": True,
"wrap": False, # I am honestly not sure what this one should map to...
}
pre = modes.get(mode)
if pre is None:
raise ValueError(f"Unrecognized mode '{mode}'")
# The following arguments are no longer arguments for field_validator:
# each_item, always, whole, and allow_reuse
return validator(
field,
*fields,
pre=pre,
check_fields=check_fields,
) |
I would start by doing things in the simplest possible way: if PYDANTIC_IS_V1:
# right now we don't need any arguments that are incompatible between v1's validator and v2's field_validator, so we can use them transparently.
from pydantic import validator as field_validator
else:
from pydantic import field_validator or will this not work? |
EDIT: I realized I misspoke (or miswrote ?) when I said we don't use the incompatible parameters. For the most part, this was true, but we do use That will not work. The parameter that each of them take is completely different. This is v2's field_validator(
field: str,
/,
*fields: str,
mode: FieldValidatorModes = "after",
check_fields: bool | None = None,
) -> Callable[[Any], Any] And this is v1's def validator(
*fields: str,
pre: bool = False,
each_item: bool = False,
always: bool = False,
check_fields: bool = True,
whole: Optional[bool] = None,
allow_reuse: bool = False,
) -> Callable[[AnyCallable], 'AnyClassMethod']: If the rest of the file only used the |
Thanks for the clarification. So what I'd do is if PYDANTIC_IS_V1:
from pydantic import validator
input_validator = partial(validator, pre=True)
else:
from pydantic import field_validator
input_validator = partial(field_validator, mode="before") which addresses the issues we have right now? |
Yes that would work. I'll make those changes. |
This was deprecated in v2. This instance of `__fields__` was called within a block that only runs in pydantic v2, so it should be fine to replace the function call.
I pushed the fix using |
I'm not sure why the integration test is failing, it seems to be failing across the board for all the PRs and branches, so I assume it doesn't have to do with my PR. @PietroPasotti, should I edit other files to completely get rid of the deprecation warnings? I have found it in 2 other places ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi!
This looks good :)
We're missing two things for this to be accepted from my side:
1 - you need to bump the LIBPATCH in both tracing
and ingress
, so that once we merge this the CI will release the new revisions to charmhub.
2 - not a blocker, but it would be great if you added a test to verify that the warnings are gone! a simple one using pytest
's caplog
fixture should do the trick. I think given the fact that the warnings happen on import, it might be a little tricky to trigger them inside the scope of a test, so perhaps you'll need a fixture to 'unimport' the packages beforehand? Let me know if you need help with that, I did something similar just a few days ago.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Pydantic has deprecated validator and should be replaced by field_validator.
See https://docs.pydantic.dev/2.8/migration/#validator-and-root_validator-are-deprecated
Let me know if you need anything else from me.
Issue
This PR addresses #382.
Solution
As documented by https://docs.pydantic.dev/2.8/migration/#validator-and-root_validator-are-deprecated,
validator
is now deprecated and should be replaced withfield_validator
.pre=True
is no longer a parameter, butmode="before"
should behave in the same way if I properly understood the documentation.Context
Testing Instructions
Upgrade Notes
N/A