Skip to content

Commit

Permalink
Remove apparently-unnecessary model_rebuild (#5371)
Browse files Browse the repository at this point in the history
* Remove apparently-unnecessary model_rebuild

* Remove pprint import

* Remove the autouse-ness of the cache cleanup

* Use clear_cache fixture in a few more places where necessary

* Re-add accidentally-deleted test
  • Loading branch information
dmontagu committed Apr 5, 2023
1 parent d102fd9 commit d1b42d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
7 changes: 0 additions & 7 deletions pydantic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,6 @@ def __class_getitem__(
# Update cache
_generics.set_cached_generic_type(cls, typevar_values, submodel, origin, args)

# Doing the rebuild _after_ populating the cache prevents infinite recursion
submodel.model_rebuild(
force=True,
raise_errors=False,
_parent_namespace_depth=0,
)

return submodel

@classmethod
Expand Down
16 changes: 7 additions & 9 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys
from collections import deque
from enum import Enum, IntEnum
from pprint import pprint
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -56,7 +55,7 @@
from pydantic.decorators import field_validator


@pytest.fixture(autouse=True)
@pytest.fixture()
def clean_cache():
# cleans up _GENERIC_TYPES_CACHE for checking item counts in the cache
_GENERIC_TYPES_CACHE.clear()
Expand Down Expand Up @@ -300,7 +299,7 @@ class Model(BaseModel, Generic[T, S]):
)


def test_cover_cache():
def test_cover_cache(clean_cache):
cache_size = len(_GENERIC_TYPES_CACHE)
T = TypeVar('T')

Expand All @@ -316,7 +315,7 @@ class Model(BaseModel, Generic[T]):
del models


def test_cache_keys_are_hashable():
def test_cache_keys_are_hashable(clean_cache):
cache_size = len(_GENERIC_TYPES_CACHE)
T = TypeVar('T')
C = Callable[[str, Dict[str, Any]], Iterable[str]]
Expand Down Expand Up @@ -348,7 +347,7 @@ class Model(BaseModel):


@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not play nice with PyO3 gc')
def test_caches_get_cleaned_up():
def test_caches_get_cleaned_up(clean_cache):
initial_types_cache_size = len(_GENERIC_TYPES_CACHE)
T = TypeVar('T')

Expand All @@ -375,7 +374,7 @@ class MyType(int):


@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not play nice with PyO3 gc')
def test_caches_get_cleaned_up_with_aliased_parametrized_bases():
def test_caches_get_cleaned_up_with_aliased_parametrized_bases(clean_cache):
types_cache_size = len(_GENERIC_TYPES_CACHE)

def run() -> None: # Run inside nested function to get classes in local vars cleaned also
Expand All @@ -401,7 +400,7 @@ class A(BaseModel, Generic[T1, T2]):
assert len(_GENERIC_TYPES_CACHE) < types_cache_size + _LIMITED_DICT_SIZE


def test_generics_work_with_many_parametrized_base_models():
def test_generics_work_with_many_parametrized_base_models(clean_cache):
cache_size = len(_GENERIC_TYPES_CACHE)
count_create_models = 1000
T = TypeVar('T')
Expand Down Expand Up @@ -1930,7 +1929,7 @@ class B(A, Generic[T]):
{
'input': 'a',
'loc': ('a',),
'msg': 'Input should be a valid integer, unable to parse string as an ' 'integer',
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
}
]
Expand Down Expand Up @@ -2051,7 +2050,6 @@ def test_double_typevar_substitution() -> None:
class GenericPydanticModel(BaseModel, Generic[T]):
x: T = []

pprint(GenericPydanticModel[List[T]].__pydantic_core_schema__)
assert GenericPydanticModel[List[T]](x=[1, 2, 3]).model_dump() == {'x': [1, 2, 3]}


Expand Down

0 comments on commit d1b42d5

Please sign in to comment.