question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pyright cannot recognize the type of `SQLModel.__tablename__`

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google “How to X in SQLModel” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from sqlmodel import SQLModel

reveal_type(SQLModel.__tablename__)

# mypy(0.910) output: Revealed type is "Union[builtins.str, def (*Any, **Any) -> builtins.str]"
# pyright(1.1.166) output: Type of "SQLModel.__tablename__" is "declared_attr"


class User(SQLModel, table=True):  # pyright error: Instance variable "__name__" overrides class variable of same name in class "SQLModel"
    __tablename__ = "users"  # pyright error: Expression of type "Literal['users']" cannot be assigned to declared type "declared_attr"
    name: str

Description

This will cause a type error when you declare __tablename__ with pyright as type checker. Like:

Expression of type “Literal[‘users’]” cannot be assigned to declared type “declared_attr”

Operating System

Linux, macOS

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.8.6

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

3reactions
indivisiblecommented, Dec 17, 2021

another really easy workaround is just specifying the type: __tablename__: str = 'poop'

2reactions
Chedicommented, Sep 30, 2021

this is my workaround to silence that type of errors (not my proudest hack but it work)

from sqlmodel import SQLModel
from sqlalchemy.orm import declared_attr

class User(SQLModel, table=True): 
    name: str

    @declared_attr
    def __tablename__(cls):  # noqa: N805
        return 'users'

inspired by the declared_attr documentation, tested with pyright 1.1.172 (Linux), python 3.9.7, sqlmodel 0.0.4

Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI with Async SQLAlchemy, SQLModel, and Alembic
This tutorial looks at how to configure SQLAlchemy, SQLModel, and Alembic to work with FastAPI asynchronously.
Read more >
Configuring how Relationship Joins
We need to use cast() in order to cast one side of the join to the type of the ... Where above, SQLAlchemy...
Read more >
How to add obj to db with sqlmodel - python - Stack Overflow
As some comments suggested, you should probably not use databases together with SQLModel . The beauty of SQLModel is (among many other ...
Read more >
Declaring Models — Flask-SQLAlchemy Documentation (2.x)
To override the table name, set the __tablename__ class attribute. ... The types of the column are the first argument to Column ....
Read more >
cameo data modeler plugin - No Magic Documentation
For the sake of compactness, columns are displayed with the. «col» keyword (instead of the long form - «Column») on the diagram. Copyright...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found