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 9, 2025
1 parent a25b760 commit fa9bfb7
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 55 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 @@ -44,6 +44,7 @@ django-paypal = { path = "../python-packages/django-paypal", develop = true }
paypal-api = { path = "../python-packages/paypal-api", develop = true }
ts-subsys = { path = "../python-packages/ts-subsys", develop = true }
ts-scanners = { path = "../python-packages/ts-scanners", develop = true }
flake8-thunderstore = { path = "../flake8_thunderstore", develop = true }

[tool.poetry.dev-dependencies]
pytest = "^6.2"
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/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .visibility_coding_conventions import VisibilityCodingConventions
36 changes: 36 additions & 0 deletions flake8_thunderstore/plugins/visibility_coding_conventions.py
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.3"

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),
)
14 changes: 14 additions & 0 deletions flake8_thunderstore/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from setuptools import find_packages, setup

setup(
name="flake8-thunderstore",
version="1.0.0",
description="Custom Flake8 plugins for Thunderstore",
author="Your Name",
packages=find_packages(),
entry_points={
"flake8.extension": [
"VIS753 = plugins:VisibilityCodingConventions",
],
},
)
2 changes: 1 addition & 1 deletion python-packages

0 comments on commit fa9bfb7

Please sign in to comment.