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

RecursionError: maximum recursion depth exceeded in comparison in _abc_instancecheck(cls, instance) #57

Closed tirth7777777 closed 2 months ago

tirth7777777 commented 3 months ago

Describe the bug I got the error

    _content_tuple: Optional[Tuple[str, str]] = self._message_field_handle(
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 200, in _message_field_handle
    self._message(message, root_desc, [FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER])
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 487, in _message
    _content_tuple: Optional[Tuple[str, str]] = self._message_field_handle(
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 200, in _message_field_handle
    self._message(message, root_desc, [FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER])
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 487, in _message
    _content_tuple: Optional[Tuple[str, str]] = self._message_field_handle(
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 200, in _message_field_handle
    self._message(message, root_desc, [FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER])
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 487, in _message
    _content_tuple: Optional[Tuple[str, str]] = self._message_field_handle(
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 200, in _message_field_handle
    self._message(message, root_desc, [FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER])
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 487, in _message
    _content_tuple: Optional[Tuple[str, str]] = self._message_field_handle(
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 200, in _message_field_handle
    self._message(message, root_desc, [FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER])
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 487, in _message
    _content_tuple: Optional[Tuple[str, str]] = self._message_field_handle(
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 200, in _message_field_handle
    self._message(message, root_desc, [FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER])
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 487, in _message
    _content_tuple: Optional[Tuple[str, str]] = self._message_field_handle(
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 221, in _message_field_handle
    protobuf_type_model = self._get_protobuf_type_model(field)
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/plugin/field_desc_proto_to_code.py", line 577, in _get_protobuf_type_model
    py_type_str=self._get_value_code(type_factory),
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/gen_code.py", line 217, in _get_value_code
    type_ = replace_protobuf_type_to_python_type(type_)
  File "/Users/tirth/work/bicycle-ai/venv/lib/python3.10/site-packages/protobuf_to_pydantic/util.py", line 77, in replace_protobuf_type_to_python_type
    elif isinstance(value, (list, *ProtobufRepeatedType)):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/abc.py", line 119, in __instancecheck__
    return _abc_instancecheck(cls, instance)
RecursionError: maximum recursion depth exceeded in comparison

To Reproduce

syntax = "proto3";
package temp.test;
message InvoiceItem2 {
  string name = 1;
  int32 amount = 2;
  int32 quantity = 3;
  repeated InvoiceItem2 items = 4;
  Invoice3 invoice = 5;
}

message Invoice3 {
  string name = 1;
  int32 amount = 2;
  int32 quantity = 3;
  repeated InvoiceItem2 items = 4;
}

I tried converting the proto to pydantic classes using python -m grpc_tools.protoc -I./testProto --python_out=./testProto ./testProto/temp2.proto

It can also be reproduced by

from protobuf_to_pydantic import msg_to_pydantic_model

from testProto.temp2_pb2 import Invoice3

model = msg_to_pydantic_model(Invoice3)

Expected behavior Pydantic classes to be generated

terrypireaux commented 2 months ago

Also repeatable if running with imports included like buf generate --template buf.gen.python.yaml --include-imports