Replies: 12 comments
-
|
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 |
Beta Was this translation helpful? Give feedback.
-
|
@Chedi Hi, of my own workaround is adding a |
Beta Was this translation helpful? Give feedback.
-
|
With @Chedi 's workaround, I still get an error on Mypy: |
Beta Was this translation helpful? Give feedback.
-
|
another really easy workaround is just specifying the type: |
Beta Was this translation helpful? Give feedback.
-
Note that Pyright complains at this: To satisfy the type checker you need to use the declared_attr approach, eg: class Lineitem(SQLModel, table=True):
@declared_attr
def __tablename__(cls):
return "invoice_lineitems"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 class Lineitem(SQLModel, table=True):
class Meta:
tablename: str = "invoice_lineitems"I thought about making a request for this but I'm guessing there's already a lot on the roadmap. |
Beta Was this translation helpful? Give feedback.
-
|
Since I posted #98 (comment), I've started simply doing __tablename__: str = "invoices_legacy" # type: ignoreI think this is by far the best solution for people still getting the "incorrect override" error for the line without
|
Beta Was this translation helpful? Give feedback.
-
|
It seems Solves this for both pyright and mypy Updated |
Beta Was this translation helpful? Give feedback.
-
|
I tried all the fixes in this thread, and I'm sorry to report that you'll need |
Beta Was this translation helpful? Give feedback.
-
|
I use __tablename__ = typing.cast(declared_attr, "Users")and be done with it. |
Beta Was this translation helpful? Give feedback.
-
|
It works on pyright strict mode . But not graceful. |
Beta Was this translation helpful? Give feedback.
-
I can confirm this works for pyright and mypy. |
Beta Was this translation helpful? Give feedback.
-
I can confirm this works. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
This will cause a type error when you declare
__tablename__with pyright as type checker. Like:Operating System
Linux, macOS
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.8.6
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions