#9213·sqlalchemy

document `@classmethod` add for all `@declared_attr` to work w/ typing

Author: DreamsorcererCreated Feb 1, 2023Updated Sep 3, 2026
Labelsdocumentationtyping

Describe the typing issue

Next issue, it seems I get an error when using a Mapped class attribute in Index.

To Reproduce

Stripped down version of what I'm running:

python
@sa.orm.declarative_mixin
class _Rhyme:
    __tablename__: str

    ph1: Mapped[int]
    ph2: Mapped[Optional[int]]

    @sa.orm.declared_attr
    def __table_args__(cls):
        return (sa.Index(cls.__tablename__ + "_ph1_ph2", cls.ph1, cls.ph2),)

Error

rhyme.py:42:66: error: Argument 2 to "Index" has incompatible type "int"; expected "Union[str, Column[Any], DDLConstraintColumnRole]"  [arg-type]
            return (sa.Index(cls.__tablename__ + "_ph1_ph2_syl_aux", cls.ph1, cls.ph2, cls.syl, cls.aux_syl),
                                                                     ^~~~~~~

Versions

  • SQLAlchemy: 2.0.0
  • Type checker: mypy 0.991

Additional context

No response