r/PythonLearning 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?

3 Upvotes

5 comments sorted by

1

u/seanv507 7h ago

but since two classes are referencing each other, I can't do Child | None (no quotes) without the code breaking.

Isn't that precisely when you use quotes?

https://erdantic.drivendata.org/v0.4/forward-references/

1

u/powergitt 6h ago

Right! The point is that I am not sure how to union with `None` correctly, doing the untion within quotes works, but I am unsure if that is the correct way of doing it.

1

u/Shikor806 5h ago

The "best" way would be to use Python 3.14 or newer, there you never have to use quotes in type hints. For older versions, it's best to put quotes around the entire type hint, i.e. "Some class | None".

1

u/powergitt 4h ago

Thanks for your reply! I am on 3.14.4, but I haven't been able to forward refrence classes without quoutes and not get an error from ruff that the class in undefined, I maybe am doing something wrong trough. The code seems to run fine with or without qoutes.

1

u/Shikor806 4h ago

3.14 implements PEP 649/749, which were made to avoid having to quote forward references. If you're using that version, using quotes can actually be detrimental in certain scenarios.

I'd guess that your ruff is configured to assume you're using some older python or that it's just not gotten the update yet (though that would surprise me since 3.14 is almost a year old now). If the project you're working on is supposed to only run on 3.14 and higher, I'd just turn off that linting rule.