Python-language-server: SQLAlchemy intellisense

Created on 9 Jan 2020  路  4Comments  路  Source: microsoft/python-language-server

This is the continuation of microsoft/vscode-python#292, i'm opening a new issue as requested by @brettcannon on microsoft/vscode-python#292

Environment data

  • VS Code version: 1.41.1
  • Extension version (available under the Extensions sidebar): 2020.1.57204
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.3
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): virtualenv
  • Relevant/affected Python packages and their versions: Flask-SQLAlchemy==2.4.1,SQLAlchemy==1.3.12

Expected behaviour

List all available properties and methods.

Actual behaviour

Can't recognize some methods (e.g: methods from the db object)

Steps to reproduce:

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

  1. in some file.py

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

db.Col //Here the intellisense can't find the Column member

bug analysis

Most helpful comment

Reproducible. Need to look into analysis, the SQLAlchemy class appears to be fairly plain...

All 4 comments

Reproducible. Need to look into analysis, the SQLAlchemy class appears to be fairly plain...

It looks like those methods are dynamically added. LS is a static analyzer and does not execute any Python code. Therefore members that are added dynamically, won't show up as static analyzer only sees what is declared in SQLAlchemy(object) and what is added in __init__().

Example

def _include_sqlalchemy(obj, cls):
    for module in sqlalchemy, sqlalchemy.orm:
        for key in module.__all__:
            if not hasattr(obj, key):
                setattr(obj, key, getattr(module, key))
    # Note: obj.Table does not attempt to be a SQLAlchemy Table class.
    obj.Table = _make_table(obj)
    obj.relationship = _wrap_with_default_query_class(obj.relationship, cls)
    obj.relation = _wrap_with_default_query_class(obj.relation, cls)
    obj.dynamic_loader = _wrap_with_default_query_class(obj.dynamic_loader, cls)
    obj.event = event

I got it. Thanks for your response @MikhailArkhipov.

Generally speaking, some systems, like Visual Studio, implement some support in some languages like C#, but in involves first actually including SQL project with schema into the solution so C# can pick it up and generate C# data source class from SQL schema and then C# intellisense picks it up as a regular C# object. Perhaps at some point Python tools will be able to provide something similar.

Was this page helpful?
0 / 5 - 0 ratings