From ac1fb22705059a9430e4014cdca75e48daab757b Mon Sep 17 00:00:00 2001 From: samay2504 Date: Fri, 19 Dec 2025 16:34:12 +0530 Subject: [PATCH] fix: replace bare except with explicit Exception in import_runner.py Signed-off-by: samay2504 --- vulnerabilities/import_runner.py | 2 +- vulnerabilities/tests/test_bare_except_fix.py | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 vulnerabilities/tests/test_bare_except_fix.py diff --git a/vulnerabilities/import_runner.py b/vulnerabilities/import_runner.py index 5bcf5f461..4b2e67c21 100644 --- a/vulnerabilities/import_runner.py +++ b/vulnerabilities/import_runner.py @@ -219,7 +219,7 @@ def process_inferences(inferences: List[Inference], advisory: Advisory, improver }, ) vulnerability.severities.add(vulnerability_severity) - except: + except Exception: logger.error( f"Failed to create VulnerabilitySeverity for: {severity} with error:\n{traceback_format_exc()}" ) diff --git a/vulnerabilities/tests/test_bare_except_fix.py b/vulnerabilities/tests/test_bare_except_fix.py new file mode 100644 index 000000000..60991f481 --- /dev/null +++ b/vulnerabilities/tests/test_bare_except_fix.py @@ -0,0 +1,28 @@ +# +# Copyright (c) nexB Inc. and others. All rights reserved. +# VulnerableCode is a trademark of nexB Inc. +# SPDX-License-Identifier: Apache-2.0 +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. +# See https://github.com/aboutcode-org/vulnerablecode for support or download. +# See https://aboutcode.org for more information about nexB OSS projects. +# + +import ast +import os + + +def test_no_bare_except_in_import_runner(): + """Test that import_runner.py does not contain bare except clauses.""" + file_path = os.path.join(os.path.dirname(__file__), "..", "import_runner.py") + with open(file_path, "r") as f: + source = f.read() + + tree = ast.parse(source) + + bare_excepts = [] + for node in ast.walk(tree): + if isinstance(node, ast.ExceptHandler): + if node.type is None: + bare_excepts.append(node.lineno) + + assert len(bare_excepts) == 0, f"Found bare except clauses at lines: {bare_excepts}"