Also allow mapped columns inside indexes, unique cosntraints, primary key constriant
Fixes: #8645
Change-Id: If37ab85ead0fbd1125cd6329c2f01a031777b081
--- /dev/null
+.. change::
+ :tags: bug, typing
+ :tickets: 8645
+
+ Fixed typing issue where pylance strict mode would report "instance
+ variable overrides class variable" when using a method to define
+ ``__tablename__``, ``__mapper_args__`` or ``__table_args__``.
__mapper__: ClassVar[Mapper[Any]]
__table__: ClassVar[Optional[FromClause]]
- __tablename__: ClassVar[Any]
+ # pyright/pylance do not consider a classmethod a ClassVar so use Any
+ # https://github.com/microsoft/pylance-release/issues/3484
+ __tablename__: Any
+ __mapper_args__: Any
+ __table_args__: Any
def __init__(self, **kw: Any):
...
from ..sql import sqltypes
from ..sql.base import _NoArg
from ..sql.elements import SQLCoreOperations
+from ..sql.roles import DDLConstraintColumnRole
from ..sql.schema import Column
from ..sql.schema import SchemaConst
from ..util.typing import de_optionalize_union_types
class MappedColumn(
+ DDLConstraintColumnRole,
SQLCoreOperations[_T],
_IntrospectsAnnotations,
_MapsColumns[_T],
import typing
from sqlalchemy import DateTime
+from sqlalchemy import Index
from sqlalchemy import Integer
from sqlalchemy import String
+from sqlalchemy import UniqueConstraint
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import declared_attr
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
+from sqlalchemy.sql.schema import PrimaryKeyConstraint
class Base(DeclarativeBase):
"polymorphic_identity": "employee",
}
+ __table_args__ = (
+ Index("my_index", name, type),
+ UniqueConstraint(name),
+ PrimaryKeyConstraint(id),
+ {"prefix": []},
+ )
+
class Engineer(Employee):
__mapper_args__ = {
class Foo(Base):
- __tablename__: typing.ClassVar[str] = "foo"
+ __tablename__ = "foo"
id = mapped_column(Integer, primary_key=True)