Open mdczaplicki opened 3 years ago
This may a plugin issue. cc @bryanforbes
Getting the same error on this class
import re
from typing import Any
from sqlalchemy.orm import as_declarative, declared_attr
@as_declarative()
class Base:
id: Any
__name__: str
# Generate __tablename__ automatically
@declared_attr
def __tablename__(cls) -> str:
name = cls.__name__
# It is a regex that matches CamelCase and replaces it with snake_case
# https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case
return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
Installed via SQLAlchemy = {extras = ["mypy"], version = "^1.4.23"}
Getting the same error even using Mapped
as the error message suggests with
class TimestampModelMixin:
@declared_attr
def created_at(cls) -> datetime:
return Column(types.DateTime(timezone=True), server_default=text("CURRENT_TIMESTAMP"), nullable=False)
@declared_attr
def updated_at(cls) -> Mapped[datetime]:
return Column(
types.DateTime(timezone=True),
server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
nullable=False,
)
Describe the bug Receiving an error when using
@declared_attr
decorator on__tablenane__
class method.Expected behavior Not receiving an error.
To Reproduce
Error
Versions.
Additional context It also happens for columns with UUID (same example) if you don't specify a type on left hand side. It happens when using
UUIDType
fromsqlalchemy_utils
orUUID
fromsqlalchemy.dialects.postgresql
.Have a nice day!