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

from __future__ import annotations causes __pydantic_extra__ type annotation error. #8752

Closed
1 task done
KasperZutterman opened this issue Feb 7, 2024 · 1 comment
Closed
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@KasperZutterman
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Importing from __future__ import annotations breaks model definition if it contains __pydantic_extra__ type annotation.

Exception stack trace

Traceback (most recent call last):
  File "/Users/kasperzutterman/repositories/otainsight/transformations/modules/integrations/transformations/pms/tests/hotelkey/test3.py", line 6, in <module>
    class Test(BaseModel):
  File "/Users/kasperzutterman/.pyenv/versions/3.10.12/envs/pydantic/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 183, in __new__
    complete_model_class(
  File "/Users/kasperzutterman/.pyenv/versions/3.10.12/envs/pydantic/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 517, in complete_model_class
    schema = cls.__get_pydantic_core_schema__(cls, handler)
  File "/Users/kasperzutterman/.pyenv/versions/3.10.12/envs/pydantic/lib/python3.10/site-packages/pydantic/main.py", line 584, in __get_pydantic_core_schema__
    return __handler(__source)
  File "/Users/kasperzutterman/.pyenv/versions/3.10.12/envs/pydantic/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
    schema = self._handler(__source_type)
  File "/Users/kasperzutterman/.pyenv/versions/3.10.12/envs/pydantic/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 499, in generate_schema
    schema = self._generate_schema(obj)
  File "/Users/kasperzutterman/.pyenv/versions/3.10.12/envs/pydantic/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 737, in _generate_schema
    schema = self._post_process_generated_schema(self._generate_schema_inner(obj))
  File "/Users/kasperzutterman/.pyenv/versions/3.10.12/envs/pydantic/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 759, in _generate_schema_inner
    return self._model_schema(obj)
  File "/Users/kasperzutterman/.pyenv/versions/3.10.12/envs/pydantic/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 543, in _model_schema
    raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: The type annotation for `__pydantic_extra__` must be `Dict[str, ...]`

Example Code

from __future__ import annotations  # <- removing this line solves the problem

from pydantic import BaseModel, ConfigDict


class Test(BaseModel):
    model_config = ConfigDict(extra='allow')
    __pydantic_extra__: dict[str, str]

Python, Pydantic & OS Version

pydantic version: 2.6.1
        pydantic-core version: 2.16.2
          pydantic-core build: profile=release pgo=true
                 install path: /Users/kasperzutterman/.pyenv/versions/3.12.1/envs/pydantic-3.12/lib/python3.12/site-packages/pydantic
               python version: 3.12.1 (main, Feb  2 2024, 09:13:09) [Clang 15.0.0 (clang-1500.1.0.2.5)]
                     platform: macOS-14.2.1-arm64-arm-64bit
             related packages: typing_extensions-4.9.0
                       commit: unknown
@KasperZutterman KasperZutterman added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 7, 2024
@sydney-runkle
Copy link
Member

@KasperZutterman,

This should be fixed on main, thanks to @alexmojaki's work in #8659.

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