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

pydantic_core._pydantic_core.SchemaError: Definitions error: definition XYZ was never filled #8984

Open
1 task done
multimeric opened this issue Mar 11, 2024 · 8 comments
Labels
bug V2 Bug related to Pydantic V2

Comments

@multimeric
Copy link
Contributor

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

It seems that a generic type alias that is later "filled" via a second type alias confuses Pydantic. If you use my_int_seq: MySeq[int] then this error doesn't occur, it's only when the second alias exists that this causes an error.

Full stack trace:

Traceback (most recent call last):
  File "/Users/milton.m/Programming/cyton-solver/backend/test_model.py", line 7, in <module>
    class MyModel(BaseModel):
  File "/Users/milton.m/Programming/cyton-solver/venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 183, in __new__
    complete_model_class(
  File "/Users/milton.m/Programming/cyton-solver/venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 535, in complete_model_class
    cls.__pydantic_validator__ = create_schema_validator(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/milton.m/Programming/cyton-solver/venv/lib/python3.12/site-packages/pydantic/plugin/_schema_validator.py", line 49, in create_schema_validator
    return SchemaValidator(schema, config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.SchemaError: Definitions error: definition `__main__.MySeq:4304815984[int:4316993544]` was never filled

Example Code

from pydantic import BaseModel
from typing import Sequence

type MySeq[T] = Sequence[T]
type MyIntSeq = MySeq[int]

class MyModel(BaseModel):
    my_int_seq: MyIntSeq

Python, Pydantic & OS Version

pydantic version: 2.6.3
        pydantic-core version: 2.16.3
          pydantic-core build: profile=release pgo=true
                 install path: /Users/milton.m/Programming/cyton-solver/venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.2 (main, Feb 20 2024, 04:06:49) [Clang 14.0.0 (clang-1400.0.29.202)]
                     platform: macOS-12.6-arm64-arm-64bit
             related packages: fastapi-0.105.0 typing_extensions-4.10.0
                       commit: unknown
@multimeric multimeric added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Mar 11, 2024
@samuelcolvin
Copy link
Member

Maybe I'm being dumb, but I don't think your code is valid python.

@multimeric
Copy link
Contributor Author

multimeric commented Mar 11, 2024

Why do you think that? Python 3.12 will execute the code if I use a dataclass instead of BaseModel, and pyright finds no issue with the typing. It's true that I can't find any example of "filling" in a generic as in type MyIntSeq = MySeq[int] in PEP 695, but it follows the same syntax as is used for generic classes so I assume it's okay?

@davidhewitt
Copy link
Contributor

https://docs.python.org/3/library/typing.html#type-aliases - type statement new in 3.12

@dmontagu
Copy link
Contributor

I believe this is a bug in our handling of nested type aliases. Definitely intend to fix it, not sure how complicated it will be..

@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Mar 12, 2024
@tamird
Copy link

tamird commented Mar 21, 2024

I'm also seeing this, even without the use of a type alias. Inheriting from a generic class produces the same explosion. Something like:

from pydantic import BaseModel
from typing import Generic, TypeVar

_T = TypeVar("_T", MyType0, MyType1)

@dataclass
class Inner(Generic[_T]):
  field0: _T
  field1: _T

@dataclass
class InnerMyType0(Inner[MyType0]):
  pass

class Outer(BaseModel):
  inner: InnerMyType0

throws something like

E   pydantic_core._pydantic_core.SchemaError: Definitions error: definition `Inner:6262921200[MyType0:6266707744]` was never filled

The reason I need this alias is that my OpenAPI schema generator incorrectly renders the generic (even when the type arguments are substituted) as polymorphic, and the added class was hoped to work around the problem.

@mbeissinger
Copy link

I'm running into this issue as well -- does anyone have a workaround?

@mark-henry01
Copy link

Here is another example:

from collections.abc import Sequence
from pydantic import TypeAdapter

type JSON = str | int | bool | JSONSeq | JSONObj | None
type JSONObj = dict[str, JSON]
type JSONSeq = list[JSON]
type MyJSONAlias = JSON
# MyJSONAlias = JSON
type JSONs = Sequence[MyJSONAlias]

adapter = TypeAdapter(JSONs)

causes

Traceback (most recent call last):
File "/path/to/proj/.venv/lib64/python3.12/site-packages/pydantic/type_adapter.py", line 217, in init
validator = _getattr_no_parents(type, 'pydantic_validator')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/proj/.venv/lib64/python3.12/site-packages/pydantic/type_adapter.py", line 98, in _getattr_no_parents
raise AttributeError(attribute)
AttributeError: pydantic_validator

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/path/to/proj/main.py", line 14, in
adapter = TypeAdapter(JSONs)
^^^^^^^^^^^^^^^^^^
File "/path/to/proj/.venv/lib64/python3.12/site-packages/pydantic/type_adapter.py", line 222, in init
validator = create_schema_validator(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/proj/.venv/lib64/python3.12/site-packages/pydantic/plugin/_schema_validator.py", line 49, in create_schema_validator
return SchemaValidator(schema, config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.SchemaError: Definitions error: definition __main__.JSON:1234 was never filled

If I replace type MyJSONAlias = JSON by MyJSONAlias = JSON, then everything works. So I guess this is related to Python 3.12 type aliases and their lazy evaluation?

@jonathan-laurent
Copy link

Here is another example, which does not involve generics (tested with pydantic v2.8.2 and Python 3.12.2):

from dataclasses import dataclass
import pydantic

type Vars = list[str]
type Expr = str
type Fun = tuple[Vars, Expr]
type IntPred = Fun

@dataclass
class ConjectureExpr:
    property: IntPred

# Error: pydantic_core._pydantic_core.SchemaError: Definitions error:
# definition `__main__.Fun:4380830240` was never filled
pydantic.TypeAdapter(ConjectureExpr)

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

No branches or pull requests

9 participants