Skip to content

Conversation

Viicos
Copy link
Member

@Viicos Viicos commented Aug 16, 2024

Fixes #9319

After a lot of debugging in the core schema generation internals, I managed to find where the issue was coming from. I'm still unsure why using a Sequence instead of a list leads to the original issue. My best guess is that is it purely a coincidence: Sequence creates a different core schema, and probably it led to a really specific scenario where not having the serialization schema copied would result in a crash later on as the Model1 definition wasn't saved retained (I suspect this issue where the serialization schema isn't copied already happened in other cases, but had no incident).

Change Summary

Related issue number

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Aug 16, 2024
Copy link

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 36c76df
Status: ✅  Deploy successful!
Preview URL: https://30c993b1.pydantic-docs.pages.dev
Branch Preview URL: https://9319.pydantic-docs.pages.dev

View logs

Copy link

codspeed-hq bot commented Aug 16, 2024

CodSpeed Performance Report

Merging #10155 will not alter performance

Comparing 9319 (36c76df) with main (58abc3d)

Summary

✅ 17 untouched benchmarks

Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic/_internal
  _core_utils.py
Project Total  

This report was generated by python-coverage-comment-action

@Viicos Viicos marked this pull request as ready for review August 16, 2024 12:03
Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Great find @Viicos! as mentioned in #9980, I think there might be some other cases where we want to make a change like this... Sequences seem to be a common culprit...

@@ -212,6 +212,7 @@ def _handle_other_schemas(self, schema: core_schema.CoreSchema, f: Walk) -> core
def _handle_ser_schemas(self, ser_schema: core_schema.SerSchema, f: Walk) -> core_schema.SerSchema:
schema: core_schema.CoreSchema | None = ser_schema.get('schema', None)
if schema is not None:
ser_schema = ser_schema.copy()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ser_schema = ser_schema.copy()
ser_schema = smart_deepcopy(ser_schema)

Can we match the approach used in this PR? #9980

Copy link
Member Author

Choose a reason for hiding this comment

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

I would rather stick with .copy here, as it is for the rest of the _WalkSchema implementation. deepcopy on a dict is slower and does not have any impact here, as .copy will create a shallow copy and this is enough (as we are only changing a top level key here, in this case the 'schema' key).

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good!

@sydney-runkle sydney-runkle merged commit 7212484 into main Aug 16, 2024
62 checks passed
@sydney-runkle sydney-runkle deleted the 9319 branch August 16, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KeyError: '__main__.Model1:95063711754448'
2 participants