8000 Do not delete mock validator/serializer in `model_rebuild()` · pydantic/pydantic@5aefad8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5aefad8

Browse files
committed
Do not delete mock validator/serializer in model_rebuild()
Backport of: #11890
1 parent 8fbe658 commit 5aefad8

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pydantic/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,11 @@ def model_rebuild(
617617
return None
618618

619619
for attr in ('__pydantic_core_schema__', '__pydantic_validator__', '__pydantic_serializer__'):
620-
if attr in cls.__dict__:
620+
if attr in cls.__dict__ and not isinstance(getattr(cls, attr), _mock_val_ser.MockValSer):
621621
# Deleting the validator/serializer is necessary as otherwise they can get reused in
622-
# pydantic-core. Same applies for the core schema that can be reused in schema generation.
622+
# pydantic-core. We do so only if they aren't mock instances, otherwise — as `model_rebuild()`
623+
# isn't thread-safe — concurrent model instantiations can lead to the parent validator being used.
624+
# Same applies for the core schema that can be reused in schema generation.
623625
delattr(cls, attr)
624626

625627
cls.__pydantic_complete__ = False

0 commit comments

Comments
 (0)
0