You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pyright cannot recognize the type of SQLModel.__tablename__ #98
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
fromsqlmodelimportSQLModelreveal_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"classUser(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"
With @Chedi 's workaround, I still get an error on Mypy: error: Signature of "__tablename__" incompatible with supertype "SQLModel".
Only # type: ignore works.
another really easy workaround is just specifying the type: __tablename__: str = 'poop'
Reacted by Andrea Parisi, Jacob Linney, Zaffer, Ganesh Rao, Jonas Frei, Joel Ferreira, Stéphane Rabenarisoa, hecarson, Kyrylo Omelchenko, Maksim Kulichenko and 1 more
Reacted by Vasilis Totskas, Christopher Carroll Smith, Jonathan Yu, sora and João Moreno
Reacted by Ben Mares, Ganesh Rao, hecarson, Benjamin Ashbaugh and Mofeng
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 Meta as ormar does:
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,
I choose this last method because the others are far too verbose for something as simple as declaring the table name. This may seem hacky, but although I have to tell Pyright to ignore a number of things in sqlmodel, it's not half the file like I have to type: ignore with sqlalchemy.
Reacted by Ben Mares, Zaffer, Sebastian Weigand, Paul T Huntsberger, M Atheer and Ernesto Rodriguez
I tried all the fixes in this thread, and I'm sorry to report that you'll need # type: ignore to fix this with Pylance in "standard" type checking mode, because none of the other suggestions work.
Reacted by Benjamin Trom, Hucci, rossbowen, Cédric Bahirwe and zhuxining
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,
I choose this last method because the others are far too verbose for something as simple as declaring the table name. This may seem hacky, but although I have to tell Pyright to ignore a number of things in sqlmodel, it's not half the file like I have to type: ignore with sqlalchemy.
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`