Skip to content

fix: replace bare except: with except ImportError: in optional import guards#1602

Open
harshadkhetpal wants to merge 2 commits into
whylabs:mainlinefrom
harshadkhetpal:fix/replace-bare-except-with-specific-exceptions-2026-04-08
Open

fix: replace bare except: with except ImportError: in optional import guards#1602
harshadkhetpal wants to merge 2 commits into
whylabs:mainlinefrom
harshadkhetpal:fix/replace-bare-except-with-specific-exceptions-2026-04-08

Conversation

@harshadkhetpal

Copy link
Copy Markdown

Summary

Replace bare except: clauses with specific except ImportError: in optional import guard blocks.

Changes

  • python/whylogs/core/preprocessing.py: The try/except block guards an optional import pandas.core.dtypes.common — catching only ImportError is more precise and avoids silently swallowing unexpected errors.
  • python/whylogs/core/datatypes.py: The try/except block guards an optional from pandas.core.api import CategoricalDtype — same fix.

Why

Bare except: catches all exceptions including SystemExit, KeyboardInterrupt, and programming errors like SyntaxError. For optional dependency imports, except ImportError: is the correct and intended exception to catch. This aligns with Python best practices (PEP 8) and makes error handling more explicit.

Testing

These are import-time guard blocks with no behavioral change when pandas is/isn't installed. The only difference is that non-import errors are no longer silently swallowed.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale. Remove stale label or it will be closed next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant