Skip to main content

NI Python Style Guide

logo


PyPI version Publish Package Code style: black

Welcome to NI's internal and external Python conventions and enforcement tooling.

Written Conventions

Our written conventions can be found at https://ni.github.io/python-styleguide/.

Their source is in docs/Coding-Conventions.md.

NOTE: Using the GitHub Pages link is preferable to a GitHub /blob link.

Enforcement tooling

As a tool, ni-python-styleguide is installed like any other script:

pip install ni-python-styleguide

The script name nps is a short-name for ni-python-styleguide, and may be used in place of ni-python-styleguide in any CLI command.

Linting

To lint, just run the lint subcommand (from within the project root, or lower):

ni-python-styleguide lint
# or
ni-python-styleguide lint ./dir/
# or
nps lint module.py

The rules enforced are all rules documented in the written convention, which are marked as enforced.

Configuration

ni-python-styleguide aims to keep the configuration to a bare minimum (none wherever possible). However there are some situations you might need to configure the tool.

Fix

ni-python-styleguide has a subcommand fix which will run black and isort to apply basic formatting fixes.

When using the --aggressive option with fix, it will first run black and isort to fix what it can then add acknowledgements (# noqa) for any remaining linting errors that cannot be automatically fixed.

When using setup.py

If you're using setup.py, you'll need to set your app's import names for import sorting.

# pyproject.toml
[tool.ni-python-styleguide]
application-import-names = "<app_name>"

Formatting

ni-python-styleguide has a subcommand format which will run black and isort with the correct settings to match the linting expectations.

If you wish to be able to invoke black directly, you'll want to set the following to get black formatting as the styleguide expects.

# pyproject.toml
[tool.black]
line-length = 100

Editor Integration

Vim/Neovim

  1. Install the ALE plugin. This is a popular asynchronous lint engine for Vim and Neovim and already does most of the heavy lifting for us. It supports many different ways to lint and fix code. Check out the documentation (:help ale) for more information.
  2. Because ni-python-styleguide is a wrapper around flake8, you can add the following vim configuration lines to wherever you configure your vim project (you can do it in your init.vim or vimrc file, but then it will apply to all Python code you edit):
let g:ale_python_flake8_executable = 'ni-python-styleguide'
let g:ale_python_flake8_options = 'lint'
let g:ale_linters = {'python': ['flake8']}
let g:ale_python_black_executable = 'ni-python-styleguide'
let g:ale_python_black_options = 'fix'
let g:ale_fixers = {'python': ['isort', 'black']}

Note: You can set all of these with b: as well.

  1. You can make ALE auto-fix issues, e.g., when hitting F8, or when saving:
let g:ale_fix_on_save = 1 " Fix on save
nmap <F8> <Plug>(ale_fix) " Fix on F8

Change all of these to your taste.

VSCode

One can configure VSCode either in the User 'settings.json' file, or in a local .vscode/settings.json.

  1. Install the Python extension by Microsoft

  2. Because ni-python-styleguide is a wrapper around flake8, you can add the following configuration to make it uses ni-python-styleguide's rules.

    If using Poetry:

    "flake8.path": [
          "poetry",
          "run",
          "ni-python-styleguide",
          "lint"
       ],
    

    If ni-python-styleguide is directly installed

    "flake8.path": [
          "ni-python-styleguide",
          "lint"
       ],
    

    (alternatively, tell flake8 to use the ni-python-styleguide config - although this method does not evaluate what the module's application-import-names are)

    "flake8.args": [
         "--config=.venv\\lib\\site-packages\\ni_python_styleguide\\config.ini"
    ],
    
  3. Telling the formatter to use ni-python-styleguide's settings

    (telling VS Code to use black and telling black to use ni-python-styleguide's settings file)

    "[python]": {
         "editor.formatOnType": true,
         "editor.defaultFormatter": "ms-python.black-formatter"
     },
    "black-formatter.args": [
       "--config=.venv\\lib\\site-packages\\ni_python_styleguide\\config.toml"
    ],
    

Download files

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

Source Distribution

ni_python_styleguide-0.5.1.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ni_python_styleguide-0.5.1-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file ni_python_styleguide-0.5.1.tar.gz.

File metadata

  • Download URL: ni_python_styleguide-0.5.1.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for ni_python_styleguide-0.5.1.tar.gz
Algorithm Hash digest
SHA256 9b362135c816ba6e5907e5630bc492102f282c0971b4db371514bc01ee54a2a9
MD5 3e37f14ba78fd4566ab1cddc5401f495
BLAKE2b-256 09db3b2f8ba8fcd3195493d69a1e96c47aefff466b136be087e1fd06ea94f532

See more details on using hashes here.

Provenance

The following attestation bundles were made for ni_python_styleguide-0.5.1.tar.gz:

Publisher: Publish-Package.yml on ni/python-styleguide

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ni_python_styleguide-0.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ni_python_styleguide-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3127122d3610502cf1407d820b1512d887da52c9c0d17f6ccdb1da3e13a06243
MD5 610d75dd113d6bf2ab03163e06493732
BLAKE2b-256 64bad5f3d29dd725e39fe3bd6dc186a0962608526793b1bfd410e038109e3bb6

See more details on using hashes here.

Provenance

The following attestation bundles were made for ni_python_styleguide-0.5.1-py3-none-any.whl:

Publisher: Publish-Package.yml on ni/python-styleguide

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 files

0.5.0

2 files

0.4.9

2 files

0.4.8

2 files

0.4.7

2 files

0.4.6

2 files

0.4.5

2 files

0.4.4

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.17

2 files

0.1.16

2 files

0.1.15

2 files

0.1.14

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page