Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove apparently-unnecessary model_rebuild #5371

Merged
merged 5 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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