#๐Ÿ”’ Type Error: TypeVar for cls?

9 messages ยท Page 1 of 1 (latest)

swift plinth
#
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import foreign, relationship
from sqlalchemy.sql.expression import and_
from app.models.translation.translation import Translation


class TranslatableMixin:
    @declared_attr
    def translations(cls):
        return relationship(
            Translation,
            # FIX: Pyright type error
            primaryjoin=and_(
                foreign(Translation.translatable_id) == cls.id,
                Translation.translatable_type == cls.__tablename__,
            ),
            remote_side=[Translation.translatable_id],
            viewonly=True,
        )

    def get_translation(self, field: str, language: str = "en"):
        return next(
            (
                t.translation
                for t in self.translations
                if t.field_name == field and t.language.iso639 == language
            ),
            None,
        )

error: Cannot access member "id" for type "TranslatableMixin*"ย ย Member "id" is unknown 14:61:9 Pyright reportGeneralTypeIssues

Is there a way to fix this typing error?

dapper gateBOT
#

@swift plinth

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

swift plinth
#
foreign(Translation.translatable_id) == cls.id, Translation.translatable_type == cls.__tablename__)

error here since cls is TranslatableMixin, instead of the model it's inherited from

nimble sand
#

TypeOfIdThatYouExpect being the Column descriptor or whatever sqlalchemy uses

swift plinth
#

that works, thanks

#

!close

dapper gateBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.