summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorJames Addison <55152140+jayaddison@users.noreply.github.com>2023-01-20 14:23:42 +0000
committerGitHub <noreply@github.com>2023-01-20 14:23:42 +0000
commit14f74d5c703e54bfd0ed1f48435f7a60a674f1ea (patch)
treeba876ca99f33b40b5a8ed54d6723fa22c40100c2 /pylint
parentb08f99a6ee3affed01ef7b6f1847a49865e9b2ad (diff)
downloadpylint-git-14f74d5c703e54bfd0ed1f48435f7a60a674f1ea.tar.gz
pointless-exception-statement: filter call inference for better performance on large codebases (#8078)
Filtering to to names that begin with an uppercase character seems to be sufficient, See the full conversation on Github for details. Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Diffstat (limited to 'pylint')
-rw-r--r--pylint/checkers/base/basic_checker.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/pylint/checkers/base/basic_checker.py b/pylint/checkers/base/basic_checker.py
index 67be99a8c..01caa0770 100644
--- a/pylint/checkers/base/basic_checker.py
+++ b/pylint/checkers/base/basic_checker.py
@@ -453,21 +453,30 @@ class BasicChecker(_BasicChecker):
# Warn W0133 for exceptions that are used as statements
if isinstance(expr, nodes.Call):
- inferred = utils.safe_infer(expr)
+ name = ""
+ if isinstance(expr.func, nodes.Name):
+ name = expr.func.name
+ elif isinstance(expr.func, nodes.Attribute):
+ name = expr.func.attrname
+
+ # Heuristic: only run inference for names that begin with an uppercase char
+ # This reduces W0133's coverage, but retains acceptable runtime performance
+ # For more details, see: https://github.com/PyCQA/pylint/issues/8073
+ inferred = utils.safe_infer(expr) if name[:1].isupper() else None
if isinstance(inferred, objects.ExceptionInstance):
self.add_message(
"pointless-exception-statement", node=node, confidence=INFERENCE
)
+ return
# Ignore if this is :
- # * a direct function call
# * the unique child of a try/except body
# * a yield statement
# * an ellipsis (which can be used on Python 3 instead of pass)
# warn W0106 if we have any underlying function call (we can't predict
# side effects), else pointless-statement
if (
- isinstance(expr, (nodes.Yield, nodes.Await, nodes.Call))
+ isinstance(expr, (nodes.Yield, nodes.Await))
or (isinstance(node.parent, nodes.TryExcept) and node.parent.body == [node])
or (isinstance(expr, nodes.Const) and expr.value is Ellipsis)
):