File tree Expand file tree Collapse file tree 1 file changed +40
-1
lines changed
Expand file tree Collapse file tree 1 file changed +40
-1
lines changed Original file line number Diff line number Diff line change 11import gc
22import platform
3- from typing import Any
3+ from typing import Any , Iterable
44from weakref import WeakValueDictionary
55
66import pytest
@@ -79,3 +79,42 @@ class MyModel(BaseModel):
7979 gc .collect (2 )
8080
8181 assert len (cache ) == 0
82+
83+
84+ @pytest .mark .xfail (
85+ condition = platform .python_implementation () == 'PyPy' , reason = 'https://foss.heptapod.net/pypy/pypy/-/issues/3899'
86+ )
87+ def test_gc_validator_iterator () -> None :
88+ # test for https://github.com/pydantic/pydantic/issues/9243
89+ class MyModel :
90+ iter : Iterable [int ]
91+
92+ v = SchemaValidator (
93+ core_schema .model_schema (
94+ MyModel ,
95+ core_schema .model_fields_schema (
96+ {'iter' : core_schema .model_field (core_schema .generator_schema (core_schema .int_schema ()))}
97+ ),
98+ ),
99+ )
100+
101+ class MyIterable :
102+ def __iter__ (self ):
103+ return self
104+
105+ def __next__ (self ):
106+ raise StopIteration ()
107+
108+ cache : 'WeakValueDictionary[int, Any]' = WeakValueDictionary ()
109+
110+ for _ in range (10_000 ):
111+ iterable = MyIterable ()
112+ cache [id (iterable )] = iterable
113+ v .validate_python ({'iter' : iterable })
114+ del iterable
115+
116+ gc .collect (0 )
117+ gc .collect (1 )
118+ gc .collect (2 )
119+
120+ assert len (cache ) == 0
You can’t perform that action at this time.
0 commit comments