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.

Infer type for dialects.postgresql.UUID(as_uuid=True)

See original GitHub issue

Is your feature request related to a problem? Please describe.

from sqlalchemy import Column, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import declarative_base

Base = declarative_base()
UUIDC = UUID(as_uuid=True)


class Foo(Base):
    id = Column(UUIDC)
    bar_id = Column(UUIDC, ForeignKey("bars.id"))

This will yield the following errors:

$ mypy --strict testuuidc.py
testuuidc.py:10: error: Need type annotation for 'id'
testuuidc.py:11: error: [SQLAlchemy Mypy plugin] Can't infer type from ORM mapped expression assigned to attribute 'bar_id'; please specify a Python type or Mapped[<python type>] on the left hand side.
Found 2 errors in 1 file (checked 1 source file)

Describe the solution you’d like

I wonder if thanks to the plugin, mypy wouldn’t be able to infer the uuid.UUID type from the postgresql.UUID(as_uuid=True) definition.

Describe alternatives you’ve considered

Obviously, we can always provide the typing explicitly.

Have a nice day!

Thanks a lot for the amazing work on sqlalchemy!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
charlaxcommented, Apr 29, 2021

Actually, providing the typing explictly fails:

from uuid import UUID

from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.dialects.postgresql import UUID as _UUIDC
from sqlalchemy.orm import declarative_base, Mapped

Base = declarative_base()
UUIDC = _UUIDC(as_uuid=True)


class Foo(Base):
    id = Column(UUIDC)
    id2: UUID = Column(UUIDC)
    id3: Mapped[UUID] = Column(UUIDC)

    bar_id = Column(UUIDC, ForeignKey("bars.id"))

    int1 = Column(Integer)
    int2: int = Column(Integer)
    int3: Mapped[int] = Column(Integer)

This yields the following errors:

$ mypy --strict testuuidc.py
testuuidc.py:12: error: Need type annotation for 'id'
testuuidc.py:13: error: Incompatible types in assignment (expression has type "Column[<nothing>]", variable has type "UUID")
testuuidc.py:14: error: Incompatible types in assignment (expression has type "Column[<nothing>]", variable has type "Mapped[UUID]")
testuuidc.py:16: error: [SQLAlchemy Mypy plugin] Can't infer type from ORM mapped expression assigned to attribute 'bar_id'; please specify a Python type or Mapped[<python type>] on the left hand side.
Found 4 errors in 1 file (checked 1 source file)
0reactions
sqla-testercommented, May 27, 2022

Mike Bayer has proposed a fix for this issue in the main branch:

add typing for PG UUID, other types https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3870

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I use UUIDs in SQLAlchemy? - Stack Overflow
The sqlalchemy postgres dialect supports UUID columns. This is easy (and the question is specifically postgres) -- I don't understand why the other...
Read more >
PostgreSQL UUID type, Python's UUID — and type hints
I looked into the implementation of the dialect's UUID whether it implements a constructor which takes a Python UUID, but no luck. Given...
Read more >
Documentation: 15: 8.12. UUID Type - PostgreSQL
The data type uuid stores Universally Unique Identifiers (UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards.
Read more >
GUID Type - FastAPI Utilities
In particular, the postgres-compatible UUID type provided by sqlalchemy ( sqlalchemy.dialects.postgresql.UUID ) will not work with other databases, ...
Read more >
PostgreSQL - UUID Data Type - GeeksforGeeks
PostgreSQL – UUID Data Type ... UUID is an abbreviation for Universal Unique Identifier defined by RFC 4122 and has a size of...
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