updated plugin to look for dotenv.load_dotenv also (attribute call vs direct call)

This commit is contained in:
Musab Abdullah 2024-11-04 11:45:27 -06:00
parent 07f54f9942
commit 48b4ec0312

View file

@ -1,10 +1,12 @@
"""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"
@ -14,6 +16,18 @@ class ForbiddenCallPlugin:
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))
if isinstance(node, ast.Call):
func_name = (
node.func.id
if isinstance(node.func, ast.Name)
else (
node.func.attr if isinstance(node.func, ast.Attribute) else None
)
)
if func_name in FORBIDDEN_CALLS:
yield (
node.lineno,
node.col_offset,
f"{PLUGIN_CODE} `{func_name}` is not allowed.",
type(self),
)