pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Wrong "Expected no arguments to <Flask SQL Alchemy> constructor"

Open senese opened this issue 1 year ago • 2 comments

Environment data

  • Pylance version: v2024.7.1
  • OS and version: Ubuntu 22.04.1 LTS running on WSL2
  • Python version: 3.12.3 build from source

Code Snippet

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase):
  pass

db = SQLAlchemy(model_class=Base)

class User(db.Model):
    id: Mapped[int] = mapped_column(primary_key=True)
    username: Mapped[str] = mapped_column(unique=True)
    email: Mapped[str]


user = User(id=1, username='test')

print(user)

Output: <User (transient 139732127139744)>

Expected behavior

No red squiggly lines around User to warn about error "Expected no arguments to "User" constructor". I've seen #507 and for SQL Alchemy models there's no squiggly lines but this isn't true for Flask-SQLAlchemy models, even if following Flask documentation.

Actual behavior

image

senese avatar Jul 27 '24 00:07 senese

Can reproduce, with base SQLAlchemy.

from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
from sqlalchemy import Column, String

Base: DeclarativeMeta = declarative_base()

class Data(Base):
    __tablename__ = "data"
    id = Column(String(30), primary_key=True, index=True)

def test_func(db: Session):
    db.add(Data(id="testid"))

Screenshot_2024-07-27_22-12-55

EleanorPrins avatar Jul 27 '24 10:07 EleanorPrins

I also want to point it out that when using unpacking syntax it doesn't raise any warnings or errors:

image

senese avatar Jul 27 '24 15:07 senese

@senese, for Pylance to believe that that User has a generated __init__ method with id, username, and email parameters, User or one of its base classes would need to be decorated with dataclass_transform. From what I can tell, it is not.

I'd suggest filing an issue on flask_sqlalchemy.

I also want to point it out that when using unpacking syntax it doesn't raise any warnings or errors:

I think this is because there's no diagnostic when an unpacked dict contains extra fields that don't match parameters on the target function. For example:

params = {"foobar": 1}
user = User(**params) # no diagnostics

debonte avatar Sep 23 '24 23:09 debonte