Giter Club home page Giter Club logo

sa_lint's Introduction

Table of Contents

  1. Salient: A simple SQLAlchemy Linter
    1. What is it?
    2. Why?
    3. Simple and Naive
      1. The Benefits of Naivete
      2. Trade-Offs
    4. The Name
    5. Usage
    6. Current State
    7. Requirements
    8. Contributing

Salient: A simple SQLAlchemy Linter

What is it?

Salient is a rather simple, naive, even simplistic, linter for SQLAlchemy class based models.

Why?

I wrote this after discovering that a SA model in a project I work on had a table column that was defined once and then redefined with a different definition later in the model. I have also found that unnecessary nullable=True and index=False can be found all over the place. I wrote this after discovering that a SA model in a project I work on had a table column that was defined once and then redefined with a different definition later in the model. I have also found that unnecessary nullable=True and index=False can be found all over the place.

Simple and Naive

Salient takes few options and parses SA models in a naive manner. Salient does not currently use an AST or a finite state machine to parse source code. Rather it parses like you might with grep.

The Benefits of Naivete

Salient should be simple to understand and to maintain.

Trade-Offs

Salient assumes a Python module contains one SA class based model, so if you have a module with multiple classes and they have column names in common you would need to separate them or not check for redefined columns. This also means that if you have nullable=True in a module being linted outside of a column definition the linter is going to be most unhappy with you.

The Name

Salient comes from my abbreviating SQLAlchemy as SA and it being a linter, SAli[e]nt.

Usage

The idea, and hope, is that your SA models live in their own directory separate from other source code. Salient probably won't break if it sees other source, but it was really intended to mostly look at SA models.

poetry run python salient.py -rni src/app/models/*.py

1 file(s) with errors were found.

examples/all_problems.py
  Redefined Columns - unoriginal_column_name:
    16: Column(Integer)
    19: Column(Boolean)
  Unnecessary nullable=True:
    17:     col_1 = Column(nullable=True)
  Unnecessary index=False:
    18:     col_2 = Column(index=False)

You can run with the -h or --help parameter for more options.

usage: salient.py [-h] [-n] [-i] [-r] [--config CONFIG] [-R RECURSIVE] [-1 STOP_AFTER_FIRST_ERROR] files [files ...]

positional arguments:
  files                 files to lint

optional arguments:
  -h, --help            show this help message and exit
  -n, --nullable-true   Check for unnecessary nullable=True
  -i, --index-false     Check for unnecessary index=False
  -r, --redefined-column
                        Check for columns that are redefined.
  --config CONFIG       Load options from CONFIG FILE.
  -R, --recursive       If FILES includes directories scan those as well
  -1 STOP_AFTER_FIRST_ERROR, --stop-after-first-error STOP_AFTER_FIRST_ERROR
                        stop after first error

Current State

Alpha / MVP

  • Believed to do what it says on the tin, but YMMV
  • Not all command line options are implemented. (help, and the three linting rules work, that is all)
  • Doesn't recurse subdirectories
  • Doesn't currently use a config file, and no environment variables have been implemented.
  • Still has TODOs in the code. :)
  • Mostly untested, but the most complex of the linters is tested.
  • PRs welcome!

Requirements

  • Python 3.8 or above.
    • I've set the Poetry config to require Python 3.8 or above. I don't believe anything is preventing use with 3.7, but I am not opposed to throwing in a walrus here and there if it is the best way to do something.
  • Poetry, any modern version.

Contributing

  • Code is formatted with the latest version of Black.
  • MyPy isn't configured yet, but please use typehints. (Not everything is typehinted, but the project is a day old at the time of this writing!)
  • New code should be tested.

sa_lint's People

Contributors

tomfaulkner avatar

Watchers

 avatar  avatar

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.