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

Fix issue with recursion error caused by ParamSpec #6923

Merged
merged 3 commits into from Jul 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pydantic/_internal/_generics.py
Expand Up @@ -359,7 +359,7 @@ def has_instance_in_type(type_: Any, isinstance_target: Any) -> bool:

# Handle special case for typehints that can have lists as arguments.
# `typing.Callable[[int, str], int]` is an example for this.
if isinstance(type_, (List, list)):
if isinstance(type_, (List, list)) and not isinstance(type_, typing_extensions.ParamSpec):
if any(has_instance_in_type(element, isinstance_target) for element in type_):
return True

Expand Down
19 changes: 18 additions & 1 deletion tests/test_generics.py
Expand Up @@ -32,7 +32,7 @@
import pytest
from dirty_equals import HasRepr, IsStr
from pydantic_core import CoreSchema, core_schema
from typing_extensions import Annotated, Literal, OrderedDict, TypeVarTuple, Unpack, get_args
from typing_extensions import Annotated, Literal, OrderedDict, ParamSpec, TypeVarTuple, Unpack, get_args

from pydantic import (
BaseModel,
Expand Down Expand Up @@ -2534,3 +2534,20 @@ class Container(BaseModel, Generic[T]):

assert Container[type(None)](value=None).value is None
assert Container[None](value=None).value is None


@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not allow ParamSpec in generics')
def test_paramspec_is_usable():
# This used to cause a recursion error due to `P in P is True`
# This test doesn't actually test that ParamSpec works properly for validation or anything.

P = ParamSpec('P')

class MyGenericParamSpecClass(Generic[P]):
def __init__(self, func: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None:
super().__init__()

class ParamSpecGenericModel(BaseModel, Generic[P]):
my_generic: MyGenericParamSpecClass[P]

model_config = dict(arbitrary_types_allowed=True)