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

Python incompatibility with pydantic 1.8 #1601

Closed
adaamz opened this issue Oct 5, 2023 · 2 comments · Fixed by #1603
Closed

Python incompatibility with pydantic 1.8 #1601

adaamz opened this issue Oct 5, 2023 · 2 comments · Fixed by #1603

Comments

@adaamz
Copy link
Contributor

adaamz commented Oct 5, 2023

Describe the bug
Looks like datamodel-code-generator >=0.14.0 is not compatible with pydantic 1.8.

To Reproduce

pip install datamodel-code-generator==0.22.0 pydantic==1.8.2
datamodel-codegen --input example.yml --input-file-type=openapi --output example.py --output-model-type="pydantic.BaseModel"

Example schema:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Example
paths:
  asdf:
    parameters:
      - $ref: "#/components/schemas/asdf"

components:
  schemas:
    asdf:
      type: object
      required:
        - example_id
      properties:
        example_id:
          type: string

Used commandline:

pip install datamodel-code-generator==0.22.0 pydantic==1.8.2
datamodel-codegen --input example.yml --input-file-type=openapi --output example.py --output-model-type="pydantic.BaseModel"

Expected behavior
No error or impossible to install with old version of Pydantic.

Current behavior

Traceback (most recent call last):
  File "/home/adam/.pyenv/versions/3.9.13/bin/datamodel-codegen", line 5, in <module>
    from datamodel_code_generator.__main__ import main
  File "/home/adam/.pyenv/versions/3.9.13/lib/python3.9/site-packages/datamodel_code_generator/__init__.py", line 35, in <module>
    from datamodel_code_generator.parser.base import Parser
  File "/home/adam/.pyenv/versions/3.9.13/lib/python3.9/site-packages/datamodel_code_generator/parser/base.py", line 30, in <module>
    from datamodel_code_generator.model import pydantic as pydantic_model
  File "/home/adam/.pyenv/versions/3.9.13/lib/python3.9/site-packages/datamodel_code_generator/model/__init__.py", line 5, in <module>
    from ..types import DataTypeManager as DataTypeManagerABC
  File "/home/adam/.pyenv/versions/3.9.13/lib/python3.9/site-packages/datamodel_code_generator/types.py", line 54, in <module>
    from datamodel_code_generator.reference import Reference, _BaseModel
  File "/home/adam/.pyenv/versions/3.9.13/lib/python3.9/site-packages/datamodel_code_generator/reference.py", line 50, in <module>
    class _BaseModel(BaseModel):
  File "pydantic/main.py", line 282, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/typing.py", line 287, in pydantic.typing.resolve_annotations
    For use of globalns and localns see the docstring for get_type_hints().
  File "/home/adam/.pyenv/versions/3.9.13/lib/python3.9/typing.py", line 292, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/home/adam/.pyenv/versions/3.9.13/lib/python3.9/typing.py", line 553, in _evaluate
    type_ = _type_check(
  File "/home/adam/.pyenv/versions/3.9.13/lib/python3.9/typing.py", line 158, in _type_check
    raise TypeError(f"{arg} is not valid as type argument")
TypeError: typing.ClassVar[typing.Set[str]] is not valid as type argument

Version:

  • OS: linux mint
  • Python version: 3.9.13
  • datamodel-code-generator version: 0.14.0 - 0.22.0
@koxudaxi koxudaxi added bug Something isn't working and removed bug Something isn't working labels Oct 5, 2023
@koxudaxi
Copy link
Owner

koxudaxi commented Oct 6, 2023

Thank you for creating an issue.
I think this is not a bug in datamodel-code-generator.
The bug was fixed in pydantic==1.9.0
pydantic/pydantic#3401

We should remove from __future__ import annotations if a model has typing.ClassVar.
Also, we need to change lazy loading types to string.

@koxudaxi
Copy link
Owner

koxudaxi commented Oct 6, 2023

I got another bug.
https://github.com/koxudaxi/datamodel-code-generator/actions/runs/6427909476/job/17454257906
pydantic/pydantic#4011

It's diffcult to support old pydantic verions :(

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

Successfully merging a pull request may close this issue.

2 participants