Skip to content
New issue

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

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

Already on GitHub? # to your account

SQL Alchemy and pyright #626

Closed
dcsan opened this issue Apr 16, 2020 · 3 comments
Closed

SQL Alchemy and pyright #626

dcsan opened this issue Apr 16, 2020 · 3 comments
Labels
as designed Not a bug, working as intended

Comments

@dcsan
Copy link

dcsan commented Apr 16, 2020

I have a class which has a superclass, and uses sqlalchemy definitions of instance variables
eg:

class Model(Base):
    id = Column(BigInteger(), primary_key=True)
    uuid = uuid_column(nullable=False, index=True, unique=True)
    name = Column(String(255), nullable=False)
    external_id = Column(String(255), nullable=False)
    source_id = Column(BigInteger(), cascading_key('model_source.id'), nullable=False)
    version = Column(String(80), nullable=False)
    marked_for_production_time = Column(DateTime)

when I try to create an instance of Model passing in properties the syntax highlighting blows up

        return Model(
            name=name,
            external_id=str(self.id), version='1.0',
            source_id=self.id,
            staging=True
        )

image

Is this because it's looking in the superclass for these properties?
or the syntax above for SQLAlchemy columns confuses things?

VS Code extension or command-line
Using within VSCode

Additional context
Above works fine in PyCharm

@dcsan
Copy link
Author

dcsan commented Apr 16, 2020

comparing code in PyCharm (vanilla config)
and using PyRight (after messing around with the config for a while)

image

@erictraut
Copy link
Collaborator

The problem here is that SQL Alchemy is making use of a metaclass (DeclarativeMeta) that implements non-standard behaviors. In particular, it dynamically auto-generates a constructor based on the class variables you've defined. PyCharm apparently has knowledge of this custom behavior.

Pyright does not currently have knowledge of non-standard behaviors within any third-party libraries, and I don't currently plan to add such knowledge. I may eventually add a plug-in mechanism to pyright so non-standard behaviors for third-party libraries could be expressed in plug-in logic. This is how mypy solves this problem.

I'm not able to repro the exact errors that you're seeing. I might be able to offer some suggested workarounds, but I'll need some additional information from you first.

  1. Have you created or installed type stubs for sqlalchemy?
  2. Do you have the "pyright.useLibraryCodeForTypes" setting enabled in VS Code? (It's disabled by default.)
  3. What version of sqlalchemy are you using?

@devenv
Copy link

devenv commented Nov 1, 2023

Just to report that changing useLibraryCodeForTypes to false worked for me, e.g.:

useLibraryCodeForTypes = false,
typeCheckingMode = "basic",

Edit: worked by stopping recognizing my own libraries too, I guess the real solution is
#945 (comment)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

3 participants