8000 Use return_schema for computed fields by dmontagu · Pull Request #595 · pydantic/pydantic-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use return_schema for computed fields #595

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 2 commits into from
May 9, 2023
Merged

Conversation

dmontagu
Copy link
Collaborator
@dmontagu dmontagu commented May 7, 2023

This change provides a way to specify a serialization schema for computed fields (previously, they were treated as Any, which would lead to the FastAPI security problem in pydantic). This is also useful for generating JSON schemas for computed fields.

The pydantic-side PR is pydantic/pydantic#5708 (note that that PR depends on pydantic/pydantic#5707, which did not require changes to pydantic-core).

Selected Reviewer: @samuelcolvin

@codecov-commenter
Copy link
codecov-commenter commented May 7, 2023

Codecov Report

Merging #595 (5957f4f) into main (0d57243) will increase coverage by 0.01%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #595      +/-   ##
==========================================
+ Coverage   94.19%   94.21%   +0.01%     
==========================================
  Files          96       96              
  Lines       13099    13114      +15     
  Branches       24       24              
==========================================
+ Hits        12339    12355      +16     
+ Misses        754      753       -1     
  Partials        6        6              
Impacted Files Coverage Δ
pydantic_core/core_schema.py 96.96% <100.00%> (+<0.01%) ⬆️
src/serializers/computed_fields.rs 98.26% <100.00%> (+1.20%) ⬆️
src/serializers/type_serializers/dataclass.rs 97.05% <100.00%> (ø)
src/serializers/type_serializers/model.rs 93.96% <100.00%> (ø)
src/serializers/type_serializers/typed_dict.rs 96.87% <100.00%> (ø)

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 0d57243...5957f4f. Read the comment docs.

@codspeed-hq
Copy link
codspeed-hq bot commented May 7, 2023

CodSpeed Performance Report

Merging #595 computed-fields-return-schema (5957f4f) will not alter performances.

Summary

🔥 0 improvements
❌ 0 regressions
✅ 115 untouched benchmarks

🆕 0 new benchmarks
⁉️ 0 dropped benchmarks

@dmontagu
Copy link
Collaborator Author
dmontagu commented May 7, 2023

please review

Copy link
Member
@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

LGTM.

My only suggested would be that we should replace all json_return_type usage with this.

@samuelcolvin
Copy link
Member

please update - just to reassign, feel free to merge, or do more here.

@dmontagu
Copy link
Collaborator Author
dmontagu commented May 8, 2023

I'll note that those are only present in PlainSerializerFunctionSerSchema and WrapSerializerFunctionSerSchema.

I think given that relates to SerSchema, where I think there is perhaps a bigger question of how to best handle interaction with more arbitrary serialization, I'll create a separate issue for that before merging this, rather than doing more work here.

@@ -415,23 +415,25 @@ def model_ser_schema(cls: Type[Any], schema: CoreSchema) -> ModelSerSchema:
class ComputedField(TypedDict, total=False):
type: Required[Literal['computed-field']]
property_name: Required[str]
json_return_type: JsonReturnTypes
return_schema: Required[CoreSchema]
Copy link
Member

Choose a reason for hiding this comment

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

I assume we need to change Pydantic to generate this from a return type annotation (which we'll require)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dmontagu dmontagu merged commit 027e679 into main May 9, 2023
@dmontagu dmontagu deleted the computed-fields-return-schema branch May 9, 2023 13:32
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.

4 participants
0