TypeError in asyncpg.connect() for specific parameters when values are not str enough
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 56/100
Direzione di ricerca
Inizia eseguendo il riproduttore dell’issue per enum.StrEnum, quindi esamina coreproto.pyx in corrispondenza di CoreProtocol._connect e il percorso WriteBuffer.write_str mostrato nel traceback. Traccia il motivo per cui i parametri di connessione vengono gestiti in modo diverso e aggiungi una copertura di regressione che dimostri che i valori compatibili con str segnalati vengono gestiti in modo coerente senza TypeError.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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?
- Lingua principale
- Python
- Stelle
- 8.1k
- Fork
- 469
- Merge medio
- 4h 42m
- PR unite (30g)
- 6
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di MagicStack/asyncpg
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
MagicStack/asyncpg#1357 ·
-
tests fail in 2032 Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
MagicStack/asyncpg#997 ·
-
Connection.close(timeout=) waits forever on a pending cancel when the server never acknowledges it Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 68/100
MagicStack/asyncpg#1356 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
MagicStack/asyncpg#1354 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
MagicStack/asyncpg#1342 ·
Tutte le issue di MagicStack/asyncpg
Issue simili
-
triage/confirmed
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
apache/cloudstack#14222 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100