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

Naming bug for twice-nested messages #17

Closed
cetanu opened this issue Feb 6, 2020 · 3 comments
Closed

Naming bug for twice-nested messages #17

cetanu opened this issue Feb 6, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@cetanu
Copy link
Collaborator

cetanu commented Feb 6, 2020

Hello!

I have an interesting one for you.

I have not had time to look into exactly why this occurs, thought I should report it first because I can reproduce it and N brains are better than 1.

Problem

Twice-nested messages results in type hints which reference the correct dataclass, however the innermost dataclass has prepended the name of the root message twice

Example proto file

syntax = "proto3";

message Root {
  message Parent {
    message Child {
      string foo = 1;
    }
    reserved 1;
    repeated Child child = 2;
    bool bar = 5;
  }
  string name = 1;
  Parent parent = 4;
}

Resulting output

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: testfile.proto
# plugin: python-betterproto
from dataclasses import dataclass
from typing import List

import betterproto


@dataclass
class Root(betterproto.Message):
    name: str = betterproto.string_field(1)
    parent: "RootParent" = betterproto.message_field(4)


@dataclass
class RootParent(betterproto.Message):
    child: List["RootParentChild"] = betterproto.message_field(2)
    bar: bool = betterproto.bool_field(5)


@dataclass
class RootRootParentChild(betterproto.Message): # <-------- "RootRoot"
    foo: str = betterproto.string_field(1)

Steps to reproduce

from pathlib import Path
from grpc_tools import protoc


protocol_buffer = '''
syntax = "proto3";

message Root {
  message Parent {
    message Child {
      string foo = 1;
    }
    reserved 1;
    repeated Child child = 2;
    bool bar = 5;
  }
  string name = 1;
  Parent parent = 4;
}
'''
testfile = Path('testfile.proto')
testfile.write_text(protocol_buffer, encoding='utf-8')

output = Path('BUILD')

proto_include = protoc.pkg_resources.resource_filename('grpc_tools', '_proto')
args = [
    __file__,
    f'--proto_path=.',
    f'--proto_path={proto_include}',
    f'--python_betterproto_out={output}',
    str(testfile)
]
protoc.main(args)
@cetanu
Copy link
Collaborator Author

cetanu commented Feb 6, 2020

Looks like it occurs here:

https://github.com/danielgtaylor/python-betterproto/blob/master/betterproto/plugin.py#L134-L138

Not sure how to fix this...

@danielgtaylor danielgtaylor added the bug Something isn't working label Feb 11, 2020
@danielgtaylor
Copy link
Owner

Nice find!

@cetanu
Copy link
Collaborator Author

cetanu commented Feb 18, 2020

@danielgtaylor I linked a PR for this :) let me know your thoughts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants