souperk / pycharm-sqlalchemy-plugin

14 stars 2 forks source link

Feature Enhancement Request: Code Completion for SQLAlchemy 2.0 Mapped Classes #1

Open andressommerhoff opened 4 months ago

andressommerhoff commented 4 months ago

Hi @souperk ,

First, I want to express my gratitude for your PyCharm plugin which addresses the ongoing challenge of type checking for SQLAlchemy models. It's been incredibly helpful, especially as PyCharm itself hasn't fully adapted to the new SQLAlchemy 2.0 syntax yet.

I'm reaching out to discuss a potential extension of the plugin's capabilities. As it stands, the plugin does a great job with type checks, but it seems that the autocomplete features of PyCharm don't yet support instances derived from MappedAsDataclass and DeclarativeBase. For example, when creating a new instance of a model, such as user1 = User(, the expected autocomplete suggestions for fields (like id, name, and email) don't appear (assuming of course that my User class declares these members as fields).

Given that your plugin effectively enhances type checking for these models, I was wondering if extending this support to include autocomplete suggestions for model instantiation might be feasible. I suspect this feature might be closely tied to the type checks already being refined by your plugin.

I understand this may involve considerable effort, so even insights into the feasibility of such an enhancement would be greatly appreciated.

Thanks again for your contributions to the community and for considering this suggestion!

andressommerhoff commented 4 months ago

Code example of the feature:

from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import Integer, String, Identity, ForeignKey

class Base(MappedAsDataclass, DeclarativeBase): 
    pass

class User(Base):
    __tablename__ = 'user'

    id: Mapped[int] = mapped_column(Identity(start=1, cycle=True), primary_key=True)
    name: Mapped[str]
    email: Mapped[str] = mapped_column(String(30),  unique=True)

user1 = User(    # <- here code Completion or type hint expected with "id", "name" and "email" as sugestion