-
Notifications
You must be signed in to change notification settings - Fork 403
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
RFC: Add support for Pydantic v2 #2672
Comments
RFC updated with a test using Lambda Layer + Plan to release a version with Pydantic v2. |
This is a mega job with the investigation. To be honest I will be extremely suprised if we can keep our code working with both v1 and v2, but you seems to be close! I believe the biggest problem is that deprecation of the root_validator. Even though it's deprecated, could we maybe keep it until we release Powertools v3 (using Pydantic v2 only)?. Another concern is customer code: unless we are very careful with our docs, custom models based on pydantic v1 would fail on v2 unless they apply the same changes (e.g: Optional fields must have default value). Sod o you still believe we can support both versions with the same code? |
Reading |
Firstly, congrats @leandrodamascena on your first RFC - bar raising to say the least. We should update our Maintainers playbook to include this RFC when it comes to breaking changes. Some quick recommendations:
Assuming we can indeed keep both v1 and v2 working with our models, I've noticed two major areas missing: Dependency resolution for Pydantic v1 and v2Customers will run into a conflict when having either All Powertools features and Pydantic v2 aws-lambda-powertools[all]
pydantic # or pydantic>=2 Powertools parser feature and Pydantic v2 aws-lambda-powertools[parser]
pydantic # or pydantic>=2 RecommendationWe should include a new section in the documentation to explain how to use Pydantic v2 with Parser. For example, customers should refrain from using
Because of the This also gives us room to recommend disabling warnings for deprecated features we're keeping for backwards compatibility (e.g., validators). This prevents Pydantic littering customers' logs ($$) when bringing Pydantic v2. Ad-hoc test for pydantic v2 depUntil Powertools V3 is a thing, we should guard against backwards incompatibility for newer Pydantic models, or changes to existing models that might be contributed externally from a Pydantic v2 customer. RecommendationSetup a new temporary GitHub Action workflow to trigger on changes to Parser's models. We can use Nox to streamline dependencies and easily trigger Parser's unit tests. Alternatively, within this temporary workflow, we could call Nox's benefit is that it's more stable, easier to reason, and it will lead us to address an unrelated area we aren't testing today -- tests w/o optional dependencies (e.g., Batch without parser code). PS: We should try GitHub Discussions next time to have threaded conversations about areas of a given RFC. |
@ran-isenberg comment - #2427 (comment)
We will not rename functions marked as deprecated. We will keep these functions to avoid breaking changes for anyone wanting to use Powertools with Pydantic 1. Our plan is to remove support for Pydantic v1 functions in Powertools V3.
By adding the default value
These functions were not removed, they were marked as deprecated and we can still use both in Pydantic v2.
Indeed its a good point, Ran. In this case I think we need to change the validators to raise ValueError/ValidationError instead of TypeError. I'll test it in more detail. Thanks a lot for bringing up these points. |
Yeah @rubenfonseca, that is the idea to avoid breaking changes.
Absolutely. Because of that, I added a table to make sure we will cover this on our documentation.
Our goal is to do just that and try to cover every part of this RFC to avoid breaking changes. |
Thanks a lot for this comment @heitorlessa! This is very important for me ❤️
Added
Done I added the new 2 sections. |
Thx @leandrodamascena , sounds like a plan! |
I've added an item to take care of this, but if the customer decides to upgrade to Pydantic V2 then they can suppress the Pydantic warning by disabling the warning messages. We also can do this on the Powertools side. from pydantic import validator, ValidationError, PydanticDeprecationWarning
import warnings
warnings.filterwarnings("ignore", category=PydanticDeprecationWarning) |
|
Update: going to release this in the next few hours. |
This is now released under 2.21.0 version! |
Is this related to an existing feature request or issue?
#2427
Which Powertools for AWS Lambda (Python) utility does this relate to?
Parser
Summary
This RFC proposes changes to Parser’s Pydantic Models to support both Pydantic V2 and V1 without breaking changes.
In Powertools v3 (~EOL, date not settled yet), we will then remove support for Pydantic V1, and update our Parser’s dependency to use Pydantic v2.
Compatibility table
Use case
Pydantic V2, the latest version of Pydantic, has been launched with enhanced features and performances. Customers using Powertools for AWS Lambda (Python) in their AWS Lambda functions express interest to update it to Pydantic V2. To meet the customer's needs, the integration of Pydantic V2 in Powertools is required.
This integration enables customers to update their workloads to use Pydantic V2 while ensuring a seamless transition for existing customers using Pydantic V1.
Proposal
To accommodate both Pydantic v1 and Pydantic v2, the proposed design is to refactor the parser models with minimal changes. These changes should work in both versions of Pydantic and be transparent to users.
TL;DR: Proposed actions summary
None
for optional fields@validator
and@root_validator
deprecated features with a note to remove in V3@parse_obj
deprecated features with a note to remove in V3datetime
coercionOptional fields must have default value
Pydantic v1
Pydantic v2
Validators are deprecated
Both
@root_validator
and@validator
validators are deprecated in Pydantic V2 and will be removed in Pydantic V3. Pydantic recommends using the new@model_validator
and@field_validator
validators. However, we can continue using the deprecated validators in Powertools to avoid breaking changes and plan their removal for Powertools v3.Pydantic v1
Pydantic v2
Another alternative is to check the Pydantic version and add the conditional import. We will also need to create a function to wrap the validator decorators and check the Pydantic version. This workaround may make the models harder to read and understand for maintenance purposes.
Powertools Layer
The Powertools Layer is built with Pydanticv1 and this can be a potential problem if the customer uses our layer and brings Pydantic v2 as an external dependency.
In tests with Lambda Powertools Layer + Pydanticv2 installed as an external dependency, Lambda first includes the
/var/task
path, that is, the external dependency will have preference over the one used in the Layer and it allows the customer brings their preferred Pydantic version.Path
Pydantic Version
Warnings
To ensure a smooth transition and minimize disruptions for our users, we have temporarily suppressed the
PydanticDeprecatedSince20
andPydanticDeprecationWarning
warnings (related to these functions). This allows existing applications to continue functioning as expected without outputting warnings.If needed, you can enable the warnings yourself with something like the code below. Reference: https://docs.python.org/3/library/warnings.html
Out of scope
Refactorings involving breaking change for customers who want to use v1. If there is something that involves breaking, it will be left out of this change.
We could this opportunity to evaluate the performance of Pydantic V2 and potentially enhance the performance of Parser utility - not required tho.
Potential challenges
Most of the challenges were addressed and I was able to use the Powertools for AWS Lambda (Python) Parser utility with Pydantic v2 with several models. But some challenges still need to be understood whether this is a breaking change or not.
Working with datetime fields
In Pydantic v1, when using
datetime
fields, the UTC offset is included and the tests work fine. However, in Pydantic v2, the UTC offset is not included, causing our tests to fail.Codebase
Pydantic v1
Pydantic v2
Batch processing
Some Batch processing tests are failing and I need to investigate why.
Developer environment
Since some of our dependencies (
cfn-lint
/aws-sam-translator
) have a requirement of Pydantic v1, we'll need to remove them from our development environment in order to accommodate Pydantic v2.Dependency resolution for Pydantic v1 and v2
Customers will run into a conflict when having either
requirements.txt
:All Powertools features and Pydantic v2
Powertools parser feature and Pydantic v2
Recommendation
We should include a new section in the documentation to explain how to use Pydantic v2 with Parser.
For example, customers should refrain from using
[all]
or[parser]
when bringing Pydantic v2 as part of their dependencies.aws-lambda-powertools[all]
becomesaws-lambda-powertools[validation,tracer,aws-sdk]
Because of the
Optional[str] = None
breaking change in v2, we should keep our pydantic pinning to v1 until we launch v3 and move away. We cannot guarantee a customer is using additional Pydantic v1 features through Powertools - or followed our docs to the letter.This also gives us room to recommend disabling warnings for deprecated features we're keeping for backwards compatibility (e.g., validators). This prevents Pydantic littering customers' logs ($$) when bringing Pydantic v2.
Ad-hoc test for pydantic v2 dep
Until Powertools V3 is a thing, we should guard against backwards incompatibility for newer Pydantic models, or changes to existing models that might be contributed externally from a Pydantic v2 customer.
Recommendation
Setup a new temporary GitHub Action workflow to trigger on changes to Parser's models. We can use Nox to streamline dependencies and easily trigger Parser's unit tests.
Alternatively, within this temporary workflow, we could call
make dev
, remove Pydantic, install Pydantic v2`, and run Parser's unit tests.Nox's benefit is that it's more stable, easier to reason, and it will lead us to address an unrelated area we aren't testing today -- tests w/o optional dependencies (e.g., Batch without parser code).
No response
Alternative solutions
No response
Acknowledgment
The text was updated successfully, but these errors were encountered: