Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Apr 18, 2024
1 parent cea6a1e commit 466dafc
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion tests/test_garbage_collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gc
import platform
from typing import Any
from typing import Any, Iterable
from weakref import WeakValueDictionary

import pytest
Expand Down Expand Up @@ -79,3 +79,42 @@ class MyModel(BaseModel):
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_validator_iterator() -> None:
# test for https://github.com/pydantic/pydantic/issues/9243
class MyModel:
iter: Iterable[int]

v = SchemaValidator(
core_schema.model_schema(
MyModel,
core_schema.model_fields_schema(
{'iter': core_schema.model_field(core_schema.generator_schema(core_schema.int_schema()))}
),
),
)

class MyIterable:
def __iter__(self):
return self

def __next__(self):
raise StopIteration()

cache: 'WeakValueDictionary[int, Any]' = WeakValueDictionary()

for _ in range(10_000):
iterable = MyIterable()
cache[id(iterable)] = iterable
v.validate_python({'iter': iterable})
del iterable

gc.collect(0)
gc.collect(1)
gc.collect(2)

assert len(cache) == 0

0 comments on commit 466dafc

Please sign in to comment.