From a7d32534779a1fd2ea5d10cbb0eed6db5584149e Mon Sep 17 00:00:00 2001 From: David Montague <35119617+dmontagu@users.noreply.github.com> Date: Tue, 9 Apr 2024 08:23:18 -0600 Subject: [PATCH] Fix allow extra generic (#9193) --- pydantic/_internal/_generate_schema.py | 2 +- tests/test_generics.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pydantic/_internal/_generate_schema.py b/pydantic/_internal/_generate_schema.py index e07e68f6ace..debff49cb65 100644 --- a/pydantic/_internal/_generate_schema.py +++ b/pydantic/_internal/_generate_schema.py @@ -536,7 +536,7 @@ def _model_schema(self, cls: type[BaseModel]) -> core_schema.CoreSchema: assert cls.__mro__[0] is cls assert cls.__mro__[-1] is object for candidate_cls in cls.__mro__[:-1]: - extras_annotation = candidate_cls.__annotations__.get('__pydantic_extra__', None) + extras_annotation = getattr(candidate_cls, '__annotations__', {}).get('__pydantic_extra__', None) if extras_annotation is not None: if isinstance(extras_annotation, str): extras_annotation = _typing_extra.eval_type_backport( diff --git a/tests/test_generics.py b/tests/test_generics.py index d95e1bbbe03..2ff8f42e750 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -2886,3 +2886,11 @@ class FooGeneric(TypedDict, Generic[T]): ta_foo_generic = TypeAdapter(FooGeneric[str]) assert ta_foo_generic.validate_python({'type': 'tomato'}) == {'type': 'tomato'} assert ta_foo_generic.validate_python({}) == {} + + +def test_generic_with_allow_extra(): + T = TypeVar('T') + + # This used to raise an error related to accessing the __annotations__ attribute of the Generic class + class AllowExtraGeneric(BaseModel, Generic[T], extra='allow'): + data: T