On Sat, Jan 15, 2022, at 3:34 PM, [email protected] wrote:
> Hello Mike,
>
> Yes that’s using the plugin:
> sqlalchemy[mypy,postgresql_psycopg2binary]==1.4.29 I changed the function
> declaration to this:
>
> @staticmethod
> def 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 = user
>
> which creates the following error:
>
> error: Argument "spouse" to "User" has incompatible type "User"; expected
> "Mapped[Any]" [arg-type]
if the User class is unmapped, you wouldn't be assigning it to a relationship
attribute like that.
I think what you might mean is that you have a User object that is what we call
"transient", meaning, it's not yet persisted with any Session.
again, you would want to set up the relationship attribute with a more specific
annotation:
class User(...):
# ...
spouse: Mapped[User] = relationship("User", ...)
background on this in terms of the plugin is at
https://docs.sqlalchemy.org/en/14/orm/extensions/mypy.html#mapping-relationships
> 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?
no that's not correct. The User class is "mapped" at the class level. you
would only need one directive or the other (one of Mapped[User] or User) when
using the Mypy plugin.
>
> Other than following the migration guide
> <https://docs.sqlalchemy.org/en/14/changelog/migration_20.html>, I haven’t
> looked into SQLA2 <https://github.com/sqlalchemy/sqlalchemy/projects/3>.
> Jens
>
>
> 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 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/5ccd954f-277f-4f4e-adc1-2d7a906b9c09n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/5ccd954f-277f-4f4e-adc1-2d7a906b9c09n%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/69514bb3-2c7c-469a-886d-f1e224c8afc0%40www.fastmail.com.