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

Setup Flake8 for Python code #24

Open
thundergolfer opened this issue Mar 18, 2020 · 1 comment
Open

Setup Flake8 for Python code #24

thundergolfer opened this issue Mar 18, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@thundergolfer
Copy link
Owner

thundergolfer commented Mar 18, 2020

Description

black (which this project is already using) is only a code formatter and doesn't catch a bunch of issues that crop up in Python code (eg. unused vars).

I'd like to continue using https://github.com/thundergolfer/bazel-linting-system but it might require modifications to enable support for more than 1 'linter' to be registered for 1 language.

@thundergolfer thundergolfer added the enhancement New feature or request label Mar 18, 2020
@thundergolfer thundergolfer changed the title Setup Flake8 Setup Flake8 for Python code Mar 18, 2020
@njlr
Copy link

njlr commented Nov 16, 2022

I was able to get this working with a shim. Interested in cleaner approaches.

BUILD.bazel

py_source_files = glob([
  "*.py",
  "tests/*.py",
])

py_test(
  name = "flake",
  srcs = [
    "flake.py",
  ],
  main = "flake.py",
  args = py_source_files,
  data = [
    ".flake8",
  ] + py_source_files,
  legacy_create_init = 0,
  deps = [
    requirement("flake8"),
  ],
)

flake.py

# Shim used to run flake from a Bazel py_test

import os
import sys
from flake8.api import legacy as flake8

if __name__ == "__main__":
    root = os.path.dirname(sys.argv[0])
    os.chdir(root)

    style_guide = flake8.get_style_guide()

    input_files = sys.argv[1:]

    report = style_guide.check_files(input_files)

    assert report.get_statistics('E') == [], 'Flake8 found violations'

    print("All good :)")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants