8000 Only fetch `__pydantic_core_schema__` from the current class during schema generation by Viicos · Pull Request #10518 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@Viicos
Copy link
Member
@Viicos Viicos commented Sep 30, 2024

In some cases, the core schema of a class is not the right one:

from pydantic.dataclasses import dataclass as pd_dc

from dataclasses import dataclass as stdlib_dc

@pd_dc
class A:
    a: int

@stdlib_dc
class B(A):
    b: str

B.__pydantic_core_schema__
#> Core schema of `A`

Until now, we had the existing_schema.get('cls', None) is obj check to avoid incorrectly using the core schema in this case. However, this doesn't work if the core schema is of type definitions, in which case existing_schema.get('cls', None) is obj is False even though it is the correct schema. This was found during the ns refactor.

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 Sep 30, 2024
)
elif (
(existing_schema := getattr(obj, '__pydantic_core_schema__', None)) is not None
hasattr(obj, '__dict__')
Copy link
Member Author

Choose a reason for hiding this comment

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

We could also check for __slots__ but there's no such type with a __pydantic_core_schema__ property as of today, so I'm keeping things simple here by only checking for __dict__

@Viicos Viicos force-pushed the gen-schema-from-property-dict branch from 3f853bb to 8dff26d Compare September 30, 2024 15:00
@cloudflare-workers-and-pages
Copy link
cloudflare-workers-and-pages bot commented Sep 30, 2024

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8dff26d
Status: ✅  Deploy successful!
Preview URL: https://1988b6bf.pydantic-docs.pages.dev
Branch Preview URL: https://gen-schema-from-property-dic.pydantic-docs.pages.dev

View logs

@codspeed-hq
Copy link
codspeed-hq bot commented Sep 30, 2024

CodSpeed Performance Report

Merging #10518 will not alter performance

Comparing gen-schema-from-property-dict (8dff26d) with main (01b5929)

Summary

✅ 38 untouched benchmarks

@github-actions
Copy link
Contributor

Coverage report

This PR does not seem to contain any modification to coverable code.

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.

Nice find!

@sydney-runkle sydney-runkle merged commit 4bcbaa1 into main Sep 30, 2024
@sydney-runkle sydney-runkle deleted the gen-schema-from-property-dict branch September 30, 2024 16:39
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.

3 participants

0