forked from pydantic/pydantic-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_garbage_collection.py
82 lines (60 loc) · 2.14 KB
/
test_garbage_collection.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import gc
import platform
from typing import Any
from weakref import WeakValueDictionary
import pytest
from pydantic_core import SchemaSerializer, SchemaValidator, core_schema
@pytest.mark.xfail(
condition=platform.python_implementation() == 'PyPy', reason='https://foss.heptapod.net/pypy/pypy/-/issues/3899'
)
def test_gc_schema_serializer() -> None:
# test for https://github.com/pydantic/pydantic/issues/5136
class BaseModel:
__schema__: SchemaSerializer
def __init_subclass__(cls) -> None:
cls.__schema__ = SchemaSerializer(
core_schema.model_schema(
cls,
core_schema.typed_dict_schema(
{'x': core_schema.typed_dict_field(core_schema.definition_reference_schema('model'))}
),
ref='model',
)
)
cache: 'WeakValueDictionary[int, Any]' = WeakValueDictionary()
for _ in range(10_000):
class MyModel(BaseModel):
pass
cache[id(MyModel)] = MyModel
del MyModel
gc.collect(0)
gc.collect(1)
gc.collect(2)
assert len(cache) == 0
@pytest.mark.xfail(
condition=platform.python_implementation() == 'PyPy', reason='https://foss.heptapod.net/pypy/pypy/-/issues/3899'
)
def test_gc_schema_validator() -> None:
# test for https://github.com/pydantic/pydantic/issues/5136
class BaseModel:
__validator__: SchemaValidator
def __init_subclass__(cls) -> None:
cls.__validator__ = SchemaValidator(
core_schema.model_schema(
cls,
core_schema.typed_dict_schema(
{'x': core_schema.typed_dict_field(core_schema.definition_reference_schema('model'))}
),
ref='model',
)
)
cache: 'WeakValueDictionary[int, Any]' = WeakValueDictionary()
for _ in range(10_000):
class MyModel(BaseModel):
pass
cache[id(MyModel)] = MyModel
del MyModel
gc.collect(0)
gc.collect(1)
gc.collect(2)
assert len(cache) == 0