Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Type error on server-side unary-unary handler code

未关闭
#561 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
grpc, python

调研方向

从 reproduce.proto 和生成的 reproduce/init.py 开始,重点关注 XBase.__rpc_y 以及对 self.y 的调用。使用所示命令重新运行 protoc,然后运行 mypy reproduce/init.py 以重现错误。当生成的 handler 通过 mypy 且不再出现不兼容的 Optional[Z] 参数错误时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

bug investigation needed
Summary

Incompatible type error on generated unary-unary handler code.

Reproduction Steps

Write reproduce.proto:

syntax = "proto3";
package reproduce;

service X {
  rpc Y(Z) returns (Z) {}
}

message Z {}

Generate code with betterproto==2.0.0b6:

$ protoc -I . --python_betterproto_out=. reproduce.proto

Verify type error by mypy==1.8.0:

$ mypy reproduce/__init__.py
Expected Results

No mypy error

Actual Results
reproduce/__init__.py:55: error: Argument 1 to "y" of "XBase" has incompatible type "Z | None"; expected "Z"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Here's a chunk of the generated code which has the type error:

class XBase(ServiceBase):
    async def __rpc_y(self, stream: "grpclib.server.Stream[Z, Z]") -> None:
        request = await stream.recv_message()
        response = await self.y(request)
        #                       ^^^^^^^ Argument 1 to "y" of "XBase" has incompatible type "Z | None"; expected "Z"  [arg-type]
        await stream.send_message(response)

stream.recv_message() returns Optional[Z] so request might be None when the connection closes. But self.y() doesn't accept None because it's parameter type is Z but not Optional[Z].

System Information
libprotoc 25.3
Python 3.11.6
Name: betterproto
Version: 2.0.0b6
Summary: A better Protobuf / gRPC generator & library
Home-page: https://github.com/danielgtaylor/python-betterproto
Author: Daniel G. Taylor
Author-email: [email protected]
License: MIT
Location: .../.venv/lib/python3.11/site-packages
Requires: grpclib, python-dateutil
Required-by:
Name: grpclib
Version: 0.4.7
Summary: Pure-Python gRPC implementation for asyncio
Home-page: https://github.com/vmagamedov/grpclib
Author: Vladimir Magamedov
Author-email: [email protected]
License: BSD-3-Clause
Location: .../.venv/lib/python3.11/site-packages
Requires: h2, multidict
Required-by: betterproto
Name: mypy
Version: 1.8.0
Summary: Optional static typing for Python
Home-page: https://www.mypy-lang.org/
Author: Jukka Lehtosalo
Author-email: [email protected]
License: MIT
Location: .../.venv/lib/python3.11/site-packages
Requires: mypy-extensions, typing-extensions
Required-by:
Checklist
  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.
主要语言
Python
星标
1.8k
派生
235
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

danielgtaylor/python-betterproto 的其他 Issue

查看 danielgtaylor/python-betterproto 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。