-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpydantic_validator.py
More file actions
31 lines (21 loc) · 967 Bytes
/
pydantic_validator.py
File metadata and controls
31 lines (21 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
""" Pydantic issue https://github.com/pydantic/pydantic/issues/12085 """
import typing
import pydantic
print(pydantic.version.version_info())
COUNTER: int = 0
class Simple(pydantic.BaseModel):
""" The most simple model, no attribute required (but fails in realistic situations as well)"""
model_config = pydantic.ConfigDict(revalidate_instances='never')
@pydantic.model_validator(mode='after')
def not_much(self) -> typing.Self:
""" Does nothing to self, keep global COUNTER to demonstrate we don't even have to use the model """
global COUNTER
COUNTER += 1
return self
class Composed(pydantic.BaseModel):
model_config = pydantic.ConfigDict(revalidate_instances='never')
simple: Simple
simple = Simple()
assert COUNTER == 1, f'model_validator Simple.not_much called: {COUNTER=}' # passes
c = Composed(simple=simple)
assert COUNTER == 1, f'model_validator Simple.not_much called: {COUNTER=}' # fails