Skip to content

Commit a7d3253

Browse files
authored
Fix allow extra generic (#9193)
1 parent 8aeac1a commit a7d3253

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pydantic/_internal/_generate_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ def _model_schema(self, cls: type[BaseModel]) -> core_schema.CoreSchema:
536536
assert cls.__mro__[0] is cls
537537
assert cls.__mro__[-1] is object
538538
for candidate_cls in cls.__mro__[:-1]:
539-
extras_annotation = candidate_cls.__annotations__.get('__pydantic_extra__', None)
539+
extras_annotation = getattr(candidate_cls, '__annotations__', {}).get('__pydantic_extra__', None)
540540
if extras_annotation is not None:
541541
if isinstance(extras_annotation, str):
542542
extras_annotation = _typing_extra.eval_type_backport(

tests/test_generics.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,3 +2886,11 @@ class FooGeneric(TypedDict, Generic[T]):
28862886
ta_foo_generic = TypeAdapter(FooGeneric[str])
28872887
assert ta_foo_generic.validate_python({'type': 'tomato'}) == {'type': 'tomato'}
28882888
assert ta_foo_generic.validate_python({}) == {}
2889+
2890+
2891+
def test_generic_with_allow_extra():
2892+
T = TypeVar('T')
2893+
2894+
# This used to raise an error related to accessing the __annotations__ attribute of the Generic class
2895+
class AllowExtraGeneric(BaseModel, Generic[T], extra='allow'):
2896+
data: T

0 commit comments

Comments
 (0)