E5DB 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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
10BC0
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pydantic/_internal/_generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,12 @@ def _generate_schema_from_property(self, obj: Any, source: Any) -> core_schema.C
source, CallbackGetCoreSchemaHandler(self._generate_schema_inner, self, ref_mode=ref_mode)
)
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__

# In some cases (e.g. a stdlib dataclass subclassing a Pydantic dataclass),
# doing an attribute access to get the schema will result in the parent schema
# being fetched. Thus, only look for the current obj's dict:
and (existing_schema := obj.__dict__.get('__pydantic_core_schema__')) is not None
and not isinstance(existing_schema, MockCoreSchema)
and existing_schema.get('cls', None) is obj
):
schema = existing_schema
elif (validators := getattr(obj, '__get_validators__', None)) is not None:
Expand Down
0