Closed
Labels
questionFurther information is requested
Description
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: strDescription
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
Reacted by Andres Bordese, Thomas Clarke, magine, Ben Mares, Alex Rogozhnikov, Pierre C., Petr Gazarov, David Freire, Frestein ☁️, Kiyo Kunii and 10 more
Reacted by Zaffer
Metadata
Metadata
Assignees
Labels
questionFurther information is requested
Activity
Chedi commentedon Sep 30, 2021
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
Ma233 commentedon Oct 7, 2021
@Chedi Hi, of my own workaround is adding a
# type: ignoreafter__tablename__ = "users".mudassirkhan19 commentedon Oct 28, 2021
With @Chedi 's workaround, I still get an error on Mypy:
error: Signature of "__tablename__" incompatible with supertype "SQLModel".Only
# type: ignoreworks.indivisible commentedon Dec 17, 2021
another really easy workaround is just specifying the type:
__tablename__: str = 'poop'cassieopea commentedon Jun 4, 2022
Note that Pyright complains at this:
To satisfy the type checker you need to use the declared_attr approach, eg:
This could be mentioned in the docs, and maybe in a future version a more intuitive approach could be used, such as putting it in a
Metaas ormar does:I thought about making a request for this but I'm guessing there's already a lot on the roadmap.
cassieopea commentedon Jun 21, 2022
Since I posted #98 (comment), I've started simply doing
I think this is by far the best solution for people still getting the "incorrect override" error for the line without
type: ignore. As I said when I mentioned this in #159,anton-fomin commentedon Feb 8, 2025
It seems
Solves this for both pyright and mypy
Updated
Works only with
basictype checking mode. Does not work instandardchriscarrollsmith commentedon May 3, 2025
I tried all the fixes in this thread, and I'm sorry to report that you'll need
# type: ignoreto fix this with Pylance in "standard" type checking mode, because none of the other suggestions work.madduck commentedon Jun 30, 2025
I use
and be done with it.
youhogeon-meritz commentedon Sep 3, 2025
It works on pyright strict mode . But not graceful.
madduck commentedon Sep 3, 2025
I can confirm this works for pyright and mypy.
Fix Pyrefly __tablename__ override errors in SQLModel classes
marcoshevaristo commentedon Dec 28, 2025
I can confirm this works.
Replace type ignores with casts and proper typing
Ship PlugPack MVP with immediate value (#9)
🐛 Move `__tablename__` default from `@declared_attr` to metaclass
🐛 Move `__tablename__` default from `@declared_attr` to metaclass
🐛 Move `__tablename__` default from `@declared_attr` to metaclass
🐛 Move `__tablename__` default from `@declared_attr` to metaclass
🐛 Declare `__tablename__` on `SQLModelMetaclass`
🐛 Declare `__tablename__` on `SQLModelMetaclass`