| 387 | @pytest.mark.thread_unsafe(reason='GC is flaky') |
| 388 | @pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not play nice with PyO3 gc') |
| 389 | def test_caches_get_cleaned_up(mocker: MockerFixture): |
| 390 | cache = mocker.patch('pydantic._internal._generics._GENERIC_TYPES_CACHE', GenericTypesCache()) |
| 391 | initial_types_cache_size = len(cache) |
| 392 | T = TypeVar('T') |
| 393 | |
| 394 | class MyGenericModel(BaseModel, Generic[T]): |
| 395 | x: T |
| 396 | |
| 397 | model_config = dict(arbitrary_types_allowed=True) |
| 398 | |
| 399 | n_types = 200 |
| 400 | types = [] |
| 401 | for i in range(n_types): |
| 402 | |
| 403 | class MyType(int): |
| 404 | pass |
| 405 | |
| 406 | types.append(MyGenericModel[MyType]) # retain a reference |
| 407 | |
| 408 | assert len(cache) == initial_types_cache_size + 3 * n_types |
| 409 | types.clear() |
| 410 | gc.collect(0) |
| 411 | gc.collect(1) |
| 412 | gc.collect(2) |
| 413 | assert len(cache) < initial_types_cache_size + _LIMITED_DICT_SIZE |
| 414 | |
| 415 | |
| 416 | @pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not play nice with PyO3 gc') |