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

Generic Models cannot be typed with BaseModel subclasses #6931

Closed
1 task done
hblunck opened this issue Jul 28, 2023 · 1 comment · Fixed by #6933
Closed
1 task done

Generic Models cannot be typed with BaseModel subclasses #6931

hblunck opened this issue Jul 28, 2023 · 1 comment · Fixed by #6933
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@hblunck
Copy link

hblunck commented Jul 28, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I have created a generic class. Now I want to create a concrete subclass passing pydantic's BaseModel (or any class derived from it) as "placeholder". Upon creating the class (no instantiation required), pydantic throws an error.

Last entry in the call stack is:

File ~\<OMITTED>\pydantic\_internal\_model_construction.py:205, in ModelMetaclass.__getattr__(self, item)
    202         if rebuilt_validator is not None:
    203             # In this case, a validator was built, and so `__pydantic_core_schema__` should now be set
    204             return getattr(self, '__pydantic_core_schema__')
--> 205 raise AttributeError(item)

Raising

AttributeError: pydantic_generic_metadata

Example Code

from typing import TypeVar, Generic
from pydantic import BaseModel

T = TypeVar("T")

class SimpleGenericModel(
    BaseModel, Generic[T]
):
    pass

class Concrete(
    SimpleGenericModel[BaseModel]
):
    pass

Python, Pydantic & OS Version

pydantic version: 2.1.1
        pydantic-core version: 2.4.0
          pydantic-core build: profile=release pgo=false mimalloc=true
                 install path: <OMITTED>
               python version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 05:59:00) [MSC v.1929 64 bit (AMD64)]
                     platform: Windows-10-10.0.19045-SP0
     optional deps. installed: ['typing-extensions']

Selected Assignee: @lig

@hblunck hblunck added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Jul 28, 2023
@dmontagu dmontagu removed the unconfirmed Bug not yet confirmed as valid/applicable label Jul 28, 2023
@dmontagu dmontagu assigned dmontagu and unassigned lig Jul 28, 2023
@dmontagu
Copy link
Contributor

Thanks for reporting! Should be fixed by #6933.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants