Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace flake8 with ruff #209

Merged
merged 14 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Black
name: Python black

# Run either when pushing directly to main/master or in a PR targeting main/master
on:
Expand All @@ -19,7 +19,6 @@ on:
jobs:
format:
runs-on: ubuntu-latest
name: Python-format
steps:
- name: Check out repository
uses: actions/checkout@v3
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ jobs:
working-directory: rust
env:
TERM: xterm-256color
run: cargo clippy
# exit non-zero if there are any warnings
# https://doc.rust-lang.org/stable/clippy/usage.html
run: cargo clippy -- -Dwarnings
11 changes: 5 additions & 6 deletions .github/workflows/isort.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: isort
name: Python isort

# Run either when pushing directly to main/master or in a PR targeting main/master
on:
Expand All @@ -7,27 +7,26 @@ on:
- master
- main
paths:
- '**.py'
- "**.py"
pull_request:
branches:
- master
- main
paths:
- '**.py'
- "**.py"

# https://github.com/marketplace/actions/python-isort
jobs:
sort-imports:
format:
runs-on: ubuntu-latest
name: Python-imports
steps:
- name: Check out source repository
uses: actions/checkout@v3

- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: '3.11'
python-version: "3.11"

- name: isort
uses: isort/[email protected]
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Python pip

# Run either when pushing directly to main/master or in a PR targeting main/master
on:
push:
branches:
- master
- main
paths:
- "python/requirements.txt"
pull_request:
branches:
- master
- main
paths:
- "python/requirements.txt"

# https://github.com/py-actions/py-dependency-install
jobs:
dependencies:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Install Python dependencies
uses: py-actions/py-dependency-install@v4
with:
path: "python/requirements.txt"
12 changes: 5 additions & 7 deletions .github/workflows/flake8.yml → .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: flake8
name: Python ruff

# Run either when pushing directly to main/master or in a PR targeting main/master
on:
Expand All @@ -15,11 +15,10 @@ on:
paths:
- "**.py"

# https://github.com/py-actions/flake8
# https://github.com/chartboost/ruff-action
jobs:
lint:
runs-on: ubuntu-latest
name: Python-lint
steps:
- name: Check out source repository
uses: actions/checkout@v3
Expand All @@ -29,8 +28,7 @@ jobs:
with:
python-version: "3.11"

- name: flake8 lint
uses: py-actions/flake8@v2
- name: ruff
uses: chartboost/ruff-action@v1
with:
path: python
args: "--config python/setup.cfg"
src: "./python"
23 changes: 12 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.286
hooks:
- id: ruff
name: python ruff
files: ^python/
args: [--fix, --exit-non-zero-on-fix, --config, python/pyproject.toml]

- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
name: python black
language_version: python3.11
files: ^python/

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
name: python flake8
files: ^python/
args: [--config, python/setup.cfg]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
Expand All @@ -41,12 +41,12 @@ repos:
files: ^python/

- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
rev: v3.10.1
hooks:
- id: pyupgrade
name: python pyupgrade
files: ^python/
args: [--py37-plus]
args: [--py38-plus]

- repo: local
hooks:
Expand All @@ -58,6 +58,7 @@ repos:
types: [file, rust]
language: system
files: ^rust/

- id: rust-clippy
name: rust lint
description: Run cargo clippy on files included in the commit. clippy should be installed before-hand.
Expand Down
2 changes: 1 addition & 1 deletion common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ check_and_set_python() {
# if DRYRUN or DRY_RUN has been set, only print commands instead of running them
run_command() {
if [ "$DRY_RUN" = true ] || [ "$DRYRUN" = true ]; then
echo "DRYRUN: $*"
print_yellow "DRYRUN: $*"
else
echo "Running: $*"
"$@"
Expand Down
1 change: 0 additions & 1 deletion python/README.md

This file was deleted.

50 changes: 50 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# nitor-vault

Python vault implementation.

See the [root readme](../README.md) for more information.

## Dependencies

The `requirements.txt` file is generated by pip-compile and should not be modified manually.
To update the requirements file, run:

```shell
pip-compile setup.py > requirements.txt
```

## Code formatting and linting

Code formatting with [Black](https://github.com/psf/black).
Import sorting with [isort](https://github.com/PyCQA/isort).
Linting with [ruff](https://github.com/charliermarsh/ruff).

These are configured with a custom line length limit of 120.
The configs can be found in [pyproject.toml](./pyproject.toml).

Usage:

```shell
black .
isort .
ruff --fix .
```

These can also be integrated to IDEs / editors or run as a pre-commit hook.
See the documentation for Black [here](https://black.readthedocs.io/en/stable/integrations/editors.html).
Visual Studio Code has built-in support for
[Black](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter)
and
[isort](https://marketplace.visualstudio.com/items?itemName=ms-python.isort)
through official plugins.
There is also a [Ruff extension](https://github.com/charliermarsh/ruff-vscode) for VS Code.

Using with [pre-commit](https://pre-commit.com/) (run from repo root):

```shell
# setup to be run automatically on git commit
pre-commit install

# run manually
pre-commit run --all-files
```
6 changes: 5 additions & 1 deletion python/n_vault/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ def main():
"--secret",
help="Give an IAM secret access key to override those defined by environment",
)
parser.add_argument("-r", "--region", help="Give a region for the stack and bucket")
parser.add_argument(
"-r",
"--region",
help="Give a region for the stack and bucket",
)

if "_ARGCOMPLETE" in os.environ:
argcomplete.autocomplete(parser)
Expand Down
3 changes: 1 addition & 2 deletions python/n_vault/vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def __init__(
self._session = session(aws_access_key_id=vault_iam_id, aws_secret_access_key=vault_iam_secret)
self._c_args = {"session": self._session, "region": self._region}

# Either use given vault kms key and/or vault bucket or look them up from a
# cloudformation stack
# Either use given vault kms key and/or vault bucket or look them up from a CloudFormation stack
if vault_key:
self._vault_key = vault_key
elif "VAULT_KEY" in os.environ:
Expand Down
107 changes: 83 additions & 24 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,69 @@
[tool.black]
# https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file
line-length = 120
target-version = ['py37']
target-version = ['py38']
include = '\.pyi?$'
extend-exclude = 'venv'

[tool.isort]
# https://pycqa.github.io/isort/docs/configuration/options.html
# profile = "black"
# manually specifying black compatibility to override line length
multi_line_output = 3
combine_as_imports = true
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true
line_length = 120
py_version = 37
sections = ['FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER']
extend_skip = ['.idea', '.vscode', 'venv']
extend-exclude = '''venv*'''

[tool.ruff]
# https://github.com/charliermarsh/ruff#configuration
target-version = "py37"
include = ["*.py", "*.pyi", "**/pyproject.toml"]
target-version = "py38"
line-length = 120
select = ["E", "F", "W", "I", "N", "UP"]
select = ["E", "F", "W", "N", "UP"]
ignore = []
per-file-ignores = {}

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["A", "B", "C", "D", "E", "F"]
fixable = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
#"I", disable isort rules
"N",
"Q",
"S",
"T",
"W",
"ANN",
"ARG",
"BLE",
"COM",
"DJ",
"DTZ",
"EM",
"ERA",
"EXE",
"FBT",
"ICN",
"INP",
"ISC",
"NPY",
"PD",
"PGH",
"PIE",
"PL",
"PT",
"PTH",
"PYI",
"RET",
"RSE",
"RUF",
"SIM",
"SLF",
"TCH",
"TID",
"TRY",
"UP",
"YTT",
]
unfixable = []

# Exclude a variety of commonly ignored directories.
Expand All @@ -51,9 +86,33 @@ exclude = [
"build",
"dist",
"node_modules",
"venv",
"venv*",
]
per-file-ignores = {}

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
[tool.ruff.isort]
# https://beta.ruff.rs/docs/settings/#isort
combine-as-imports = true
lines-between-types = 1
section-order = [
"future",
"standard-library",
"third-party",
"first-party",
"local-folder",
]

[tool.isort]
# https://pycqa.github.io/isort/docs/configuration/options.html
# profile = "black"
# manually specifying black compatibility to override line length
combine_as_imports = true
ensure_newline_before_comments = true
extend_skip = [".idea", ".vscode", ".venv", "venv"]
extend_skip_glob = ["venv*"]
force_grid_wrap = 0
include_trailing_comma = true
line_length = 120
multi_line_output = 3
py_version = 38
sections = ['FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER']
use_parentheses = true
Loading