-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
7 changed files
with
72 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
flake8-thunderstore/flake8_thunderstore/visibility_coding_conventions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |