question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The custom uuid implementation of asyncpg causes errors in my api

See original GitHub issue
  • asyncpg version: asyncpg==0.20.0
  • PostgreSQL version: PostgreSQL 11.5 on x86_64-pc-linux-musl, compiled by gcc (Alpine 8.3.0) 8.3.0, 64-bit
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce the issue with a local PostgreSQL install?: no
  • Python version: 3.8.0
  • Platform: linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes
  • If you built asyncpg locally, which version of Cython did you use?:
  • Can the issue be reproduced under both asyncio and uvloop?: yes

I am using asyncpg to build a simple api using the library fastapi, the asyncpg implementation of uuid causes an error when I read a uuid from the database.

The uuid implementation of asynpg is not properly picked up by fastapi because they use type(obj) instead of instance(obj, uuid.UUID). Basically there is a defined list of types with their jsonification methods and asyncpg.pgproto.pgproto.UUID is not in that list. This piece of code demonstrates the issue:

import uuid
from asyncpg.pgproto import pgproto


def demonstrate_uuid_issue():
    asyncpg_uuid = pgproto.UUID("a10ff360-3b1e-4984-a26f-d3ab460bdb51")
    regular_uuid = uuid.UUID("a10ff360-3b1e-4984-a26f-d3ab460bdb51")

    print(regular_uuid == asyncpg_uuid)  # True
    print(isinstance(asyncpg_uuid, type(regular_uuid)))  # True
    print(type(asyncpg_uuid) == type(regular_uuid))  # False

The pgproto.UUID implementation does a pretty good job hiding it’s true colors but unfortunately it is still of a different type than the regular uuid.UUID. This basically throws an error in my api and forces me to use the regular uuid type like so:

await connection.set_type_codec('uuid',
                                encoder=str,
                                decoder=uuid.UUID,
                                schema='pg_catalog')

Someone already suggested to push your new uuid implementation upstream: https://github.com/MagicStack/py-pgproto/blob/484e3520d8cb0514b7596a8f9eaa80f3f7b79d0c/uuid.pyx#L324-L328 I think that would be an ideal solution to the problem. Are there any other steps I can take to resolve this issue on the asyncpg side of things?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
lowercase00commented, Jul 21, 2022

Experiencing the same problem while dumping with orjson. Considering some libs in the wild do use type(val), would it make sense to account for third party libraries? It can be the case that those libs are doing it wrong, but perhaps making a change in only one place instead of making PR for N different libraries could help.

3reactions
elpranscommented, Nov 29, 2019

This looks to me like a fastapi bug. Using type(foo) == for type checks is almost always wrong.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AttributeError: 'UUID' object has no attribute 'replace' when ...
The implementation given in the documentation expected a string, however, so this fails: >>> import uuid >>> value = uuid.uuid4() >>> uuid.
Read more >
tiangolo/fastapi - 'UUID' object has no attribute 'replace' - Gitter
the data = dict(obj) fails with e: 'UUID' object has no attribute 'replace' then data works in data = vars(obj) ans is set...
Read more >
Package com.google.api.gax.tracing (2.18.1) | Java client library
The UUID of the connection which the attempt was sent. Attempt failed, error not retryable with the following attributes: attempt: Zero based ...
Read more >
PostgreSQL - SQLAlchemy 1.4 Documentation
Represents the UUID column type, interpreting data either as natively returned by the DBAPI or as Python uuid objects. The UUID type is...
Read more >
4. 20 Asyncio Libraries You Aren't Using (But…Oh, Never Mind)
Many of those libraries will also be useful in your asyncio -based applications, ... The streams API is the high-level interface offered for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found