Hello,

For a PostgreSQL 14 db, I defined a User mapping and helper function like 
so:

from sqlalchemy.dialects.postgresql import UUID

class User(Base):
    id = Column(UUID(as_uuid=True), primary_key=True, 
server_default=func.gen_random_uuid())
    name = Column(Unicode(128))

    @staticmethod
    def create(dbsession: Session, name: str, id_: uuid.UUID = None):
        user = User(name=name, id=id_)
        dbsession.add(user)
        return user

Notice that the helper function’s id_ has a different UUID type than the 
User’s mapped property. And that, in return causes this mypy 
<https://mypy.readthedocs.io/> error:

error: Argument "id_" to "create" of "User" has incompatible type 
"uuid.UUID"; expected "Optional[sqlalchemy.dialects.postgresql.base.UUID]" 
 [arg-type]

I looked into the implementation of the dialect’s UUID 
<https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/dialects/postgresql/base.py#L1695-L1752>
 
whether it implements a constructor which takes a Python UUID, but no luck. 
Given that PostgreSQL supports UUIDs natively 
<https://www.postgresql.org/docs/14/datatype-uuid.html>, I didn’t want to 
use UUIDType 
<https://sqlalchemy-utils.readthedocs.io/en/latest/data_types.html#module-sqlalchemy_utils.types.uuid>
 
either. So, Other that cast() 
<https://docs.python.org/3/library/typing.html#typing.cast> I’m not sure 
how to work with this error…

Thanks!
Jens

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/54c50972-85be-4a86-b9bf-4daa435d939an%40googlegroups.com.

Reply via email to