| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import ast |
| 4 |
import typing as t |
| 5 |
|
| 6 |
CODEMAP: dict[str, str] = { |
| 7 |
# SDK001 is necessary for SDK002 enforcement to be easy |
| 8 |
# otherwise, we would have to have a more sophisticated linter which knows about |
| 9 |
# lexical scopes! |
| 10 |
"SDK001": "SDK001 loggers should be named 'log'", |
| 11 |
"SDK002": "SDK002 never use 'log.info'", |
| 12 |
# don't do `isinstance(x, MissingType)` -- use `x is MISSING` instead |
| 13 |
"SDK003": "SDK003 use `is MISSING`, not `isinstance(..., MissingType)`", |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
class Plugin: |
| 18 |
name = "globus-sdk-flake8" |
| 19 |
version = "1.0.0" |
| 20 |
|
| 21 |
# args to init determine plugin behavior. see: |
| 22 |
# https://flake8.pycqa.org/en/latest/plugin-development/plugin-parameters.html#indicating-desired-data |
| 23 |
# |
| 24 |
# by having "tree" as an init arg, we tell flake8 that we are an AST-handling |
| 25 |
# plugin, run once per file |
| 26 |
def __init__(self, tree: ast.AST) -> None: |
| 27 |
self.tree = tree |
| 28 |
|
| 29 |
# Plugin.run() is how checks will run. For detail, see implementation of: |
| 30 |
# https://flake8.pycqa.org/en/latest/internal/checker.html#flake8.checker.FileChecker.run_ast_checks |
| 31 |
def run(self) -> t.Iterator[tuple[int, int, str, type]]: |
| 32 |
visitor = SDKVisitor() |
| 33 |
visitor.visit(self.tree) |
| 34 |
for lineno, col, code in visitor.collect: |
| 35 |
yield lineno, col, CODEMAP[code], type(self) |
| 36 |
|
| 37 |
|
| 38 |
class SDKVisitor(ast.NodeVisitor): |
| 39 |
def __init__(self) -> None: |
| 40 |
super().__init__() |
| 41 |
self.collect: list[tuple[int, int, str]] = [] |
| 42 |
|
| 43 |
def _record(self, node: ast.expr | ast.stmt, code: str) -> None: |
| 44 |
self.collect.append((node.lineno, node.col_offset, code)) |
| 45 |
|
| 46 |
def visit_Assign(self, node: ast.Assign) -> None: |
| 47 |
if matches_sdk001(node): |
| 48 |
self._record(node, "SDK001") |
| 49 |
|
| 50 |
self.generic_visit(node) |
| 51 |
|
| 52 |
def visit_Call(self, node: ast.Call) -> None: |
| 53 |
if matches_sdk003(node): |
| 54 |
self._record(node, "SDK003") |
| 55 |
elif matches_sdk002(node): |
| 56 |
self._record(node, "SDK002") |
| 57 |
|
| 58 |
self.generic_visit(node) |
| 59 |
|
| 60 |
|
| 61 |
def matches_sdk001(node: ast.Assign) -> bool: |
| 62 |
""" |
| 63 |
A matcher for the SDK001 lint rule. |
| 64 |
|
| 65 |
Checks for `x = logging.getLogger(__name__)` where `x` is not `log`. |
| 66 |
|
| 67 |
:param node: the assignment statement AST node to check |
| 68 |
""" |
| 69 |
# the value must be a call and must be using attr access in the function call |
| 70 |
# this eliminates bare funcs like `x = foo()` but allows `x = foo.bar()` |
| 71 |
if not ( |
| 72 |
isinstance(node.value, ast.Call) and isinstance(node.value.func, ast.Attribute) |
| 73 |
): |
| 74 |
return False |
| 75 |
|
| 76 |
call_node: ast.Call = node.value |
| 77 |
func_node: ast.Attribute = node.value.func |
| 78 |
|
| 79 |
# make sure it's 'logging.getLogger' and no other function |
| 80 |
if not ( |
| 81 |
func_node.attr == "getLogger" |
| 82 |
and isinstance(func_node.value, ast.Name) |
| 83 |
and func_node.value.id == "logging" |
| 84 |
): |
| 85 |
return False |
| 86 |
|
| 87 |
# the assignee must be a single variable and it must be a name node |
| 88 |
if not (len(node.targets) == 1 and isinstance(node.targets[0], ast.Name)): |
| 89 |
return False |
| 90 |
|
| 91 |
# if the assigned name is `log`, then it cannot be a match |
| 92 |
name_node: ast.Name = node.targets[0] |
| 93 |
if name_node.id == "log": |
| 94 |
return False |
| 95 |
|
| 96 |
# confirm that the `logging.getLogger` args are a single name -- that's the |
| 97 |
# form it will be when `__name__` is passed |
| 98 |
if len(call_node.args) != 1 or not isinstance(call_node.args[0], ast.Name): |
| 99 |
return False |
| 100 |
|
| 101 |
# if the argument is some other name, e.g. `logging.getLogger(my_variable)`, |
| 102 |
# then that cannot be a match |
| 103 |
logger_arg: ast.Name = call_node.args[0] |
| 104 |
if logger_arg.id != "__name__": |
| 105 |
return False |
| 106 |
|
| 107 |
return True # all conditions met! |
| 108 |
|
| 109 |
|
| 110 |
def matches_sdk002(node: ast.Call) -> bool: |
| 111 |
""" |
| 112 |
A matcher for the SDK002 lint rule. |
| 113 |
|
| 114 |
Checks for `log.info(...)` |
| 115 |
|
| 116 |
:param node: the function call AST node to check |
| 117 |
""" |
| 118 |
func_node = node.func |
| 119 |
# the function call must be of the form 'OBJ.info(...)' |
| 120 |
if not (isinstance(func_node, ast.Attribute) and func_node.attr == "info"): |
| 121 |
return False |
| 122 |
|
| 123 |
# and, more specifically, the object must be named 'log', so |
| 124 |
# it's `log.info(...) |
| 125 |
if not (isinstance(func_node.value, ast.Name) and func_node.value.id == "log"): |
| 126 |
return False |
| 127 |
|
| 128 |
return True # all conditions met! |
| 129 |
|
| 130 |
|
| 131 |
def matches_sdk003(node: ast.Call) -> bool: |
| 132 |
""" |
| 133 |
A matcher for the SDK003 lint rule. |
| 134 |
|
| 135 |
Checks for `isinstance(x, MissingType)` |
| 136 |
|
| 137 |
:param node: the function call AST node to check |
| 138 |
""" |
| 139 |
# it must be a call to a function named "isinstance" |
| 140 |
if not (isinstance(node.func, ast.Name) and node.func.id == "isinstance"): |
| 141 |
return False |
| 142 |
# if the number of arguments is improper, it can't be a violation (not a |
| 143 |
# valid 'isinstance' call) |
| 144 |
if len(node.args) != 2: |
| 145 |
return False |
| 146 |
right_hand_side = node.args[1] |
| 147 |
# the second argument must be the name 'MissingType' |
| 148 |
if not ( |
| 149 |
isinstance(right_hand_side, ast.Name) and right_hand_side.id == "MissingType" |
| 150 |
): |
| 151 |
return False |
| 152 |
|
| 153 |
return True # all conditions met! |