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

Include an error message hint for inherited ordering #7124

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -147,6 +147,7 @@ def wrapped_model_post_init(self: BaseModel, __context: Any) -> None:
# If that is the case, and a user hits this, I could imagine it being very helpful
# to have this extra detail in the reported traceback.
error_message += f' (bases={bases})'
error_message += ' Hint: Inherit BaseModel before typing.Generic'
raise TypeError(error_message)

cls.__pydantic_generic_metadata__ = {
Expand Down
24 changes: 24 additions & 0 deletions tests/test_generics.py
Expand Up @@ -2022,6 +2022,30 @@ class B(A[T], Generic[S]):
...


def test_generic_subclass_with_extra_type_with_hint_message():
KE = TypeVar('KE')
KD = TypeVar('KD')
E = TypeVar('E', bound=BaseModel)
D = TypeVar('D')
Q = TypeVar('Q')

class BaseGenericClass(Generic[KE, KD, E, D, Q], BaseModel):
uid: str
name: str
yohanvalencia marked this conversation as resolved.
Show resolved Hide resolved

with pytest.raises(
TypeError,
match=re.escape(
"All parameters must be present on typing.Generic; you should inherit from typing.Generic[~KE, ~KD, ~E, ~D, ~Q] (bases=(<class 'tests.test_generics.test_generic_subclass_with_extra_type_with_hint_message.<locals>.BaseGenericClass'>,)) Hint: Inherit BaseModel before typing.Generic"
),
):

class ChildGenericClass(
BaseGenericClass[KE, KD, E, Dict[str, Any], str],
):
...
yohanvalencia marked this conversation as resolved.
Show resolved Hide resolved


def test_multi_inheritance_generic_binding():
T = TypeVar('T')

Expand Down