so1n / protobuf_to_pydantic

Generate a pydantic.BaseModel with parameter verification function from the Python Message object(by the Protobuf file).
Apache License 2.0
80 stars 6 forks source link

ImportError: cannot import name 'PydanticGeneralMetadata' from 'pydantic._internal._fields' with latest pydantic #29

Closed jankatins closed 10 months ago

jankatins commented 10 months ago

Describe the bug

After udating to the latest pydantic version, I get this:

cd ./build/proto && poetry run python -m grpc_tools.protoc -I. --protobuf-to-pydantic_out=. example_model/v1/classes.proto
/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/grpc_tools/protoc.py:21: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  import pkg_resources
Traceback (most recent call last):
  File "/Users/jankatins/projects/project1/.venv/bin/protoc-gen-protobuf-to-pydantic", line 5, in <module>
    from protobuf_to_pydantic.plugin.main import main
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/__init__.py", line 2, in <module>
    from .gen_code import pydantic_model_to_py_code, pydantic_model_to_py_file
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/gen_code.py", line 33, in <module>
    from protobuf_to_pydantic import _pydantic_adapter, customer_validator, gen_model
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/gen_model.py", line 18, in <module>
    from protobuf_to_pydantic.get_desc import (
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/get_desc/__init__.py", line 1, in <module>
    from .from_pb_option import get_desc_from_p2p, get_desc_from_pgv
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/get_desc/from_pb_option/__init__.py", line 1, in <module>
    from .from_p2p import get_desc_from_p2p
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/get_desc/from_pb_option/from_p2p.py", line 5, in <module>
    from .base import DescFromOptionTypedDict, ParseFromPbOption
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/get_desc/from_pb_option/base.py", line 7, in <module>
    from protobuf_to_pydantic.customer_con_type import (
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/customer_con_type/__init__.py", line 6, in <module>
    from .v2 import *  # noqa
    ^^^^^^^^^^^^^^^^^
  File "/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/protobuf_to_pydantic/customer_con_type/v2.py", line 6, in <module>
    from pydantic._internal._fields import PydanticGeneralMetadata
ImportError: cannot import name 'PydanticGeneralMetadata' from 'pydantic._internal._fields' (/Users/jankatins/projects/project1/.venv/lib/python3.11/site-packages/pydantic/_internal/_fields.py)
--protobuf-to-pydantic_out: protoc-gen-protobuf-to-pydantic: Plugin failed with status code 1.

To Reproduce

Install the following versions:

λ  poetry show
...
grpcio               1.59.3    HTTP/2-based RPC framework
grpcio-tools         1.59.3    Protobuf code generator for gRPC
mypy                 1.7.1     Optional static typing for Python
mypy-extensions      1.0.0     Type system extensions for programs checked with the mypy type checker.
mypy-protobuf        3.5.0     Generate mypy stub files from protobuf specs
protobuf             4.25.1
protobuf-to-pydantic 0.2.1     Generate the `pydantic.BaseModel` class (and the corresponding source code) with parameter verification function through the Protobuf file
pydantic             2.5.2     Data validation using Python type hints
pydantic-core        2.14.5
types-protobuf       4.24.0.4  Typing stubs for protobuf

Expected behavior

This should work (and it did work with older versions) :-) -> will downgrade pydantic for now...

so1n commented 10 months ago

Thank you for your feedback, in order to minimize the amount of code generated I opted for an internal method. This caused a version incompatibility issue that I have now resolved. Internal methods will no longer be used from version 0.3 onwards.