Skip to content

Commit 5150344

Browse files
Fix crash in catching-non-exception when something besides a class in except (#10232) (#10234)
(cherry picked from commit 007a745) Co-authored-by: ChandanChainani <[email protected]>
1 parent 54f9825 commit 5150344

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

doc/whatsnew/fragments/10106.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash when something besides a class is found in an except handler.
2+
3+
Closes #10106

pylint/checkers/exceptions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,13 @@ def _check_catching_non_exception(
432432
return
433433
if all(
434434
node
435-
and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
435+
and (
436+
utils.inherit_from_std_ex(node)
437+
or (
438+
isinstance(node, nodes.ClassDef)
439+
and not utils.has_known_bases(node)
440+
)
441+
)
436442
for node in inferred
437443
):
438444
return

tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,11 @@ class SomeBase(UnknownError):
132132
raise ValueError
133133
except EXCEPTIONS:
134134
pass
135+
136+
137+
LAMBDA = lambda x: 1, 2
138+
139+
try:
140+
pass
141+
except LAMBDA: # [catching-non-exception]
142+
pass

tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ catching-non-exception:71:7:71:52::"Catching an exception which doesn't inherit
1010
catching-non-exception:84:7:84:26::"Catching an exception which doesn't inherit from Exception: NON_EXCEPTION_TUPLE":UNDEFINED
1111
catching-non-exception:102:7:102:13::"Catching an exception which doesn't inherit from Exception: object":UNDEFINED
1212
catching-non-exception:107:7:107:12::"Catching an exception which doesn't inherit from Exception: range":UNDEFINED
13+
catching-non-exception:141:7:141:13::"Catching an exception which doesn't inherit from Exception: LAMBDA":UNDEFINED

0 commit comments

Comments
 (0)