Skip to content

STYLE: Prohibit parsing exeption messages in try/except code flows #50353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mroeschke opened this issue Dec 19, 2022 · 3 comments
Open

STYLE: Prohibit parsing exeption messages in try/except code flows #50353

mroeschke opened this issue Dec 19, 2022 · 3 comments
Labels
Code Style Code style, linting, code_checks Error Reporting Incorrect or improved errors from pandas

Comments

@mroeschke
Copy link
Member

There are a few spots in the code base where we parse exception message to rewrite a new exception message or dictate fallback behavior e.g.

try:
    ...
except ValueError as err:
    if "Message1" in str(err):
         raise ValueError(message_other) from err
    elif "Message2" in str(err):
         return True
% grep -R --include="*.py" "in str(err)"
./pandas/compat/pickle_compat.py:        if msg in str(err):
./pandas/core/resample.py:            if "Must produce aggregated value" in str(err):
./pandas/core/dtypes/cast.py:            if "cannot supply both a tz and a timezone-naive dtype" in str(err):
./pandas/core/dtypes/missing.py:                if "boolean value of NA is ambiguous" in str(err):
./pandas/core/groupby/generic.py:                    if "No objects to concatenate" not in str(err):
./pandas/core/internals/blocks.py:        elif "'value.closed' is" in str(err):
./pandas/core/frame.py:            if "shape mismatch" not in str(err):
./pandas/core/apply.py:            if "All arrays must be of the same length" in str(err):
./pandas/core/indexes/base.py:            if "index must be specified when data is not list-like" in str(err):
./pandas/core/indexes/base.py:            if "Data must be 1-dimensional" in str(err):

This signals that the exception and message is significant to ops behavior and is more brittle to rewriting exception message in general. IMO we should be using exception subclasses instead. Thoughts?

@mroeschke mroeschke added Error Reporting Incorrect or improved errors from pandas Code Style Code style, linting, code_checks labels Dec 19, 2022
@jbrockmendel
Copy link
Member

Sounds like good practice

@vyasr
Copy link
Contributor

vyasr commented Dec 19, 2022

FWIW we are actively trying to do this for cudf as well.

@luke396
Copy link
Contributor

luke396 commented Dec 23, 2022

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Style Code style, linting, code_checks Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants