Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
senese opened this issue Jul 27, 2024 · 3 comments
Closed

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

senese opened this issue Jul 27, 2024 · 3 comments
Assignees

Comments

@senese
Copy link

senese commented Jul 27, 2024

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

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Jul 27, 2024
@EleanorPrins
Copy link

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

@senese
Copy link
Author

senese commented Jul 27, 2024

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

image

@debonte
Copy link
Contributor

debonte commented Sep 23, 2024

@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 debonte closed this as completed Sep 23, 2024
@debonte debonte added by design and removed needs repro Issue has not been reproduced yet labels Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants