added a local flake8 plugin to check if load_dotenv is ever called. plugin can be extended to check for calls to other 'forbidden' functions

This commit is contained in:
Musab Abdullah 2024-11-03 23:28:41 -06:00
parent 8413603b98
commit 07f54f9942
2 changed files with 25 additions and 0 deletions

View file

@ -44,3 +44,9 @@ ignore =
# https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#flake8
extend-ignore = E203
[flake8:local-plugins]
extension =
FC1 = flake8_forbidden_calls:ForbiddenCallPlugin
paths =
./ci_cd

View file

@ -0,0 +1,19 @@
"""Flake8 Extension that finds usage of load_dotenv."""
import ast
from typing import Generator
FORBIDDEN_CALLS = ["load_dotenv"]
PLUGIN_CODE = "FC1"
class ForbiddenCallPlugin:
name = "flake8_forbidden_calls"
version = "1.0.0"
def __init__(self, tree: ast.AST):
self._tree = tree
def run(self) -> Generator:
for node in ast.walk(self._tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
if node.func.id in FORBIDDEN_CALLS:
yield (node.lineno, node.col_offset, f"{PLUGIN_CODE} `{node.func.id}` is not allowed.", type(self))