Plugin that records all calls to a given target.
| 99 | |
| 100 | |
| 101 | class SuggestionPlugin(Plugin): |
| 102 | """Plugin that records all calls to a given target.""" |
| 103 | |
| 104 | def __init__(self, target: str) -> None: |
| 105 | if target.endswith((".__new__", ".__init__")): |
| 106 | target = target.rsplit(".", 1)[0] |
| 107 | |
| 108 | self.target = target |
| 109 | # List of call sites found by dmypy suggest: |
| 110 | # (path, line, <arg kinds>, <arg names>, <arg types>) |
| 111 | self.mystery_hits: list[Callsite] = [] |
| 112 | |
| 113 | def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None: |
| 114 | if fullname == self.target: |
| 115 | return self.log |
| 116 | else: |
| 117 | return None |
| 118 | |
| 119 | def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | None: |
| 120 | if fullname == self.target: |
| 121 | return self.log |
| 122 | else: |
| 123 | return None |
| 124 | |
| 125 | def log(self, ctx: FunctionContext | MethodContext) -> Type: |
| 126 | self.mystery_hits.append( |
| 127 | Callsite( |
| 128 | ctx.api.path, |
| 129 | ctx.context.line, |
| 130 | ctx.arg_kinds, |
| 131 | ctx.callee_arg_names, |
| 132 | ctx.arg_names, |
| 133 | ctx.arg_types, |
| 134 | ) |
| 135 | ) |
| 136 | return ctx.default_return_type |
| 137 | |
| 138 | |
| 139 | # NOTE: We could make this a bunch faster by implementing a StatementVisitor that skips |
no outgoing calls
no test coverage detected
searching dependent graphs…