You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypeError in asyncpg.connect() for specific parameters when values are not str enough #1340
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:
importasyncioimportenumimportasyncpgclassConnect(enum.StrEnum):
HOST='localhost'USER='postgresql'asyncdefconnect_host():
returnawaitasyncpg.connect(host=Connect.HOST)
asyncdefconnect_host_user():
returnawaitasyncpg.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())
exceptasyncpg.PostgresErrorase:
print('not connected:', repr(e))
try:
asyncio.run(connect_host_user())
exceptasyncpg.PostgresErrorase:
# this except block is never hit, connection setup encounters a TypeErrorprint('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?
Both halves check out on current main. coreproto.pyx line 947 is exactly where your traceback points, buf.write_str(self.con_params.user, self.encoding) inside _connect(), and write_str is the one that raises on a str subclass. The second bug is real too and independent of the first: protocol.pyx has exactly one reference to _on_error, the call site at line 983 inside connection_made's except block. There's no definition in that file, so whatever sets it up must not have run yet when _connect() fails this early, which is exactly why you got an AttributeError instead of the TypeError actually being handled.
That second one seems worth fixing regardless of how the first question shakes out, masking the real error with an unrelated AttributeError isn't intentional either way. Rejecting str subclasses might be, some C extension code does exact type checks like this deliberately for the fast path, so I'll leave that call to whoever knows why write_str is strict here.
Activity
adityaanikam commentedon Aug 8, 2026
Both halves check out on current
main.coreproto.pyxline 947 is exactly where your traceback points,buf.write_str(self.con_params.user, self.encoding)inside_connect(), andwrite_stris the one that raises on astrsubclass. The second bug is real too and independent of the first:protocol.pyxhas exactly one reference to_on_error, the call site at line 983 insideconnection_made's except block. There's no definition in that file, so whatever sets it up must not have run yet when_connect()fails this early, which is exactly why you got anAttributeErrorinstead of theTypeErroractually being handled.That second one seems worth fixing regardless of how the first question shakes out, masking the real error with an unrelated
AttributeErrorisn't intentional either way. Rejectingstrsubclasses might be, some C extension code does exact type checks like this deliberately for the fast path, so I'll leave that call to whoever knows whywrite_stris strict here.