Using Mapped[str | None]

619 views
Skip to first unread message

Peter Schutt

unread,
Aug 31, 2022, 5:00:11 AM8/31/22
to sqlalchemy
Hi,

I've been using 2.0 from main and notice that annotating an attribute with `mapped[str | None]` raises with:

sqlalchemy.exc.ArgumentError: Could not locate SQLAlchemy Core type for Python type: str | None

I've been able to get it to work with a couple of mods in util.typing and orm.properties and the basic tox run doesn't show any failures.

I've searched both GH issues and in the group here for that exception string without result, is this worth opening an issue for?

Thanks!


Mike Bayer

unread,
Aug 31, 2022, 9:32:37 AM8/31/22
to noreply-spamdigest via sqlalchemy
you should be using Mapped[Optional[str]] ; that said if Mapped[Union[str, None]] and/or Mapped[str | None] is not being parsed then yes this is worthy of a bug report for 2.0.



Thanks!



--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
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+...@googlegroups.com.

Peter Schutt

unread,
Aug 31, 2022, 8:27:00 PM8/31/22
to sqlalchemy
Thanks Mike, I will submit at next opportunity.

I believe Union[str, None] is fine, just that pep 604 brought in UnionType in 3.10+ which represents the "str | None" union type, but I'll stick more detail in the issue.

Not having to import Optional or Union everywhere is one of the nice ergonomic improvements to typing of late.

Cheers:)

Mike Bayer

unread,
Aug 31, 2022, 9:17:33 PM8/31/22
to noreply-spamdigest via sqlalchemy
absolutely, there's no reason str | None shouldn't work also
Reply all
Reply to author
Forward
0 new messages