danielgtaylor / python-betterproto

Clean, modern, Python 3.6+ code generator & library for Protobuf 3 and async gRPC
MIT License
1.5k stars 211 forks source link

v2.0.0.b7 AttributeError on serializing pydantic message with inner message #618

Open mikhainin opened 2 days ago

mikhainin commented 2 days ago

Summary

AttributeError on serializing pydantic message with inner message

Reproduction Steps

Having the following project structure

$ tree
.
├── poetry.lock
├── pyproject.toml
└── src
    ├── __init__.py
    ├── main.py
    ├── test
    │   ├── __init__.py
    └── test.proto

src/test.proto

syntax = "proto3";
package test;

message TestMessage {
  message InnerMessage {
    string inner_str = 1;
  }
  InnerMessage inner_field = 1;
}

Compile command

poetry run python -m grpc_tools.protoc --python_betterproto_opt=pydantic_dataclasses --python_betterproto_out=src -Isrc src/test.proto

And finally, src/main.py

from src.test import TestMessage

if __name__ == "__main__":
    msg = TestMessage()
    msg.to_dict(include_default_values=False)
    print("ok")

Expected Results

Seeing "ok"

Actual Results

$ poetry install
$ poetry run python src/main.py

Traceback (most recent call last):
  File "~/PycharmProjects/betterproto-repro/src/main.py", line 5, in <module>
    msg.to_dict(include_default_values=False)
  File "~/Library/Caches/pypoetry/virtualenvs/betterproto-repro-O0YfRvug-py3.11/lib/python3.11/site-packages/betterproto/__init__.py", line 1463, in to_dict
    value._serialized_on_wire
AttributeError: 'object' object has no attribute '_serialized_on_wire'

System Information

libprotoc 27.3 Python 3.11.9 Name: betterproto Version: 2.0.0b7 Summary: A better Protobuf / gRPC generator & library Home-page: https://github.com/danielgtaylor/python-betterproto Author: Daniel G. Taylor Author-email: danielgtaylor@gmail.com License: MIT Location: /Users/mikhailgalanin/Library/Caches/pypoetry/virtualenvs/betterproto-repro-O0YfRvug-py3.11/lib/python3.11/site-packages Requires: grpclib, python-dateutil, typing-extensions Required-by: betterproto-repro

Checklist

mikhainin commented 2 days ago

Reproduce-case betterproto-repro.zip

AdrienVannson commented 13 hours ago

Can you try again with the latest version of betterproto (last commit on the master branch)? It seems to be the same problem as in https://github.com/danielgtaylor/python-betterproto/issues/606 , which was fixed yesterday.

mikhainin commented 12 hours ago

Hi @AdrienVannson,

Thanks for taking the time to look at this. Yes, the latest master works well

betterproto = { git = "https://github.com/danielgtaylor/python-betterproto.git", rev = "1161803" }
$ poetry lock
$ poetry install
Installing dependencies from lock file

Package operations: 0 installs, 1 updates, 0 removals

  - Updating betterproto (2.0.0b7 -> 2.0.0b7 1161803)

$ poetry run python src/main.py
ok