Pyright cannot recognize the type of `SQLModel.__tablename__`
See original GitHub issueFirst 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:
- Created 2 years ago
- Reactions:5
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
another really easy workaround is just specifying the type:
__tablename__: str = 'poop'
this is my workaround to silence that type of errors (not my proudest hack but it work)
inspired by the declared_attr documentation, tested with pyright 1.1.172 (Linux), python 3.9.7, sqlmodel 0.0.4