-
Notifications
You must be signed in to change notification settings - Fork 247
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
Raise warnings when deprecated fields are filled at model instantiation #1551
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1551 +/- ##
==========================================
- Coverage 90.21% 89.62% -0.59%
==========================================
Files 106 112 +6
Lines 16339 17892 +1553
Branches 36 40 +4
==========================================
+ Hits 14740 16036 +1296
- Misses 1592 1836 +244
- Partials 7 20 +13
... and 54 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
CodSpeed Performance ReportMerging #1551 will not alter performanceComparing Summary
|
Please review. Meanwhile, I'd like to ask the reviewer's opinion on how we can make this thing "less slow". Since this implementation calls Since we know that performance will definitely be impacted, we can try to mitigate this. For instance, instead of calling import each time a deprecated field is filled, we can collect warnings and raise them all together in one warning at the end. There are some issues with this approach though. First, the final warning message will need to be more specific because all deprecation messages will be mixed up. We will need to indicate which fields are deprecated and which message belongs to which field for clarity, but this will be inconsistent with what is implemented in pydantic now, i.e. print the deprecation warning only without any additional information. Maybe we can just implement that here and change pydantic later on. Another issue is when that final warning should be raised. Currently |
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.
This looks reasonable to me, we can get access to the warning type via the PyO3 API and let that handle the most efficient way to do it 😄
src/validators/model_fields.rs
Outdated
@@ -184,6 +186,10 @@ impl Validator for ModelFieldsValidator { | |||
// extra logic either way | |||
used_keys.insert(lookup_path.first_key()); | |||
} | |||
if let Some(msg) = &field.deprecation_msg { | |||
let deprecation_warning_type = py.import_bound("builtins")?.getattr("DeprecationWarning")?; |
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.
Try this instead:
let deprecation_warning_type = py.import_bound("builtins")?.getattr("DeprecationWarning")?; | |
let deprecation_warning_type = pyo3::exceptions::PyDeprecationWarning::type_object_bound(py); |
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.
Updated. This works. Thanks! There's another UserWarning
imported in the same way somewhere in the code base. I can create another PR to change that.
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.
That would be a great follow-up, yes please 👍
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.
Thanks!
@Viicos if you're happy with the core schema & test then let's merge this, the Rust code LGTM.
Change Summary
Raise warnings when deprecated fields receive values at model instantiation. My previous attempt at pydantic/pydantic#10865 was rejected because it led to performance issues, so I'm hoping that by moving the logic to pydantic-core will work better. At least doing it here means that the code won't need to loop over the input values one more time.
One extra config is added to the
model-field
schema:deprecation_msg
(Option<String>
). A field is marked as deprecated when this config value is present. The actual deprecation message will be passed in from pydantic; this keeps handling the type of the deprecation message simple, and most importantly, there is already logic over there dealing with possible cases of deprecation messages and thus we should not duplicate the logic here.Also tested with pydantic and it worked.
Related issue number
Part of pydantic/pydantic#8922
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @sydney-runkle