Skip to content

builtins.NameError: name 'betterproto_lib_google_protobuf' is not defined #377

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

Closed
honglei opened this issue Apr 23, 2022 · 11 comments · Fixed by #381
Closed

builtins.NameError: name 'betterproto_lib_google_protobuf' is not defined #377

honglei opened this issue Apr 23, 2022 · 11 comments · Fixed by #381
Labels
compiler-bug The compiler is broken

Comments

@honglei
Copy link

honglei commented Apr 23, 2022

Env: win10/python3.10.4/python-betterproto 2.0.0b4+

 File "E:\PythonPrj\crud_test\tests\gprc_demos\my_python\hello\uftplink_server.py", line 56, in <module>
  asyncio.run(main( port))
File "C:\Python310\Lib\asyncio\runners.py", line 44, in run
  return loop.run_until_complete(main)
File "C:\Python310\Lib\asyncio\base_events.py", line 646, in run_until_complete
  return future.result()
 File "E:\PythonPrj\crud_test\tests\gprc_demos\my_python\hello\uftplink_server.py", line 46, in main
  server = Server([UftpLinkService()])
File "C:\Python310\Lib\site-packages\grpclib\server.py", line 601, in __init__
  mapping.update(handler.__mapping__())
 File "E:\PythonPrj\crud_test\tests\gprc_demos\my_python\hello\_proto2\uftplink\__init__.py", line 337, in __mapping__
  betterproto_lib_google_protobuf.Empty,

builtins.NameError: name 'betterproto_lib_google_protobuf' is not defined

proto file:

syntax = "proto3";
import "google/protobuf/timestamp.proto";
import "google/protobuf/empty.proto";

package uftplink;

service UftpLink { 
    rpc startLink(google.protobuf.Empty) returns(Reply){}
    rpc stopLink(google.protobuf.Empty) returns(Reply){}
}
message Reply{
    bool success=1;//
    string error=2;//
}
@Gobot1234
Copy link
Collaborator

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

import betterproto
from betterproto.grpc.grpclib_server import ServiceBase
import grpclib


@dataclass(eq=False, repr=False)
class Reply(betterproto.Message):
    success: bool = betterproto.bool_field(1)
    error: str = betterproto.string_field(2)


class UftpLinkStub(betterproto.ServiceStub):
    async def start_link(self) -> "Reply":

        request = betterproto_lib_google_protobuf.Empty()

        return await self._unary_unary("/uftplink.UftpLink/startLink", request, Reply)

    async def stop_link(self) -> "Reply":

        request = betterproto_lib_google_protobuf.Empty()

        return await self._unary_unary("/uftplink.UftpLink/stopLink", request, Reply)


class UftpLinkBase(ServiceBase):
    async def start_link(self) -> "Reply":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def stop_link(self) -> "Reply":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def __rpc_start_link(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {}

        response = await self.start_link(**request_kwargs)
        await stream.send_message(response)

    async def __rpc_stop_link(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()

        request_kwargs = {}

        response = await self.stop_link(**request_kwargs)
        await stream.send_message(response)

    def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
        return {
            "/uftplink.UftpLink/startLink": grpclib.const.Handler(
                self.__rpc_start_link,
                grpclib.const.Cardinality.UNARY_UNARY,
                betterproto_lib_google_protobuf.Empty,
                Reply,
            ),
            "/uftplink.UftpLink/stopLink": grpclib.const.Handler(
                self.__rpc_stop_link,
                grpclib.const.Cardinality.UNARY_UNARY,
                betterproto_lib_google_protobuf.Empty,
                Reply,
            ),
        }


import betterproto.lib.google.protobuf as betterproto_lib_google_protobuf

This is the file that I get generated, everything here looks fine to me. This however isn't the full 300+ line file that you are using so maybe you need the whole thing?

@Gobot1234 Gobot1234 added bug Something isn't working wait Waiting for author labels Apr 23, 2022
@honglei
Copy link
Author

honglei commented Apr 23, 2022

I shorten the proto file, check betterproto_lib_google_protobuf.Empty in your generated python file, it does not been imported from any lib.

@Gobot1234
Copy link
Collaborator

import betterproto.lib.google.protobuf as betterproto_lib_google_protobuf it's imported from the last line

@Gobot1234 Gobot1234 added compiler-bug The compiler is broken and removed bug Something isn't working labels Apr 23, 2022
@honglei
Copy link
Author

honglei commented Apr 23, 2022

For the master verion of betterproto, the line is missed in the produced python:

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: test.proto
# plugin: python-betterproto
from dataclasses import dataclass
from typing import (
    TYPE_CHECKING,
    Dict,
    Optional,
)

import betterproto
import grpclib
from betterproto.grpc.grpclib_server import ServiceBase


if TYPE_CHECKING:
    from betterproto.grpc.grpclib_client import MetadataLike
    from grpclib.metadata import Deadline


@dataclass(eq=False, repr=False)
class Reply(betterproto.Message):
    success: bool = betterproto.bool_field(1)
    error: str = betterproto.string_field(2)


class UftpLinkStub(betterproto.ServiceStub):
    async def start_link(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["_MetadataLike"] = None,
    ) -> "Reply":
        return await self._unary_unary(
            "/uftplink.UftpLink/startLink",
            betterproto_lib_google_protobuf_empty,
            Reply,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )

    async def stop_link(
        self,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["_MetadataLike"] = None,
    ) -> "Reply":
        return await self._unary_unary(
            "/uftplink.UftpLink/stopLink",
            betterproto_lib_google_protobuf_empty,
            Reply,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        )


class UftpLinkBase(ServiceBase):
    async def start_link(self) -> "Reply":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def stop_link(self) -> "Reply":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def __rpc_start_link(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()
        response = await self.start_link(request)
        await stream.send_message(response)

    async def __rpc_stop_link(self, stream: grpclib.server.Stream) -> None:
        request = await stream.recv_message()
        response = await self.stop_link(request)
        await stream.send_message(response)

    def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
        return {
            "/uftplink.UftpLink/startLink": grpclib.const.Handler(
                self.__rpc_start_link,
                grpclib.const.Cardinality.UNARY_UNARY,
                betterproto_lib_google_protobuf.Empty,
                Reply,
            ),
            "/uftplink.UftpLink/stopLink": grpclib.const.Handler(
                self.__rpc_stop_link,
                grpclib.const.Cardinality.UNARY_UNARY,
                betterproto_lib_google_protobuf.Empty,
                Reply,
            ),
        }

I uninstall and then reinstall betterproto for the latest version:

PS E:\PythonPrj\crud_test\tests\gprc_demos\my_python\hello\_proto2> python -m pip uninstall betterproto
Found existing installation: betterproto 2.0.0b4
Uninstalling betterproto-2.0.0b4:
  Would remove:
    c:\python310\lib\site-packages\betterproto-2.0.0b4.dist-info\*
    c:\python310\lib\site-packages\betterproto\*
    c:\python310\scripts\protoc-gen-python_betterproto.exe
Proceed (Y/n)? Y
  Successfully uninstalled betterproto-2.0.0b4
PS E:\PythonPrj\crud_test\tests\gprc_demos\my_python\hello\_proto2> python -m pip install E:\xx\lib_docs\python-betterproto-master.zip --proxy=http://localhost:6666
Processing e:\xx\lib_docs\python-betterproto-master.zip
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: python-dateutil<3.0,>=2.8 in c:\python310\lib\site-packages (from betterproto==2.0.0b4) (2.8.2)
Requirement already satisfied: grpclib<0.5.0,>=0.4.1 in c:\python310\lib\site-packages (from betterproto==2.0.0b4) (0.4.2)
Requirement already satisfied: h2<5,>=3.1.0 in c:\python310\lib\site-packages (from grpclib<0.5.0,>=0.4.1->betterproto==2.0.0b4) (4.1.0)
Requirement already satisfied: multidict in c:\python310\lib\site-packages (from grpclib<0.5.0,>=0.4.1->betterproto==2.0.0b4) (5.1.0)
Requirement already satisfied: six>=1.5 in c:\python310\lib\site-packages (from python-dateutil<3.0,>=2.8->betterproto==2.0.0b4) (1.16.0)
Requirement already satisfied: hyperframe<7,>=6.0 in c:\python310\lib\site-packages (from h2<5,>=3.1.0->grpclib<0.5.0,>=0.4.1->betterproto==2.0.0b4) (6.0.1)
Requirement already satisfied: hpack<5,>=4.0 in c:\python310\lib\site-packages (from h2<5,>=3.1.0->grpclib<0.5.0,>=0.4.1->betterproto==2.0.0b4) (4.0.0)
Building wheels for collected packages: betterproto
  Building wheel for betterproto (pyproject.toml) ... done
  Created wheel for betterproto: filename=betterproto-2.0.0b4-py3-none-any.whl size=60934 sha256=60eb8206c0857cf766f819112062f0d71b6d4ecaffc6914b771abf55e37384fc
  Stored in directory: c:\users\admin\appdata\local\pip\cache\wheels\9d\f1\82\4836bc1990a4a8a8df16fbce9f18d1c7f639ce4ed2d480006d
Successfully built betterproto
Installing collected packages: betterproto
Successfully installed betterproto-2.0.0b4
PS E:\PythonPrj\crud_test\tests\gprc_demos\my_python\hello\_proto2> python -m grpc.tools.protoc -I./  --python_betterproto_out=./   ./test.proto
test.proto:2:1: warning: Import google/protobuf/timestamp.proto is unused.
Writing __init__.py
Writing uftplink\__init__.py

@Gobot1234
Copy link
Collaborator

Ok yes I've been able to reproduce this on master fix incoming soon

@Gobot1234
Copy link
Collaborator

I've managed to fix this locally. The fix should be ready for testing tomorrow

@honglei
Copy link
Author

honglei commented Apr 24, 2022

manually add line import betterproto.lib.google.protobuf as betterproto_lib_google_protobuf works for me.

@honglei
Copy link
Author

honglei commented Apr 24, 2022

also betterproto_lib_google_protobuf_empty need to be changed to betterproto_lib_google_protobuf.Empty in UftpLinkStub.

Gobot1234 added a commit to Gobot1234/python-betterproto that referenced this issue Apr 24, 2022
@Gobot1234 Gobot1234 removed the wait Waiting for author label Apr 24, 2022
@Gobot1234
Copy link
Collaborator

Would you mind testing generation with #381?

@honglei
Copy link
Author

honglei commented Apr 24, 2022

@Gobot1234 I'll test it tomorrow, become it only occured in another pc which is not at my handside rightnow.

@honglei
Copy link
Author

honglei commented Apr 25, 2022

@Gobot1234 test passed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-bug The compiler is broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants