GithubHelp home page GithubHelp logo

flask-sqlalchemy-stubs's Introduction

Mypy plugin and stubs for Flask-SQLAlchemy

Please check example directory.

Important: you need to enable the plugin in your mypy config file, and pycharm doesn't support mypy plugin.

[mypy]
plugins = flasksqlamypy

Exapmle

from typing import TYPE_CHECKING
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

if TYPE_CHECKING:
    from flask_sqlalchemy.model import Model

    BaseModel = db.make_declarative_base(Model)
else:
    BaseModel = db.Model

class User(BaseModel):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

user = User(id=42, name=42)  # Error: Incompatible type for "name" of "User"
                             # (got "int", expected "Optional[str]")
user.id  # Inferred type is "int"
User.name  # Inferred type is "Column[Optional[str]]"

Install

pip install git+https://github.com/ssfdust/flask-sqlalchemy-stubs.git

Jedi Completion Tricks

You can define a classmethod with self typed, which makes jedi completion to work. Please check examples/sample.py for the entire example.

./assets/jedi.gif

flask-sqlalchemy-stubs's People

Contributors

snacsnoc avatar ssfdust avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

flask-sqlalchemy-stubs's Issues

Compatibility with older versions of Python?

Hi,

Thanks for your work! Unfortunately, as I'm using Python 3.6, I'm unable to test it. Is there a reason why only Python 3.8+ is supported? Would it be possible to have it also for Python 3.6?

Thanks,
Thomas

mypy failing in examples using filter_by and other functions

mypy doesn't pass using the examples in this repo in my environment.

Steps to reproduce using Python 3.8.5:

  1. Clone this repo
  2. pip install .
  3. mypy examples

Output:

examples/sample.py:31: error: Incompatible type for argument "name" (got "int", expected "Optional[str]")
examples/sample.py:32: error: Call to untyped function "filter_by" in typed context
examples/sample.py:37: error: Call to untyped function "filter_by" in typed context
Found 3 errors in 1 file (checked 4 source files)

I get similar issues in my project.

Multiple inheritance crashes flask-sqlalchemy-stubs

Python 3.8

Using mypy 0.782

(venv) PS C:\path\to\project> dmypy run -- --strict --follow-imports error .

Restarting: configuration changed
Daemon stopped
Daemon started
Daemon crashed!
Traceback (most recent call last):
  File "mypy\dmypy_server.py", line 223, in serve
  File "mypy\dmypy_server.py", line 266, in run_command
  File "mypy\dmypy_server.py", line 325, in cmd_run
  File "mypy\dmypy_server.py", line 382, in check
  File "mypy\dmypy_server.py", line 412, in initialize_fine_grained
  File "mypy\build.py", line 180, in build
  File "mypy\build.py", line 252, in _build
  File "mypy\build.py", line 2626, in dispatch
  File "mypy\build.py", line 2949, in process_graph
  File "mypy\build.py", line 3041, in process_stale_scc
  File "mypy\semanal_main.py", line 78, in semantic_analysis_for_scc
  File "mypy\semanal_main.py", line 199, in process_top_levels
  File "mypy\semanal_main.py", line 326, in semantic_analyze_target
  File "mypy\semanal.py", line 357, in refresh_partial
  File "mypy\semanal.py", line 368, in refresh_top_level
  File "mypy\semanal.py", line 4693, in accept
  File "mypy\errors.py", line 720, in report_internal_error
  File "mypy\semanal.py", line 4691, in accept
  File "mypy\nodes.py", line 939, in accept
  File "mypy\semanal.py", line 1011, in visit_class_def
  File "mypy\semanal.py", line 1088, in analyze_class
  File "mypy\semanal.py", line 1097, in analyze_class_body_common
  File "mypy\semanal.py", line 1157, in apply_class_plugin_hooks
  File "c:\path\to\project\venv\lib\site-packages\flasksqlamypy\hooks.py", line 72, in model_base_class_hook
    add_query_to_cls(ctx)
  File "c:\path\to\project\venv\lib\site-packages\flasksqlamypy\utils.py", line 332, in add_query_to_cls
    query_type = get_query_class_from_base(ctx)
  File "c:\path\to\project\venv\lib\site-packages\flasksqlamypy\utils.py", line 313, in get_query_class_from_base
    if "query_class" in expr.node.names:  # type: ignore
AttributeError: 'Var' object has no attribute 'names'

Using mypy 0.790

(venv) PS C:\path\to\project> dmypy run -- --strict --follow-imports error .

Daemon crashed!
Traceback (most recent call last):
  File "mypy\dmypy_server.py", line 221, in serve
  File "mypy\dmypy_server.py", line 264, in run_command
  File "mypy\dmypy_server.py", line 323, in cmd_run
  File "mypy\dmypy_server.py", line 383, in check
  File "mypy\dmypy_server.py", line 511, in fine_grained_increment  
  File "mypy\server\update.py", line 243, in update
  File "mypy\server\update.py", line 309, in update_one
  File "mypy\server\update.py", line 374, in update_module
  File "mypy\server\update.py", line 590, in update_module_isolated 
  File "mypy\semanal_main.py", line 78, in semantic_analysis_for_scc
  File "mypy\semanal_main.py", line 199, in process_top_levels
  File "mypy\semanal_main.py", line 326, in semantic_analyze_target
  File "mypy\semanal.py", line 357, in refresh_partial
  File "mypy\semanal.py", line 368, in refresh_top_level
  File "mypy\semanal.py", line 4702, in accept
  File "mypy\errors.py", line 741, in report_internal_error
  File "mypy\semanal.py", line 4700, in accept
  File "mypy\nodes.py", line 1196, in accept
  File "mypy\semanal.py", line 3255, in visit_if_stmt
  File "mypy\semanal.py", line 3170, in visit_block
  File "mypy\semanal.py", line 4702, in accept
  File "mypy\errors.py", line 741, in report_internal_error
  File "mypy\semanal.py", line 4700, in accept
  File "mypy\nodes.py", line 1063, in accept
  File "mypy\semanal.py", line 1943, in visit_assignment_stmt
  File "mypy\semanal.py", line 2194, in apply_dynamic_class_hook
  File "c:\path\to\project\venv\lib\site-packages\flasksqlamypy\hooks.py", line 38, in declarative_base_hook
    info = create_dynamic_class(
  File "c:\path\to\project\venv\lib\site-packages\flasksqlamypy\utils.py", line 204, in create_dynamic_class
    add_query_cls_var(ctx.api, info)
  File "c:\path\to\project\venv\lib\site-packages\flasksqlamypy\utils.py", line 145, in add_query_cls_var
    assert sym is not None
AssertionError

mypy.ini:2: error: Error importing plugin 'flasksqlamypy': No module named 'flasksqlamypy'

I added the flasksqlamypy plugin to my mypy.ini as suggested by the docs, but ended up hitting the following error when running mypy:

mypy.ini:2: error: Error importing plugin 'flasksqlamypy': No module named 'flasksqlamypy'

Looking in my site-packages folder, it doesn't appear that flasksqlamypy is getting installed as a top-level module when installing from PyPi.

Does something need to be fixed with the package configuration?

Unresolved attribute reference 'Model' for class 'SQLAlchemy'

Before installing this plugin, PyCharm was at least able to detect db.Model declaration, but now it doesn't.

Ways to reproduce:

from typing import TYPE_CHECKING

from flask_sqlalchemy import SQLAlchemy as FlaskSQLAlchemy


db = FlaskSQLAlchemy()

if TYPE_CHECKING:
    from flask_sqlalchemy.model import Model

    BaseModel = db.make_declarative_base(Model)
else:
    BaseModel = db.Model  # <- Unresolved attribute reference 'Model' for class 'SQLAlchemy'

A work around is:

from typing import TYPE_CHECKING

from flask_sqlalchemy import SQLAlchemy as FlaskSQLAlchemy
from flask_sqlalchemy.model import DefaultMeta


class SQLAlchemy(FlaskSQLAlchemy): 
    Model: DefaultMeta


db = SQLAlchemy()

if TYPE_CHECKING:
    from flask_sqlalchemy.model import Model

    BaseModel = db.make_declarative_base(Model)

else:
    BaseModel = db.Model  # No error

Could we please add type hints for db.Model as well?

PyPI

Wazzap!
First of all, thank you for your work!
Is there availability to install this package from pypi via pip or pipenv/poetry?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.