Skip to main content

Salient: A simple SqlAlchemy Linter.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Salient-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

Salient-0.1.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file Salient-0.1.1.tar.gz.

File metadata

  • Download URL: Salient-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.0 CPython/3.8.6 Linux/5.8.0-7625-generic

File hashes

Hashes for Salient-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9bb494c6b08bf6b2b2ae71c43f75dfc8d09ff7a75451f3bee23ac8656e5c0989
MD5 a59a2a2402519cd489b461aba65099be
BLAKE2b-256 2dc1cceeb59b6a3ec440cba8ec9ca3e45b5d3680c24b0132883b698fd5ab3921

See more details on using hashes here.

File details

Details for the file Salient-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: Salient-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.0 CPython/3.8.6 Linux/5.8.0-7625-generic

File hashes

Hashes for Salient-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 23adf94daa75063f8a93596df07bca71f0b08124ad1e956b6f86f53572276a88
MD5 3beade226f0759a2e56488ba7fb70f9c
BLAKE2b-256 f4d1a2bc62c33316c7465463a29f90295277419cd1bee2a6b8caf9ccc5004c89

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page