⬆️ Update Pydantic v2 code to address deprecations#15101
⬆️ Update Pydantic v2 code to address deprecations#15101tiangolo merged 34 commits intofastapi:masterfrom
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
| "module_path", | ||
| [ | ||
| pytest.param("pydantic.color"), | ||
| pytest.param("pydantic_extra_types.color"), |
There was a problem hiding this comment.
For pydantic_extra_types.color, this will fail on master with
ValueError: [TypeError("'Color' object is not iterable"), TypeError('vars() argument must have dict attribute')]
There was a problem hiding this comment.
For pydantic_extra_types.color, this will fail on master with
ValueError: [TypeError("'Color' object is not iterable"), TypeError('vars() argument must have dict attribute')]
I took me some time to understand this comment 😅
Just to clarify if somebody also gets confused: it fails on master because pydantic_extra_types.color.Color is not currently supported by jsonable_encoder. This PR adds such support
|
Thanks for the detailed review Yurii, I addressed everything, let me know what you think! |
| globalns: dict[str, Any] | None = None, | ||
| localns: dict[str, Any] | None = None, | ||
| ) -> Any: | ||
| # eval_type_lenient has been deprecated since Pydantic v2.10.0b1 (PR #10530) |
There was a problem hiding this comment.
As an alternative, we can use try...except:
try:
return _pydantic_typing_extra.try_eval_type(value, globalns, localns)[0]
except AttrbuteError:
return _pydantic_typing_extra.eval_type_lenient( # ty: ignore[deprecated]
value, globalns, localns
) # pragma: no coverOr move this to module level, and only use it inside evaluate_forwardref (so that evaluate_forwardref would make it typed).
It might also be a bit faster (but it's only used at app stratup, so maybe not as important)
There was a problem hiding this comment.
Yea I think it's a style preference, I'm personally more fan of checking explicitely first rather than relying on a try-except, but both are valid options. I'll leave it up to Sebastián to decide if he's got a preference 🙂
There was a problem hiding this comment.
I found it a bit unfortunate that this PR still relies on an internal function.. The only reason I kept eval_type_lenient() in Pydantic was because FastAPI was using it, and now the same issue exists with try_eval_type().
There was a problem hiding this comment.
@Viicos: right, good point. What would be the better way to address this? I can make a new PR.
There was a problem hiding this comment.
I think it might be worth solving this cleanly, that is by first having the ability to do such evaluations using the stdlib. I proposed https://discuss.python.org/t/100698 a while ago, I'll try to continue working on it. Then we could see how can this be used in FastAPI/Pydantic!
| "module_path", | ||
| [ | ||
| pytest.param("pydantic.color"), | ||
| pytest.param("pydantic_extra_types.color"), |
There was a problem hiding this comment.
For pydantic_extra_types.color, this will fail on master with
ValueError: [TypeError("'Color' object is not iterable"), TypeError('vars() argument must have dict attribute')]
I took me some time to understand this comment 😅
Just to clarify if somebody also gets confused: it fails on master because pydantic_extra_types.color.Color is not currently supported by jsonable_encoder. This PR adds such support
When running
tyon the code base (cf #15091), we got two valid deprecation warnings:eval_type_lenient
eval_type_lenienthas been deprecated since pydantic#1053, which was released as part of v2.10.0b1.color
pydantic.color.Colorhas been deprecated since pydantic#6003, which was released as part of v2.0b3.ENCODERS_BY_TYPEto also cover the newpydantic_extra_types.color.Color. The added unit test fails onmaster.Open question
As Pydantic updates & makes older functionality deprecated, we could (also in the future) be tempted to update the methods (especially whentytells us to), without realizing we might be breaking older versions that we still support according to ourpyproject.toml. For this reason, I'd want to add a specific test CLI run with Pydantic 2.7.0 to ensure it keeps working as expected. Aye? Nay?UPDATE: cf #15139!