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 allow extra generic #9193

Merged
merged 1 commit into from Apr 9, 2024
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/_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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(at least in python 3.9)

class AllowExtraGeneric(BaseModel, Generic[T], extra='allow'):
data: T