From 07f54f99426f912c89049544ded2e93e22a16f9b Mon Sep 17 00:00:00 2001 From: Musab Abdullah Date: Sun, 3 Nov 2024 23:28:41 -0600 Subject: [PATCH] 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 --- .flake8 | 6 ++++++ ci_cd/flake8_forbidden_calls.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 ci_cd/flake8_forbidden_calls.py diff --git a/.flake8 b/.flake8 index afd4596076..d8a484e0c5 100644 --- a/.flake8 +++ b/.flake8 @@ -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 \ No newline at end of file diff --git a/ci_cd/flake8_forbidden_calls.py b/ci_cd/flake8_forbidden_calls.py new file mode 100644 index 0000000000..7d44c4daf9 --- /dev/null +++ b/ci_cd/flake8_forbidden_calls.py @@ -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)) \ No newline at end of file