danielgtaylor / danielgtaylor/python-betterproto

Type error on server-side unary-unary handler code

オープン
#561 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug investigation needed
主要言語
Python
スター
1.8k
フォーク
234
PR マージ指標
30日以内にマージされた PR はありません

説明

### Summary

Incompatible type error on generated unary-unary handler code.

### Reproduction Steps

Write `reproduce.proto`:

```proto
syntax = "proto3";
package reproduce;

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

message Z {}
```

Generate code with `betterproto==2.0.0b6`:

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

Verify type error by `mypy==1.8.0`:

```sh
$ 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:

```python
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: danielgtaylor@gmail.com
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: vladimir@magamedov.com
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: jukka.lehtosalo@iki.fi
License: MIT
Location: .../.venv/lib/python3.11/site-packages
Requires: mypy-extensions, typing-extensions
Required-by:
```

### Checklist

- [X] I have searched the issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have verified this issue occurs on the latest prelease of betterproto which can be installed using `pip install -U --pre betterproto`, if possible.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

reproduce.proto と生成された reproduce/__init__.py から始め、XBase.__rpc_y と self.y の呼び出しに注目してください。示されているコマンドで protoc を再実行し、その後 mypy reproduce/__init__.py を実行してエラーを再現してください。生成された handler が互換性のない Optional[Z] 引数エラーなしで mypy を通過すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
grpc, python
領域
backend-api-design, devtools
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。