Skip to content
This repository was archived by the owner on Aug 19, 2022. It is now read-only.

Flake8 / pre-commit / nox review #119

Merged
merged 1 commit into from
Jul 21, 2020
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
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
select = ANN,B,B9,BLK,C,E,F,RST,S,W
select = B,B9,C,E,F,N,RST,S,W
ignore = E203,E501,W503,C901,S308
max-line-length = 80
max-complexity = 10
Expand Down
63 changes: 43 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
- repo: local
hooks:
- id: black
name: black
entry: black
language: system
types: [python]
require_serial: true
- id: check-added-large-files
name: Check for added large files
entry: check-added-large-files
language: system
- id: check-toml
name: Check Toml
entry: check-toml
language: system
types: [toml]
- id: check-yaml
name: Check Yaml
entry: check-yaml
language: system
types: [yaml]
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.1
hooks:
name: Fix End of Files
entry: end-of-file-fixer
language: system
types: [text]
stages: [commit, push, manual]
- id: flake8
additional_dependencies:
- flake8-bandit==2.1.2
- flake8-bugbear==20.1.4
- flake8-docstrings==1.5.0
- pep8-naming==0.10.0
- darglint==1.3.0
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.3.0
hooks:
name: flake8
entry: flake8
language: system
types: [python]
require_serial: true
- id: reorder-python-imports
name: Reorder python imports
entry: reorder-python-imports
language: system
types: [python]
args: [--application-directories=src]
- id: trailing-whitespace
name: Trim Trailing Whitespace
entry: trailing-whitespace-fixer
language: system
types: [text]
stages: [commit, push, manual]
# - repo: https://github.com/prettier/prettier
# rev: 2.0.5
# hooks:
# - id: prettier
69 changes: 68 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import tempfile
from pathlib import Path
from textwrap import dedent
from typing import cast
from typing import Iterator

Expand Down Expand Up @@ -109,12 +110,78 @@ def install(session: Session, *args: str) -> None:
session.install(f"--constraint={requirements}", *args)


def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
"""Activate virtualenv in hooks installed by pre-commit.

This function patches git hooks installed by pre-commit to activate the
session's virtual environment. This allows pre-commit to locate hooks in
that environment when invoked from git.

Args:
session: The Session object.
"""
if session.bin is None:
return

virtualenv = session.env.get("VIRTUAL_ENV")
if virtualenv is None:
return

hookdir = Path(".git") / "hooks"
if not hookdir.is_dir():
return

for hook in hookdir.iterdir():
if hook.name.endswith(".sample") or not hook.is_file():
continue

text = hook.read_text()
bindir = repr(session.bin)[1:-1] # strip quotes
if not (
Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text
):
continue

lines = text.splitlines()
if not (lines[0].startswith("#!") and "python" in lines[0].lower()):
continue

header = dedent(
f"""\
import os
os.environ["VIRTUAL_ENV"] = {virtualenv!r}
os.environ["PATH"] = os.pathsep.join((
{session.bin!r},
os.environ.get("PATH", ""),
))
"""
)

lines.insert(1, header)
hook.write_text("\n".join(lines))


@nox.session(name="pre-commit", python="3.8")
def precommit(session: Session) -> None:
"""Lint using pre-commit."""
args = session.posargs or ["run", "--all-files", "--show-diff-on-failure"]
install(session, "pre-commit")
install(
session,
"black",
"darglint",
"flake8",
"flake8-bandit",
"flake8-bugbear",
"flake8-docstrings",
"flake8-rst-docstrings",
"pep8-naming",
"pre-commit",
"pre-commit-hooks",
"reorder-python-imports",
)
session.run("pre-commit", *args)
if args and args[0] == "install":
activate_virtualenv_in_precommit_hooks(session)


@nox.session(python="3.8")
Expand Down
Loading