danielgtaylor / python-betterproto

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

Field named "request" breaks the generated code #639

Closed JanPokorny closed 3 weeks ago

JanPokorny commented 3 weeks ago

Summary

Non-functional code gets generated when a message field is named request

Reproduction Steps

Have a message field named request in your protofile, and use betterproto to generate the Python stubs.

Expected Results

A correct, working code is generated. One possible solution would be to auto-generate a variable name that does not collide with any of the argument names.

Actual Results

A code similar to this is generated:

    async def generate_stream(
        self,
        *,
        request: str = "",
    ) -> AsyncGenerator[GenerationResponse, None]:
        request = SingleGenerationRequest()
        request.request = request

        async for response in self._unary_stream(
            "/fmaas.GenerationService/GenerateStream",
            request,
            GenerationResponse,
        ):
            yield response

Clearly not intentionally, the local variable request overwrites the function argument request, which leads to a circular reference and a crash during serialization.

System Information

I don't use protoc, I compile using buf.

❯ buf --version
1.45.0

❯ python --version
Python 3.12.7

❯ pip show betterproto
Name: betterproto
Version: 1.2.5
Summary: A better Protobuf / gRPC generator & library
Home-page: http://github.com/danielgtaylor/python-betterproto
Author: Daniel G. Taylor
Author-email: danielgtaylor@gmail.com
License: MIT
Requires: grpclib, stringcase

Checklist

Gobot1234 commented 3 weeks ago

This is fixed in 2.0.0 where you directly pass the message into the service