Skip to content
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

Avoid raising bare Exception #6

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

lizard-boy
Copy link

Summary

Before tackling issue Instagram#457, there are some bare Exceptions thrown from within the code.

Here is the logic for changes in Exceptions:

  • use ValueError when issue caused by user input
  • use CSTValidationError & TypeError when issue occurs during the CST validation process
  • use ParserSyntaxError when issue occurs during the parsing process
  • use CSTLogicError to replace all Exception which contained "Logic error" in the cause message
  • remove Exception catch+re-raise when not needed

I'm all for changes if some Exceptions changes are not OK,
In particular, there are some ParserSyntaxError which may not be meaningful - with raw_line=0, raw_column=0 params

Test Plan

Keep the current test plan.

@lizard-boy
Copy link
Author

@greptileai

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR improves exception handling throughout the LibCST codebase by replacing bare Exception raises with more specific exception types.

  • Introduced CSTLogicError in libcst/_excep.py for logical errors within CST processing
  • Replaced generic Exception raises with ValueError, CSTValidationError, ParserSyntaxError, and CSTLogicError across multiple files
  • Updated libcst/__init__.py to include CSTLogicError in the __all__ list for easier importing
  • Modified error messages in various files to provide more precise information about the cause of exceptions
  • Improved consistency in exception handling across different modules and components of LibCST

30 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings

Comment on lines +125 to 128
raise ValueError(
"Cannot have multiple star ('*') markers in a single argument "
+ "list."
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using ParserSyntaxError instead of ValueError for consistency with other parsing errors

Comment on lines +140 to 143
raise ValueError(
"Cannot have multiple slash ('/') markers in a single argument "
+ "list."
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using ParserSyntaxError instead of ValueError for consistency with other parsing errors

@@ -168,7 +169,7 @@ def add_param(
# Example code:
# def fn(**kwargs, trailing=None)
# This should be unreachable, the grammar already disallows it.
raise Exception("Cannot have any arguments after a kwargs expansion.")
raise ValueError("Cannot have any arguments after a kwargs expansion.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using ParserSyntaxError instead of ValueError for consistency with other parsing errors

Comment on lines +185 to 188
raise ValueError(
"Expected a keyword argument but found a starred positional "
+ "argument expansion."
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using ParserSyntaxError instead of ValueError for consistency with other parsing errors

Comment on lines +201 to 204
raise ValueError(
"Multiple starred keyword argument expansions are not allowed in a "
+ "single argument list"
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using ParserSyntaxError instead of ValueError for consistency with other parsing errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants