TypeError in asyncpg.connect() for specific parameters when values are not str enough
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 56/100
Research direction
Start by running the issue's enum.StrEnum reproducer, then inspect coreproto.pyx at CoreProtocol._connect and the WriteBuffer.write_str path shown in the traceback. Trace why the connection parameters are handled differently, and add regression coverage showing the reported str-compatible values are handled consistently without the TypeError.
Written by the indexing model from the issue text.
Description
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?
- Dominant language
- Python
- Stars
- 8.1k
- Forks
- 469
- Avg merge
- 4h 42m
- Merged PRs (30d)
- 6
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MagicStack/asyncpg
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
MagicStack/asyncpg#1357 ·
-
tests fail in 2032 Open
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
MagicStack/asyncpg#997 ·
-
Connection.close(timeout=) waits forever on a pending cancel when the server never acknowledges it Open
Difficulty 4/5 3-5 days Newbie friendliness 68/100
MagicStack/asyncpg#1356 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
MagicStack/asyncpg#1354 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
MagicStack/asyncpg#1342 ·
All issues in MagicStack/asyncpg
Similar issues
-
documentation help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
simonw/sqlite-utils#872 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100