|
7 | 7 | # TODO: all of these functions where code_text is given should also be able to read a file at a given path (?) |
8 | 8 |
|
9 | 9 |
|
10 | | -def _python_ast_exception_name(node: Union[ast.Raise, ast.ExceptHandler]) -> str: |
| 10 | +def _python_ast_exception_name(node: Union[ast.Raise, ast.ExceptHandler]) -> str: # noqa: CCR001 |
11 | 11 | """.""" |
12 | 12 | if hasattr(node, 'exc') and node.exc: # this handles ast.Raise nodes |
13 | 13 | if hasattr( |
@@ -41,38 +41,34 @@ def _python_ast_exception_name(node: Union[ast.Raise, ast.ExceptHandler]) -> str |
41 | 41 |
|
42 | 42 | def python_ast_raise_name(node: ast.Raise) -> Optional[str]: |
43 | 43 | """Get the name of the exception raise by the given ast.Raise object.""" |
44 | | - if isinstance(node, ast.Raise): |
45 | | - return _python_ast_exception_name(node) |
| 44 | + return _python_ast_exception_name(node) |
46 | 45 |
|
47 | 46 |
|
48 | 47 | def python_ast_exception_handler_exceptions_handled(handler: ast.ExceptHandler) -> Optional[Iterable[str]]: |
49 | 48 | """Return all of the exceptions handled by the given exception handler.""" |
50 | | - if isinstance(handler, ast.ExceptHandler): |
51 | | - handler_has_multiple_exceptions = handler.type and hasattr(handler.type, 'elts') |
52 | | - if handler_has_multiple_exceptions: |
53 | | - yield from (_python_ast_exception_name(i) for i in handler.type.elts) |
54 | | - else: |
55 | | - exception_name = _python_ast_exception_name(handler) |
56 | | - if exception_name: |
57 | | - yield exception_name |
| 49 | + handler_has_multiple_exceptions = handler.type and hasattr(handler.type, 'elts') |
| 50 | + if handler_has_multiple_exceptions: |
| 51 | + yield from (_python_ast_exception_name(i) for i in handler.type.elts) |
| 52 | + else: |
| 53 | + exception_name = _python_ast_exception_name(handler) |
| 54 | + if exception_name: |
| 55 | + yield exception_name |
58 | 56 |
|
59 | 57 |
|
60 | 58 | def python_ast_exception_handler_exceptions_raised(handler: ast.ExceptHandler) -> Optional[Iterable[str]]: |
61 | 59 | """Return the exception raised by the given exception handler.""" |
62 | | - if isinstance(handler, ast.ExceptHandler): |
63 | | - raise_nodes = list(python_ast_objects_of_type(handler, ast.Raise)) |
64 | | - if any(raise_nodes): |
65 | | - exceptions_names = list(map(python_ast_raise_name, raise_nodes)) |
66 | | - for name in exceptions_names: |
67 | | - if name and name == handler.name: |
68 | | - exceptions_names = iterable_replace( |
69 | | - exceptions_names, name, python_ast_exception_handler_exceptions_handled(handler) |
70 | | - ) |
71 | | - elif name is None: |
72 | | - exceptions_names = iterable_replace( |
73 | | - exceptions_names, name, python_ast_exception_handler_exceptions_handled(handler) |
74 | | - ) |
75 | | - yield from more_itertools.collapse(exceptions_names, base_type=str) |
| 60 | + raise_nodes = python_ast_objects_of_type(handler, ast.Raise) |
| 61 | + exceptions_names = list(map(python_ast_raise_name, raise_nodes)) |
| 62 | + for name in exceptions_names: |
| 63 | + if name and name == handler.name: |
| 64 | + exceptions_names = iterable_replace( |
| 65 | + exceptions_names, name, python_ast_exception_handler_exceptions_handled(handler) |
| 66 | + ) |
| 67 | + elif name is None: |
| 68 | + exceptions_names = iterable_replace( |
| 69 | + exceptions_names, name, python_ast_exception_handler_exceptions_handled(handler) |
| 70 | + ) |
| 71 | + yield from more_itertools.collapse(exceptions_names, base_type=str) |
76 | 72 |
|
77 | 73 |
|
78 | 74 | def python_exceptions_handled(code_text: str) -> Iterable[str]: |
@@ -145,7 +141,7 @@ def _python_ast_clean(code_text: str) -> str: |
145 | 141 |
|
146 | 142 |
|
147 | 143 | # TODO: have a decorator to parse a first argument that is a string |
148 | | -def python_ast_objects_of_type( |
| 144 | +def python_ast_objects_of_type( # noqa: CCR001 |
149 | 145 | code_text_or_ast_object: Union[str, object], ast_type: type, *, recursive_search: bool = True |
150 | 146 | ) -> Iterable[object]: |
151 | 147 | """Return all of the ast objects of the given ast_type in the code_text_or_ast_object.""" |
|
0 commit comments