From 88a8550b2bf2e7b1d72e24b0ca448c952114a754 Mon Sep 17 00:00:00 2001 From: ChandanChainani Date: Sun, 16 Feb 2025 00:02:43 +0530 Subject: [PATCH] Fix crash in catching-non-exception when something besides a class in except (#10232) (cherry picked from commit 007a745c8619c2cbf59f829a8f09fc6afa6eb0f1) --- doc/whatsnew/fragments/10106.bugfix | 3 +++ pylint/checkers/exceptions.py | 8 +++++++- .../invalid_exceptions/invalid_exceptions_caught.py | 8 ++++++++ .../invalid_exceptions/invalid_exceptions_caught.txt | 1 + 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/10106.bugfix diff --git a/doc/whatsnew/fragments/10106.bugfix b/doc/whatsnew/fragments/10106.bugfix new file mode 100644 index 0000000000..7309e5b949 --- /dev/null +++ b/doc/whatsnew/fragments/10106.bugfix @@ -0,0 +1,3 @@ +Fix a crash when something besides a class is found in an except handler. + +Closes #10106 diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py index 3834f260be..258c640c1b 100644 --- a/pylint/checkers/exceptions.py +++ b/pylint/checkers/exceptions.py @@ -432,7 +432,13 @@ def _check_catching_non_exception( return if all( node - and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node)) + and ( + utils.inherit_from_std_ex(node) + or ( + isinstance(node, nodes.ClassDef) + and not utils.has_known_bases(node) + ) + ) for node in inferred ): return diff --git a/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py b/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py index 1b513f9724..0c3589c311 100644 --- a/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py +++ b/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py @@ -132,3 +132,11 @@ class SomeBase(UnknownError): raise ValueError except EXCEPTIONS: pass + + +LAMBDA = lambda x: 1, 2 + +try: + pass +except LAMBDA: # [catching-non-exception] + pass diff --git a/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt b/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt index 6d35a1e22e..905f35f129 100644 --- a/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt +++ b/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt @@ -10,3 +10,4 @@ catching-non-exception:71:7:71:52::"Catching an exception which doesn't inherit catching-non-exception:84:7:84:26::"Catching an exception which doesn't inherit from Exception: NON_EXCEPTION_TUPLE":UNDEFINED catching-non-exception:102:7:102:13::"Catching an exception which doesn't inherit from Exception: object":UNDEFINED catching-non-exception:107:7:107:12::"Catching an exception which doesn't inherit from Exception: range":UNDEFINED +catching-non-exception:141:7:141:13::"Catching an exception which doesn't inherit from Exception: LAMBDA":UNDEFINED