-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
560406b
commit 9bb112a
Showing
3 changed files
with
64 additions
and
1 deletion.
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
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,61 @@ | ||
import ast | ||
|
||
from tryceratops.violations import codes | ||
|
||
from .base import BaseAnalyzer | ||
|
||
STANDARD_NON_TYPE_ERROR_IDS = ( | ||
"ArithmeticError", | ||
"AssertionError", | ||
"AttributeError", | ||
"BufferError", | ||
"EOFError", | ||
"Exception", | ||
"ImportError", | ||
"LookupError", | ||
"MemoryError", | ||
"NameError", | ||
"ReferenceError", | ||
"RuntimeError", | ||
"SyntaxError", | ||
"SystemError", | ||
"ValueError", | ||
) | ||
|
||
|
||
class PreferTypeErrorAnalyzer(BaseAnalyzer): | ||
violation_code = codes.PREFER_TYPE_ERROR | ||
|
||
def _is_checking_type(self, node) -> bool: | ||
if isinstance(node, ast.If): | ||
return self._is_checking_type(node.test) | ||
if isinstance(node, ast.UnaryOp): | ||
return self._is_checking_type(node.operand) | ||
if isinstance(node, ast.BoolOp): | ||
for value in node.values: | ||
if self._is_checking_type(value): | ||
return True | ||
if isinstance(node, ast.Call): | ||
if isinstance(node.func, ast.Name): | ||
if node.func.id in ("isinstance", "issubclass", "callable"): | ||
return True | ||
return False | ||
|
||
def _check_is_raise_other_than_typeerror(self, node: ast.Raise): | ||
if isinstance(node.exc, ast.Call): | ||
if isinstance(node.exc.func, ast.Name): | ||
if node.exc.func.id in STANDARD_NON_TYPE_ERROR_IDS: | ||
self._mark_violation((node.exc.func)) | ||
elif isinstance(node.exc, ast.Name): | ||
if node.exc.id in STANDARD_NON_TYPE_ERROR_IDS: | ||
self._mark_violation((node.exc)) | ||
|
||
def visit_If(self, node: ast.If) -> None: | ||
if self._is_checking_type(node): | ||
for stm in ast.iter_child_nodes(node): | ||
if isinstance(stm, ast.Raise): | ||
self._check_is_raise_other_than_typeerror(stm) | ||
else: | ||
for stm in node.orelse: | ||
if isinstance(stm, ast.If): | ||
self.visit_If(stm) |
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