Skip to content

Selecting array of enums breaks enum codecs #590

Description

@neumond
  • asyncpg version: 0.20.0
  • PostgreSQL version: 10
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : local Postgres in docker
  • Python version: 3.7.4
  • Platform: Alpine Linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes

I've made script to reproduce the bug with explanations in comments:

importasynciofromenumimportEnumimportasyncpgclassChoice(Enum):
rock='rock'paper='paper'scissors='scissors'ACTIVATE_KRAKEN=Trueasyncdefmain():
conn=awaitasyncpg.connect('postgresql://postgres@localhost/test')
# make test repeatableawaitconn.execute("DROP TABLE IF EXISTS game")
awaitconn.execute("DROP TYPE IF EXISTS choice")
awaitconn.execute(
"CREATE TYPE choice AS ENUM ('rock', 'paper', 'scissors')")
awaitconn.execute(""" CREATE TABLE game( id serial PRIMARY KEY, ch choice ) """)
awaitconn.execute(
"INSERT INTO game (ch) VALUES ('rock'), ('paper'), ('rock')")
awaitconn.set_type_codec(
'choice',
encoder=lambdac: c.name,
decoder=Choice,
schema='public',
format='text',
)
ifnotACTIVATE_KRAKEN:
# looks like we're preheating some internal cache hereawaitconn.fetchval("SELECT 'paper'::choice")
awaitconn.fetch("SELECT * FROM game WHERE ch = $1", Choice.paper)
# moment when things do happenprint(repr(
awaitconn.fetchval("SELECT '{paper,rock}'::choice[]")
))
# ['paper', 'rock']# ^ it returns reasonable result here, but if we do the followingprint(repr(
awaitconn.fetchval("SELECT 'paper'::choice")
))
print(repr(
awaitconn.fetch("SELECT * FROM game WHERE ch = $1", Choice.paper)
))
# if kraken was asleep, everything is fine:# <Choice.paper: 'paper'># [<Record id=2 ch=<Choice.paper: 'paper'>>]# if kraken has been activated:# 'paper'# asyncpg.exceptions.DataError: invalid input for query argument $1:# <Choice.paper: 'paper'> (expected str, got Choice)asyncio.run(main())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions