--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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+...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/54c50972-85be-4a86-b9bf-4daa435d939an%40googlegroups.com.
Hello Mike,Yes that’s using the plugin: sqlalchemy[mypy,postgresql_psycopg2binary]==1.4.29 I changed the function declaration to this:@staticmethoddef create(dbsession: Session, name: str, id_: typing.Union[uuid.UUID, sqlalchemy.dialects.postgresql.base.UUID] = None):and it seems to work. In a similar vain, how do I manage the types of mapped and unmapped objects? Following the above example:user = User(dbsession, "Joe Black")returns an unmapped, simple User object. Now suppose I have another existing, mapped User object and would like to assign that newly created unmapped user:other_user.spouse = userwhich creates the following error:error: Argument "spouse" to "User" has incompatible type "User"; expected "Mapped[Any]" [arg-type]
Declaring that column with:spouse: typing.Union[Mapped[User], User] = relationship("User", back_populates="spouse", uselist=False)seems to work; I just want to make sure that that’s a recommended way of going about this? Should I always declare a type as a Union between a mapped and unmapped same class?
On Thursday, January 13, 2022 at 11:52:18 PM UTC+10 Mike Bayer wrote:is this with the SQLAlchemy Mypy plugin? current status is for 2.0 we are looking to move away from the plugin model and pretty much change how these things work. Otherwise if this is with the plugin, you would use "id: Mapped[uuid.UUID] = ..."
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/5ccd954f-277f-4f4e-adc1-2d7a906b9c09n%40googlegroups.com.
Mike, thank you for elaborating, that helps a lot! Yes, I meant a transient or pending object above, you’re correct.To make sure I understand correctly: it’s ok to declaredeleted_at: Mapped[Optional[datetime.datetime]] = Column(sqlalchemy.DateTime, nullable=True)orid: Mapped[uuid.UUID] = Column(sqlalchemy.dialects.postgresql.UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)where the RHS declares the column’s type by means of a SQLA type and the LHS’s type a “mapped” stdlib Python type?
It works here because the column’s type actually is implemented by means of the LHS stdlib type. I also noticed that I can omit e.g. Mapped[uuid.UUID] and in some cases mypy is still able to infer the correct type.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/8e5d65eb-9fde-4ae9-8425-96c09230e389n%40googlegroups.com.