Skip to content

Commit

Permalink
Add custom Flake8 plugin
Browse files Browse the repository at this point in the history
Create a new python project for flake8 plugins, currently only containing
a plugin to test for unsafe usage of models with visibility
  • Loading branch information
x753 committed Jan 14, 2025
1 parent a25b760 commit 39acac4
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 54 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RUN apt-get update && apt-get install -y \
curl build-essential git \
&& rm -rf /var/lib/apt/lists/*

COPY ./flake8-thunderstore/ /flake8-thunderstore
COPY ./python-packages/ /python-packages
COPY ./django/pyproject.toml ./django/poetry.lock /app/

Expand Down
16 changes: 15 additions & 1 deletion django/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions django/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ flake8-comprehensions = "^3.3.1"
flake8-pie = "^0.6.1"
flake8-printf-formatting = "^1.1.0"
flake8-pytest-style = "^1.3.0"
flake8-thunderstore = { path = "../flake8-thunderstore", develop = true }
pep8-naming = "^0.11.1"
watchdog = {extras = ["watchmedo"], version = "^1.0.2"}
mypy-boto3-s3 = "^1.17.47"
Expand Down
53 changes: 0 additions & 53 deletions django/thunderstore/core/tests/test_coding_conventions.py

This file was deleted.

1 change: 1 addition & 0 deletions flake8-thunderstore/flake8_thunderstore/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .visibility_coding_conventions import VisibilityCodingConventions
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import ast

from flake8_plugin_utils import Plugin


class VisibilityCodingConventions(Plugin):
name = "visibility-coding-conventions"
version = "1.0.1"

visibility_classes = ["PackageListing", "PackageVersion"]
safe_functions = ["public_list", "system", "get", "create", "get_or_create"]
excluded_filepaths = ["/migrations/", "/tests/", "/commands/", "/conftest.py"]

def __init__(self, tree: ast.AST, filename: str) -> None:
self.tree = tree
self.filename = filename

def run(self):
if any(excluded in self.filename for excluded in self.excluded_filepaths):
return

for node in ast.walk(self.tree):
if (
isinstance(node, ast.Attribute)
and isinstance(node.value, ast.Attribute)
and node.value.attr == "objects"
and isinstance(node.value.value, ast.Name)
and node.value.value.id in self.visibility_classes
and node.attr not in self.safe_functions
):
yield (
node.lineno,
node.col_offset,
f"VIS753 {node.value.value.id}.objects.{node.attr} is unsafe; Objects with visibility should always be called with .public_list() or .system())",
type(self),
)
18 changes: 18 additions & 0 deletions flake8-thunderstore/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[tool.poetry]
name = "flake8-thunderstore"
version = "1.0.2"
description = "Custom Flake8 plugins for Thunderstore"
authors = ["753 <[email protected]>"]
packages = [
{ include = "flake8_thunderstore" },
]

[tool.poetry.dependencies]
python = "*"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.plugins."flake8.extension"]
VIS = 'flake8_thunderstore.visibility_coding_conventions:VisibilityCodingConventions'

0 comments on commit 39acac4

Please sign in to comment.