From 6e7228eeff4d683be50cb15e3bd48a8200ed6437 Mon Sep 17 00:00:00 2001 From: haosenwang1018 <167664334+haosenwang1018@users.noreply.github.com> Date: Thu, 26 Feb 2026 01:50:34 +0000 Subject: [PATCH] fix: replace 32 bare except clauses with except Exception --- .test-infra/metrics/sync/github/sync.py | 4 ++-- .../metrics/sync/jenkins/syncjenkins.py | 2 +- .../discussion-docs/generate_doc_md.py | 4 ++-- .../python-challenge/task.py | 2 +- .../python-solution/task.py | 2 +- .../python-challenge/task.py | 2 +- .../python-solution/task.py | 2 +- .../python-challenge/task.py | 2 +- .../python-solution/task.py | 2 +- .../license_scripts/pull_licenses_java.py | 20 +++++++++---------- .../extensions/python/bootstrap_beam_venv.py | 2 +- sdks/python/apache_beam/dataframe/io_test.py | 4 ++-- .../io/aws/clients/s3/fake_client.py | 2 +- sdks/python/apache_beam/metrics/cells_test.py | 2 +- .../integration/tests/screen_diff_test.py | 2 +- .../fn_api_runner/fn_runner_test.py | 4 ++-- .../apache_beam/runners/portability/stager.py | 2 +- .../testing/load_tests/load_test.py | 2 +- .../license_scripts/pull_licenses_py.py | 2 +- 19 files changed, 32 insertions(+), 32 deletions(-) diff --git a/.test-infra/metrics/sync/github/sync.py b/.test-infra/metrics/sync/github/sync.py index 543b19476c10..ff25c5d66249 100644 --- a/.test-infra/metrics/sync/github/sync.py +++ b/.test-infra/metrics/sync/github/sync.py @@ -99,7 +99,7 @@ def initDBConnection(): conn = psycopg2.connect( f"dbname='{DB_NAME}' user='{DB_USER_NAME}' host='{DB_HOST}'" f" port='{DB_PORT}' password='{DB_PASSWORD}'") - except: + except Exception: print('Failed to connect to DB; retrying in 1 minute') sys.stdout.flush() time.sleep(60) @@ -452,7 +452,7 @@ def fetchNewData(): data = None try: data = jsonData["data"]["search"]["edges"] - except: + except Exception: # TODO This means that API returned error. # We might want to bring this to stderr or utilize other means of logging. # Examples: we hit throttling, etc diff --git a/.test-infra/metrics/sync/jenkins/syncjenkins.py b/.test-infra/metrics/sync/jenkins/syncjenkins.py index 32bbf1fff2e9..ef0c31ca1803 100644 --- a/.test-infra/metrics/sync/jenkins/syncjenkins.py +++ b/.test-infra/metrics/sync/jenkins/syncjenkins.py @@ -75,7 +75,7 @@ def initConnection(): try: conn = psycopg2.connect(f"dbname='{dbname}' user='{dbusername}' host='{host}'" f" port='{port}' password='{dbpassword}'") - except: + except Exception: print('Failed to connect to DB; retrying in 1 minute') time.sleep(60) return conn diff --git a/contributor-docs/discussion-docs/generate_doc_md.py b/contributor-docs/discussion-docs/generate_doc_md.py index 4f36802ae0d9..4cba99535a03 100644 --- a/contributor-docs/discussion-docs/generate_doc_md.py +++ b/contributor-docs/discussion-docs/generate_doc_md.py @@ -123,7 +123,7 @@ def convert_to_timestamp(date_string): date_format = "%a, %d %b %Y %H:%M:%S %z" datetime_obj = datetime.datetime.strptime(date_string, date_format) return datetime_obj.timestamp() - except: + except Exception: return None @@ -238,7 +238,7 @@ def find_google_docs_links(mbox_file, doc_messages, doc_urls): title = get_google_doc_title(doc_url) try: sender = extract_name_re(str(message["From"])) - except: + except Exception: print("Something is wrong: ", message["From"]) sender = None if not sender: diff --git a/learning/tour-of-beam/learning-content/common-transforms/motivating-challenge/python-challenge/task.py b/learning/tour-of-beam/learning-content/common-transforms/motivating-challenge/python-challenge/task.py index 3b96f064cc5b..b0da7f3308ce 100644 --- a/learning/tour-of-beam/learning-content/common-transforms/motivating-challenge/python-challenge/task.py +++ b/learning/tour-of-beam/learning-content/common-transforms/motivating-challenge/python-challenge/task.py @@ -56,7 +56,7 @@ def tryParseTaxiRideCost(line,index): if(len(line) > index): try: yield float(line[index]) - except: + except Exception: yield float(0) else: yield float(0) diff --git a/learning/tour-of-beam/learning-content/common-transforms/motivating-challenge/python-solution/task.py b/learning/tour-of-beam/learning-content/common-transforms/motivating-challenge/python-solution/task.py index 3c3a5aa94e7a..e7457d0bf59c 100644 --- a/learning/tour-of-beam/learning-content/common-transforms/motivating-challenge/python-solution/task.py +++ b/learning/tour-of-beam/learning-content/common-transforms/motivating-challenge/python-solution/task.py @@ -56,7 +56,7 @@ def tryParseTaxiRideCost(line,index): if(len(line) > index): try: yield float(line[index]) - except: + except Exception: yield float(0) else: yield float(0) diff --git a/learning/tour-of-beam/learning-content/triggers/motivating-challenge/python-challenge/task.py b/learning/tour-of-beam/learning-content/triggers/motivating-challenge/python-challenge/task.py index 62dbbdbb511b..cd325780bb4c 100644 --- a/learning/tour-of-beam/learning-content/triggers/motivating-challenge/python-challenge/task.py +++ b/learning/tour-of-beam/learning-content/triggers/motivating-challenge/python-challenge/task.py @@ -60,7 +60,7 @@ def tryParseTaxiRideCost(line,index): if(len(line) > index): try: yield float(line[index]) - except: + except Exception: yield float(0) else: yield float(0) diff --git a/learning/tour-of-beam/learning-content/triggers/motivating-challenge/python-solution/task.py b/learning/tour-of-beam/learning-content/triggers/motivating-challenge/python-solution/task.py index 39894355e5af..4f09b9c18766 100644 --- a/learning/tour-of-beam/learning-content/triggers/motivating-challenge/python-solution/task.py +++ b/learning/tour-of-beam/learning-content/triggers/motivating-challenge/python-solution/task.py @@ -60,7 +60,7 @@ def tryParseTaxiRideCost(line,index): if(len(line) > index): try: yield float(line[index]) - except: + except Exception: yield float(0) else: yield float(0) diff --git a/learning/tour-of-beam/learning-content/windowing/motivating-challenge/python-challenge/task.py b/learning/tour-of-beam/learning-content/windowing/motivating-challenge/python-challenge/task.py index d38752810d0e..c424b22c8f0d 100644 --- a/learning/tour-of-beam/learning-content/windowing/motivating-challenge/python-challenge/task.py +++ b/learning/tour-of-beam/learning-content/windowing/motivating-challenge/python-challenge/task.py @@ -57,7 +57,7 @@ def tryParseTaxiRideCost(line,index): if(len(line) > index): try: yield float(line[index]) - except: + except Exception: yield float(0) else: yield float(0) diff --git a/learning/tour-of-beam/learning-content/windowing/motivating-challenge/python-solution/task.py b/learning/tour-of-beam/learning-content/windowing/motivating-challenge/python-solution/task.py index 3708047906af..c5415a17801d 100644 --- a/learning/tour-of-beam/learning-content/windowing/motivating-challenge/python-solution/task.py +++ b/learning/tour-of-beam/learning-content/windowing/motivating-challenge/python-solution/task.py @@ -56,7 +56,7 @@ def tryParseTaxiRideCost(line,index): if(len(line) > index): try: yield float(line[index]) - except: + except Exception: yield float(0) else: yield float(0) diff --git a/sdks/java/container/license_scripts/pull_licenses_java.py b/sdks/java/container/license_scripts/pull_licenses_java.py index f0c1b48468f5..1f0965b55fdd 100644 --- a/sdks/java/container/license_scripts/pull_licenses_java.py +++ b/sdks/java/container/license_scripts/pull_licenses_java.py @@ -144,7 +144,7 @@ def pull_source_code(base_url, dir_name, dep): try: soup = BeautifulSoup(urlopen(Request(base_url, headers={ 'User-Agent': 'Apache Beam'})).read(), "html.parser") - except: + except Exception: logging.error('Error reading source base from {base_url}'.format(base_url=base_url)) raise source_count = 0 @@ -173,7 +173,7 @@ def write_to_csv(csv_list): writer.writeheader() for data in csv_list: writer.writerow(data) - except: + except Exception: traceback.print_exc() raise @@ -210,10 +210,10 @@ def execute(dep): # pull license try: license_url = dep_config[name][version]['license'] - except: + except Exception: try: license_url = dep['moduleLicenseUrl'] - except: + except Exception: # url cannot be found, add to no_licenses and skip to pull. with thread_lock: no_licenses.append(name_version) @@ -230,12 +230,12 @@ def execute(dep): try: notice_url = dep_config[name][version]['notice'] pull_from_url(dir_name + '/NOTICE', notice_url, name_version) - except: + except Exception: pass else: try: license_url = dep['moduleLicenseUrl'] - except: + except Exception: license_url = '' logging.debug( 'License/notice for {name_version} were pulled automatically.'. @@ -244,10 +244,10 @@ def execute(dep): # get license_type to decide if pull source code. try: license_type = dep['moduleLicense'] - except: + except Exception: try: license_type = dep_config[name][version]['type'] - except: + except Exception: license_type = 'no_license_type' with thread_lock: no_license_type.append(name_version) @@ -256,7 +256,7 @@ def execute(dep): if any(x in license_type.lower() for x in SOURCE_CODE_REQUIRED_LICENSES): try: base_url = dep_config[name][version]['source'] - except: + except Exception: module = dep['moduleName'].split(':')[0].replace('.', '/') base_url = maven_url_temp.format(module=module + '/' + name, version=version) @@ -281,7 +281,7 @@ def read_cached_licenses(): try: CACHED_LICENSES=set(os.listdir(cached_license_path)) logging.info("Read %d licenses from cache.", len(CACHED_LICENSES)) - except: + except Exception: logging.warning("Error occurred when reading cached licenses.") traceback.print_exc() diff --git a/sdks/java/extensions/python/src/main/resources/org/apache/beam/sdk/extensions/python/bootstrap_beam_venv.py b/sdks/java/extensions/python/src/main/resources/org/apache/beam/sdk/extensions/python/bootstrap_beam_venv.py index f71a0b784bfd..872b849ff2ff 100644 --- a/sdks/java/extensions/python/src/main/resources/org/apache/beam/sdk/extensions/python/bootstrap_beam_venv.py +++ b/sdks/java/extensions/python/src/main/resources/org/apache/beam/sdk/extensions/python/bootstrap_beam_venv.py @@ -107,7 +107,7 @@ def main(): # Sanity check the installation. subprocess.run([venv_python, '-c', 'import apache_beam'], check=True) - except: + except Exception: shutil.rmtree(venv_dir) raise diff --git a/sdks/python/apache_beam/dataframe/io_test.py b/sdks/python/apache_beam/dataframe/io_test.py index 313d955b4550..ba93f0b46f65 100644 --- a/sdks/python/apache_beam/dataframe/io_test.py +++ b/sdks/python/apache_beam/dataframe/io_test.py @@ -213,7 +213,7 @@ def check(actual): actual = actual.rename( columns=dict(zip(actual.columns, expected.columns))) return assert_frame_equal(expected, actual, check_like=True) - except: + except Exception: print("EXPECTED") print(expected) print("ACTUAL") @@ -240,7 +240,7 @@ def check(actual): p | getattr(io, 'read_%s' % format)(dest + '*', **read_kwargs), yield_elements='pandas') assert_that(result, frame_equal_to(df, **check_options)) - except: + except Exception: os.system('head -n 100 ' + dest + '*') raise diff --git a/sdks/python/apache_beam/io/aws/clients/s3/fake_client.py b/sdks/python/apache_beam/io/aws/clients/s3/fake_client.py index e2d34a11a46f..8b345d173141 100644 --- a/sdks/python/apache_beam/io/aws/clients/s3/fake_client.py +++ b/sdks/python/apache_beam/io/aws/clients/s3/fake_client.py @@ -71,7 +71,7 @@ def add_file(self, f): def get_file(self, bucket, obj): try: return self.files[bucket, obj] - except: + except Exception: raise messages.S3ClientError('Not Found', 404) def delete_file(self, bucket, obj): diff --git a/sdks/python/apache_beam/metrics/cells_test.py b/sdks/python/apache_beam/metrics/cells_test.py index 11ea20ed6f6d..17521923a621 100644 --- a/sdks/python/apache_beam/metrics/cells_test.py +++ b/sdks/python/apache_beam/metrics/cells_test.py @@ -367,7 +367,7 @@ def run_fuzz(self, iterations=10, **params): segments = self.random_segments(**params, rand=random.Random(seed)) try: self.run_test(segments) - except: + except Exception: print("SEED", seed) raise diff --git a/sdks/python/apache_beam/runners/interactive/testing/integration/tests/screen_diff_test.py b/sdks/python/apache_beam/runners/interactive/testing/integration/tests/screen_diff_test.py index dbb978b44619..1746bf500f1d 100644 --- a/sdks/python/apache_beam/runners/interactive/testing/integration/tests/screen_diff_test.py +++ b/sdks/python/apache_beam/runners/interactive/testing/integration/tests/screen_diff_test.py @@ -39,7 +39,7 @@ def explicit_wait(self): WebDriverWait(self.driver, 5).until( expected_conditions.presence_of_element_located((By.ID, 'test-done'))) # pylint: disable=bare-except - except: + except Exception: pass # The test will be ignored. def test_dataframes(self): diff --git a/sdks/python/apache_beam/runners/portability/fn_api_runner/fn_runner_test.py b/sdks/python/apache_beam/runners/portability/fn_api_runner/fn_runner_test.py index 0197733e9115..2fb5637a26ce 100644 --- a/sdks/python/apache_beam/runners/portability/fn_api_runner/fn_runner_test.py +++ b/sdks/python/apache_beam/runners/portability/fn_api_runner/fn_runner_test.py @@ -1873,7 +1873,7 @@ def process(self, element): max=hamcrest.greater_than(0), sum=hamcrest.greater_than(0), count=hamcrest.greater_than(0)) - except: + except Exception: print(res._monitoring_infos_by_stage) raise @@ -1996,7 +1996,7 @@ def has_mi_for_ptransform(mon_infos, ptransform): postgbk_mis, monitoring_infos.ELEMENT_COUNT_URN, labels, value=5) self.assert_has_distribution( postgbk_mis, monitoring_infos.SAMPLED_BYTE_SIZE_URN, labels) - except: + except Exception: print(res._monitoring_infos_by_stage) raise diff --git a/sdks/python/apache_beam/runners/portability/stager.py b/sdks/python/apache_beam/runners/portability/stager.py index 668477ce1461..0e92841f3dcd 100644 --- a/sdks/python/apache_beam/runners/portability/stager.py +++ b/sdks/python/apache_beam/runners/portability/stager.py @@ -326,7 +326,7 @@ def create_job_resources( sdk_remote_location=setup_options.sdk_location, temp_dir=temp_dir, )) - except: + except Exception: raise RuntimeError( 'The --sdk_location option was used with an unsupported ' 'type of location: %s' % sdk_location) diff --git a/sdks/python/apache_beam/testing/load_tests/load_test.py b/sdks/python/apache_beam/testing/load_tests/load_test.py index 20dea3932b49..ea20fa4dbd27 100644 --- a/sdks/python/apache_beam/testing/load_tests/load_test.py +++ b/sdks/python/apache_beam/testing/load_tests/load_test.py @@ -183,5 +183,5 @@ def get_option_or_default(self, opt_name, default=0): return default try: return type(default)(option) - except: + except Exception: raise diff --git a/sdks/python/container/license_scripts/pull_licenses_py.py b/sdks/python/container/license_scripts/pull_licenses_py.py index a5cfef974d5c..2fe49b575bc3 100644 --- a/sdks/python/container/license_scripts/pull_licenses_py.py +++ b/sdks/python/container/license_scripts/pull_licenses_py.py @@ -141,7 +141,7 @@ def license_url(name, project_url, dep_config): with urlopen(url) as a: if a.getcode() == 200: return url - except: + except Exception: pass return project_url