Skip to content

Commit

Permalink
Fix allow extra generic (#9193)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Apr 9, 2024
1 parent 8aeac1a commit a7d3253
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pydantic/_internal/_generate_schema.py
Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions tests/test_generics.py
Expand Up @@ -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

0 comments on commit a7d3253

Please sign in to comment.