TypeError in asyncpg.connect() for specific parameters when values are not str enough
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 56/100
調査の方向性
まず issue の enum.StrEnum 再現コードを実行し、次に coreproto.pyx の CoreProtocol._connect と、トレースバックに示された WriteBuffer.write_str の経路を調べてください。接続パラメーターの扱いが異なる理由を追跡し、報告された str 互換の値が TypeError なしで一貫して処理されることを示すリグレッションカバレッジを追加してください。
索引モデルが issue の本文から書いたものです。
説明
When calling asyncpg.connect() with values that are simple str, things work as I expect them to work. Not all of the arguments are treated the same, though. When values that go through the WriteBuffer onto the wire are str, but maybe not only a str, WriteBuffer.write_str raises a TypeError. My concrete use case where I ran into this is connection parameters that were read from a TOML file using tomlkit, which produces tomlkit.items.String values, that are instances of str, but are not accepted.
The following example script demonstrates the issue using enum.StrEnum, which causes the same unexpected error:
import asyncio
import enum
import asyncpg
class Connect(enum.StrEnum):
HOST = 'localhost'
USER = 'postgresql'
async def connect_host():
return await asyncpg.connect(host=Connect.HOST)
async def connect_host_user():
return await asyncpg.connect(host=Connect.HOST, user=Connect.USER)
if __name__ == '__main__':
print('HOST is str:', isinstance(Connect.HOST, str))
print('USER is str:', isinstance(Connect.USER, str))
try:
asyncio.run(connect_host())
except asyncpg.PostgresError as e:
print('not connected:', repr(e))
try:
asyncio.run(connect_host_user())
except asyncpg.PostgresError as e:
# this except block is never hit, connection setup encounters a TypeError
print('not connected:', repr(e))
The output of this script is as follows (Python 3.14.6 on Linux amd64, asyncpg 0.31.0):
HOST is str: True
USER is str: True
not connected: InvalidAuthorizationSpecificationError('role "user" does not exist')
Traceback (most recent call last):
File "asyncpg/protocol/protocol.pyx", line 978, in asyncpg.protocol.protocol.BaseProtocol.connection_made
File "asyncpg/protocol/coreproto.pyx", line 947, in asyncpg.protocol.protocol.CoreProtocol._connect
TypeError: Expected str, got Connect
During handling of the above exception, another exception occurred:
[...]
File "asyncpg/protocol/protocol.pyx", line 983, in asyncpg.protocol.protocol.BaseProtocol.connection_made
AttributeError: 'Protocol' object has no attribute '_on_error'
Using a enum here is a bit odd of course, the point is that the values being passed to asyncpg.connect() are str and treated differently depending on where that argument ends up in the connection setup.
My expectation is that values that are instances of str are used as such and work as intended. The full array connection parameters in my code are read through tomlkit, which produces values that very much quack and walk like a str 🦆.
Are these values being rejected for good reason, or should these just be accepted?
- 主要言語
- Python
- スター
- 8.1k
- フォーク
- 469
- 平均マージ
- 18分
- マージ済み PR(30日)
- 4
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
MagicStack/asyncpg のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
MagicStack/asyncpg#1357 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
MagicStack/asyncpg#1328 ·
-
tests fail in 2032 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
MagicStack/asyncpg#997 ·
-
Connection.close(timeout=) waits forever on a pending cancel when the server never acknowledges it オープン
難易度 4/5 3〜5日 初心者へのやさしさ 68/100
MagicStack/asyncpg#1356 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
MagicStack/asyncpg#1355 · リアクション 3 件 ·
MagicStack/asyncpg の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
OpenHands/extensions#626 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
CSCfi/sd-search-api#39 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
StevenBlack/hosts#3255 ·