Type annotations on dataclass base class cannot be resolved if future annotations are used #8730
Closed
1 task done
Labels
bug V2
Bug related to Pydantic V2
Initial Checks
Description
When the base class of a dataclass has an annotation type that is not imported by the module the subclass is in, and
from __future__ import annotations
is in use (or in my case, Cython is in use), an error occurs (full traceback from the example code below).The root cause is that when pydantic builds up the type namespace, it does not consider base classes (https://github.com/pydantic/pydantic/blob/main/pydantic/_internal/_generate_schema.py#L1474). This is fine for models, because each base class builds a schema immediately, but does not work for plain stdlib dataclasses. One solution would be to iterate though all of the base classes of the dataclass that are also dataclasses, and push their namespaces as well. For example:
I can make this into a PR if it looks reasonable.
Full traceback for the example
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: