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 generic models fail to generate schema with typing-extensions 4.12 TypeVar #9499

Closed
1 task done
strangemonad opened this issue May 25, 2024 · 5 comments
Closed
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@strangemonad
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

typing-extensions 4.12 adds support for PEP696 default type params.

If a generic model uses typing extensions 4.12 TypeVar, the model class fails to be created.

Related to #9426. Consider preventing typing-extensions 4.12 (capped to 4.11) in older pydantic dot-releases

Example Code

from typing import Generic

import typing_extensions
from pydantic import BaseModel

T = typing_extensions.TypeVar("T")


class MyModel(BaseModel, Generic[T]):
    a: T



```toml
[tool.poetry]
name = "repro"
version = "0.1.0"
description = ""
authors = ["Shawn Morel <shawn@instance.bio>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.8"
pydantic = ">2.7"
typing-extensions = "4.11.0"
# 4.11 works since it doesn't have an implementation of NoDefault
# typing-extensions = "4.11.0"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"


### Python, Pydantic & OS Version

```Text
pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=true
                 install path: /Users/shawn/Code/instance-bio/instance/repro/.venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]
                     platform: macOS-14.4.1-arm64-arm-64bit
             related packages: typing_extensions-4.11.0
                       commit: unknown
@strangemonad strangemonad added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 25, 2024
@sydney-runkle
Copy link
Member

Hmm, what error are you getting @strangemonad? I'm not seeing an error with my local testing...

@strangemonad
Copy link
Author

@sydney-runkle it might be that #2426 (released in pydantic 2.7.2?) indirectly fixes the issue? I don't think this specific case was added to any tests in #2426 and it might be good to have some sort of test that's able to exhaustively run through all or most types in typing extensions to catch these automatically or fail if there are new types in a new version of typing extensions that aren't tested yet?

with

  • pydantic<=2.7.1
  • typing-extensions==4.12.0

I get the following

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/shawn/Code/instance-bio/instance/services/gen/repro/__init__.py", line 9, in <module>
    class MyModel(BaseModel, Generic[T]):
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 202, in __new__
    complete_model_class(
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 539, in complete_model_class
    schema = cls.__get_pydantic_core_schema__(cls, handler)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/main.py", line 607, in __get_pydantic_core_schema__
    return handler(source)
           ^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
    schema = self._handler(source_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 499, in generate_schema
    schema = self._generate_schema_inner(obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 750, in _generate_schema_inner
    return self._model_schema(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 577, in _model_schema
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 577, in <dictcomp>
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 913, in _generate_md_field_schema
    common_field = self._common_field_schema(name, field_info, decorators)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 1078, in _common_field_schema
    schema = self._apply_annotations(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 1817, in _apply_annotations
    schema = get_inner_schema(source_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
    schema = self._handler(source_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 1798, in inner_handler
    schema = self._generate_schema_inner(obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 755, in _generate_schema_inner
    return self.match_type(obj)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 814, in match_type
    return self._unsubstituted_typevar_schema(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 1664, in _unsubstituted_typevar_schema
    return self.generate_schema(default)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 499, in generate_schema
    schema = self._generate_schema_inner(obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 755, in _generate_schema_inner
    return self.match_type(obj)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 841, in match_type
    return self._unknown_type_schema(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/shawn/Code/instance-bio/instance/services/gen/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 402, in _unknown_type_schema
    raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for typing_extensions.NoDefault. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.

If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.

For further information visit https://errors.pydantic.dev/2.7/u/schema-for-unknown-type

@sydney-runkle
Copy link
Member

@strangemonad,

Did you mean to link #2426? I don't think that was released in 2.7.2...

@sydney-runkle
Copy link
Member

Ah, probably #9426 :)

@sydney-runkle
Copy link
Member

Definitely possible. Going to mark this as resolved, as it's no longer an issue in more recent versions. @strangemonad, feel free to open a PR with those additional test cases, I'd be happy to review!

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 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

2 participants