Skip to content

typing-extensions 4.13.0 possibly breaks ORM declarative codepaths that are deprecated in 2.0 #12473

@Giuzzilla

Description

@Giuzzilla

Describe the bug

Defining a simple custom type with the type syntax (es. type MyType = Literal["test1", "test2"]) and using it within a declarative model within a Mapped (e.g. Mapped[MyType]) fails with typing-extensions 4.13.0

I raised the same issue on typing_extensions and it seems something that needs to be fixed on SQLAlchemy side (just like pydantic had to fix it)
python/typing_extensions#560

Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected

No response

SQLAlchemy Version in Use

2.0.39

DBAPI (i.e. the database driver)

psycopg(3) (but shouldn't matter)

Database Vendor and Major Version

Postgres 13 (but shouldn't matter)

Python Version

3.12.9

Operating system

Linux (Ubuntu)

To Reproduce

from typing import Literal

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

type MyType = Literal["test1", "test2"]


class Base(DeclarativeBase):
    pass


class MyModel(Base):
    __tablename__ = "my_model"

    id: Mapped[int] = mapped_column(primary_key=True)
    my_type: Mapped[MyType]

Error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "../lib/python3.12/site-packages/sqlalchemy/orm/decl_api.py", line 842, in __init_subclass__
    _as_declarative(cls._sa_registry, cls, cls.__dict__)
  File "../lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 244, in _as_declarative
    return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "../lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 325, in setup_mapping
    return _ClassScanMapperConfig(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "../lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 572, in __init__
    self._extract_mappable_attributes()
  File "../lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 1568, in _extract_mappable_attributes
    value.declarative_scan(
  File "../lib/python3.12/site-packages/sqlalchemy/orm/properties.py", line 707, in declarative_scan
    self._init_column_for_annotation(
  File "../lib/python3.12/site-packages/sqlalchemy/orm/properties.py", line 871, in _init_column_for_annotation
    raise sa_exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Could not locate SQLAlchemy Core type for Python type MyType inside the 'my_type' attribute Mapped annotation

Additional context

It doesn't matter whether you add the type in the type_annotation_map within Base.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghigh prioritynear-term releaseaddition to the milestone which indicates this should be in a near-term releaseormorm - annotated declarativeissues with the new annotations-based declarative ORM approach

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions