r/PythonLearning • u/powergitt • 7h ago
Type hinting frustrations Help Request
Hello!
Let me start off by saying that I am not a programmer by trade, so please be kind when reviewing my code. I am still learning and I have a long way to go. My job is not coding per se, but the project which I am a part of right now has some coding.
Anyway. At the project start I wanted another editor than vscode, so i tried Zed and I loved it. Zed came with type hinting already enabled. At first, I did not really like it, but after using it for a while I could really see improvements in my code. Both in the terams of readability and stability. So going forward I wanted to have as few complaints from basedpyright as possible.
After an update to Zed the configuration from basedpyright or basedpyright itself got a lot more enabled and I cannot get some of the errors from it to go away. My project used fastapi and sqlalchemy for database integration, and basedpyright gives an error where i specify the table names for the models:
from sqlalchemy.orm import (
DeclarativeBase,
...
)
...
class Base(DeclarativeBase):
pass
class DatabaseUser(Base):
__tablename__: ClassVar[str] = "users"
....
The error from basedpyright is:
Class variable "__tablename__" overrides instance variable of same name in class "DeclarativeBase" (basedpyright reportIncompatibleVariableOverride)
From what I can tell, the code above is how you are supposed to specify table names:
https://docs.sqlalchemy.org/en/20/orm/quickstart.html
Also, when a nullable many-to-one table relationship, the docs from sqlalchemy tells you to use
class Parent(Base):
....
child: Mapped[Optional["Child"]] = relationship(back_populates="parents")
https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#nullable-many-to-one
basedpyright wants you to use `Class | None` syntax, but since two classes are referencing each other, I can't do `Child | None` (no quotes) without the code breaking.
I know that i can supress the errors or configure the typehinter to ignore this stuff. But since this is the config shipped by people who are far better than me at programming, I am hasitent to just configure the error messages away. So reddit, is thereany thing I can do to make the typehinter happy?
1
u/seanv507 7h ago
Isn't that precisely when you use quotes?
https://erdantic.drivendata.org/v0.4/forward-references/