-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Straighten out error handling via a thread-local (but otherwise globa…
…l) context (#1327) * Introduce an ErrorContext manager. * Content._carry won't be needing its 3rd argument anymore. * Replace NestedIndexError with nested_indexerror. * Remove NestedIndexError entirely. Using ErrorContext now. * Move error-handling to ak._v2._util. * Every exception should pass through ak._v2._util.error. * ak._v2._util.error can now check for contexts. * Defined error format for operations. * Now every operation sets the OperationErrorContext. * Now they do (I missed a big batch last time). * Avoid reentering the OperationErrorContext. * Make reducers non-reenterant, too. * Merged with main. * chore: add a custom flake8 check * Fix that one NotImplementedError. Co-authored-by: Henry Schreiner <[email protected]>
- Loading branch information
Showing
148 changed files
with
3,532 additions
and
1,687 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import ast | ||
import sys | ||
from typing import NamedTuple, Iterator | ||
|
||
|
||
class Flake8ASTErrorInfo(NamedTuple): | ||
line_number: int | ||
offset: int | ||
msg: str | ||
cls: type # unused | ||
|
||
|
||
class Visitor(ast.NodeVisitor): | ||
msg = "AK101 exception must be wrapped in ak._v2._util.*error" | ||
|
||
def __init__(self): | ||
self.errors: list[Flake8ASTErrorInfo] = [] | ||
|
||
def visit_Raise(self, node): | ||
if isinstance(node.exc, ast.Call): | ||
if isinstance(node.exc.func, ast.Attribute): | ||
if node.exc.func.attr in {"error", "indexerror"}: | ||
return | ||
if node.exc.func.id in {"ImportError"}: | ||
return | ||
|
||
self.errors.append( | ||
Flake8ASTErrorInfo(node.lineno, node.col_offset, self.msg, type(self)) | ||
) | ||
|
||
|
||
class AwkwardASTPlugin: | ||
name = "flake8_awkward" | ||
version = "0.0.0" | ||
|
||
def __init__(self, tree: ast.AST) -> None: | ||
self._tree = tree | ||
|
||
def run(self) -> Iterator[Flake8ASTErrorInfo]: | ||
visitor = Visitor() | ||
visitor.visit(self._tree) | ||
yield from visitor.errors | ||
|
||
|
||
def main(path): | ||
with open(path) as f: | ||
code = f.read() | ||
|
||
node = ast.parse(code) | ||
plugin = AwkwardASTPlugin(node) | ||
for err in plugin.run(): | ||
print(f"{path}:{err.line_number}:{err.offset} {err.msg}") | ||
|
||
|
||
if __name__ == "__main__": | ||
for item in sys.argv[1:]: | ||
main(item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.