8000 Fix PydanticKnownError user context handling issues by MarkusSintonen · Pull Request #839 · pydantic/pydantic-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix PydanticKnownError user context handling issues #839

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

Merged
merged 5 commits into from
Aug 7, 2023

Conversation

MarkusSintonen
Copy link
Contributor
@MarkusSintonen MarkusSintonen commented Jul 28, 2023

Change Summary

Fixes error context issues with PydanticKnownError which lost user provided error context data and caused types to change from what was given to the context.

Fixing this requires passing error context data as a part of the ErrorType enum. It is not enough to add the context data as a part of the PydanticKnownError as internally this is converted into ErrorType enum. This conversion was lossy for the context data. Now this issue no longer happens. Adding the context as a part of the ErrorType happens via a macro that creates the Enum items. Also creating the utilities for using them and handling the error context data. Basic errors without any predefined fields are used via ErrorTypeDefaults::TheError constants to make it easier to use those (without having to add context: None in cases where error is internally used).

Related issue number

fix #824

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @dmontagu

@codecov
Copy link
codecov bot commented Jul 28, 2023

Codecov Report

Merging #839 (460b4a6) into main (ca1055b) will increase coverage by 0.51%.
The diff coverage is 97.27%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #839      +/-   ##
==========================================
+ Coverage   93.52%   94.04%   +0.51%     
==========================================
  Files         102      102              
  Lines       14609    15024     +415     
  Branches       25       25              
==========================================
+ Hits        13663    14129     +466     
+ Misses        940      889      -51     
  Partials        6        6              
Files Changed Coverage Δ
src/errors/mod.rs 92.30% <ø> (ø)
src/validators/function.rs 90.65% <ø> (ø)
src/validators/list.rs 100.00% <ø> (ø)
src/validators/time.rs 98.41% <ø> (ø)
src/validators/typed_dict.rs 97.39% <ø> (ø)
src/validators/definitions.rs 88.49% <50.00%> (ø)
src/input/shared.rs 95.77% <66.66%> (+0.06%) ⬆️
src/input/return_enums.rs 85.66% <70.58%> (-0.23%) ⬇️
src/validators/url.rs 93.26% <92.59%> (-0.28%) ⬇️
src/input/input_json.rs 91.33% <93.02%> (+0.26%) ⬆️
... and 25 more

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ca1055b...460b4a6. Read the comment docs.

@codspeed-hq
Copy link
codspeed-hq bot commented Jul 28, 2023

CodSpeed Performance Report

Merging #839 will degrade performances by 48.57%

Comparing MarkusSintonen:fix-error-ctx (460b4a6) with main (ca1055b)

Summary

🔥 4 improvements
❌ 7 regressions
✅ 124 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main MarkusSintonen:fix-error-ctx Change
test_complete_core_lax 1.1 ms 1.3 ms -18.19%
test_core_python_fs 235.3 µs 269.9 µs -12.82%
test_core_string_lax_wrong 40 µs 60.7 µs -34.21%
🔥 test_core_python 46 µs 40.4 µs +13.88%
🔥 test_date_from_datetime 38.8 µs 34.1 µs +13.81%
🔥 test_core_future_str 25.4 µs 22.5 µs +12.88%
test_core_python 41.6 µs 46.7 µs -10.89%
test_with_default 36.5 µs 42.5 µs -13.96%
test_int_error 74.1 µs 99.6 µs -25.62%
test_tagged_union_int_keys_python 30.3 µs 59 µs -48.57%
🔥 test_model_exclude_unset_false 65.7 µs 52.4 µs +25.48%

@MarkusSintonen MarkusSintonen changed the title Fix error ctx Fix PydanticKnownError user context handling issues Jul 28, 2023
@MarkusSintonen
Copy link
Contributor Author

please review

@dmontagu
Copy link
Collaborator

Given the performance consequences, I'm going to defer to one of the rust/pydantic-core experts @samuelcolvin @adriangb @davidhewitt

@@ -332,7 +394,7 @@ def test_error_decimal():
e = PydanticKnownError('greater_than', {'gt': Decimal('42.1')})
assert e.message() == 'Input should be greater than 42.1'
assert e.type == 'greater_than'
assert e.context == {'gt': 42.1}
assert e.context == {'gt': Decimal('42.1')}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As a bonus this issue also gets fixed

@MarkusSintonen MarkusSintonen force-pushed the fix-error-ctx branch 2 times, most recently from 26025a3 to 6c3eb23 Compare July 30, 2023 19:21
Copy link
Contributor
@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

This change looks good to me, many thanks.

The performance numbers look like there may be a little bit of (expected) slowdown on error pathways but also a lot of that is just noise from performance instabilities we've had on codspeed recently.

I also defend this change as a correction to behaviour so think we should merge this and if we need to optimize later, that's a problem for the future.

@davidhewitt davidhewitt merged commit e22a2fd into pydantic:main Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PydanticKnownError should not drop custom user error context
5 participants
0