-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
TypeAlias cannot be found in type_annotation_map #11370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
we probably cant use Unions as keys, kind of an edge case |
I think the issue here is this: JsonPrimitive: TypeAlias = Union[str, int, float, bool, None]
JsonObject: TypeAlias = Dict[str, object]
JsonArray: TypeAlias = List[object]
Json: TypeAlias = Union[JsonObject, JsonArray, JsonPrimitive]
print(Optional[Json] == Json) This prints @zzzeek I think the current implementation removes the None from the union when searching for type. |
do we have a patch for this? near term label is for stuff we have a fix set up for. this one is in that intricate place w/ the type guessing etc |
I'm not sure it can be fixed, since changing the current logic would change the current documented behaviour. The main issue here is that we have an union that includes None in the column type, since this is a json column and there are two null supported: json-null and sql-null. This conflicts with the logic used to detect if a column is nullable, meaning that in the type map we look for the union-without-none. Overall this case is undecidable, so I would flag it as won't fix, maybe adding that special case to the error message / linked error link. |
Frazer McLean has proposed a fix for this issue in the main branch: Permit optional types in type annotation map https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5513 |
this can't be cleanly fixed until we fix the architecture that's broken in #11944 |
Frazer McLean has proposed a fix for this issue in the main branch: General improvement on annotated declarative https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5513 |
Frazer McLean has proposed a fix for this issue in the main branch: Permit optional types in type annotation map https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5513 |
Frazer McLean has proposed a fix for this issue in the rel_2_0 branch: Permit optional types in type annotation map https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5600 |
Fixed issue regarding ``Union`` types that would be present in the :paramref:`_orm.registry.type_annotation_map` of a :class:`_orm.registry` or declarative base class, where a ``Mapped[]`` element that included one of the subtypes present in that ``Union`` would be matched to that entry, potentially ignoring other entries that matched exactly. The correct behavior now takes place such that an entry should only match in ``type_annotation_map`` exactly, as a ``Union`` type is a self-contained type. For example, an attribute with ``Mapped[float]`` would previously match to a ``type_annotation_map`` entry ``Union[float, Decimal]``; this will no longer match and will now only match to an entry that states ``float``. Pull request courtesy Frazer McLean. Fixes #11370 Closes: #11942 Pull-request: #11942 Pull-request-sha: 21a3d19 Change-Id: I3467be00f8fa8bd011dd4805a77a3b80ff74a215 (cherry picked from commit 40c30ec)
Describe the bug
I have a
TypeAlias
that I'm trying to put intype_annotation_map
but SQLAlchemy can't seem to find it.(my naive understanding was that
Json
in the example would be looked up directly by hash in the annotation map)Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected
No response
SQLAlchemy Version in Use
2.0.30
DBAPI (i.e. the database driver)
psycopg2
Database Vendor and Major Version
PostgreSQL 12
Python Version
3.10
Operating system
macOS
To Reproduce
Error
Additional context
There's still an error even if the TypeAlias is not self-referential:
but weirdly it works if
JsonPrimitive
doesn't haveNone
:(both of these types aren't very useful, so I would specify the JSONB explicitly in
mapped_column
, but I thought it's worth mentioning what I'd tried).The text was updated successfully, but these errors were encountered: