Skip to content

Commit 6ba8f32

Browse files
authored
Enable ruff TRY ruleset for warnings related to try/except issues (#1426)
Globally disabled specific TRY003 rule because it wants you to avoid long messages when raising an exception and basically put the logic in a custom exception class for everything. This is too much of a hassle IMO.
1 parent b697356 commit 6ba8f32

File tree

4 files changed

+6
-11
lines changed

4 files changed

+6
-11
lines changed

cmd2/argparse_custom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,14 @@ def __init__(
392392
def completer(self) -> CompleterFunc:
393393
if not isinstance(self.to_call, (CompleterFuncBase, CompleterFuncWithTokens)): # pragma: no cover
394394
# this should've been caught in the constructor, just a backup check
395-
raise ValueError('Function is not a CompleterFunc')
395+
raise TypeError('Function is not a CompleterFunc')
396396
return self.to_call
397397

398398
@property
399399
def choices_provider(self) -> ChoicesProviderFunc:
400400
if not isinstance(self.to_call, (ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens)): # pragma: no cover
401401
# this should've been caught in the constructor, just a backup check
402-
raise ValueError('Function is not a ChoicesProviderFunc')
402+
raise TypeError('Function is not a ChoicesProviderFunc')
403403
return self.to_call
404404

405405

cmd2/cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,7 +2497,7 @@ def onecmd_plus_hooks(
24972497
if stop:
24982498
# we should not run the command, but
24992499
# we need to run the finalization hooks
2500-
raise EmptyStatement
2500+
raise EmptyStatement # noqa: TRY301
25012501

25022502
redir_saved_state: Optional[utils.RedirectionSavedState] = None
25032503

@@ -4047,7 +4047,7 @@ def select(self, opts: Union[str, list[str], list[tuple[Any, Optional[str]]]], p
40474047
try:
40484048
choice = int(response)
40494049
if choice < 1:
4050-
raise IndexError
4050+
raise IndexError # noqa: TRY301
40514051
return fulloptions[choice - 1][0]
40524052
except (ValueError, IndexError):
40534053
self.poutput(f"'{response}' isn't a valid choice. Pick a number between 1 and {len(fulloptions)}:")

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ select = [
215215
"TC", # flake8-type-checking (type checking warnings)
216216
"TD", # flake8-todos (force all TODOs to include an author and issue link)
217217
"TID", # flake8-tidy-imports (extra import rules to check)
218-
# "TRY", # tryceratops (warnings related to exceptions and try/except)
218+
"TRY", # tryceratops (warnings related to exceptions and try/except)
219219
"UP", # pyupgrade (A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language)
220220
"W", # pycodestyle warnings (warn about minor stylistic issues)
221221
"YTT", # flake8-2020 (checks for misuse of sys.version or sys.version_info)
@@ -236,6 +236,7 @@ ignore = [
236236
"Q002", # Conflicts with ruff format
237237
"Q003", # Conflicts with ruff format
238238
"TC006", # Add quotes to type expression in typing.cast() (not needed except for forward references)
239+
"TRY003", # Avoid specifying long messages outside the exception class (force custom exceptions for everything)
239240
"UP007", # Use X | Y for type annotations (requires Python 3.10+)
240241
"UP017", # Use datetime.UTC alias (requires Python 3.11+)
241242
"UP036", # Version block is outdated for minimum Python version (requires ruff target_version set to minimum supported)

tests/test_cmd2.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,6 @@ def test_output_redirection(base_app) -> None:
643643
appended_content = f.read()
644644
assert appended_content.startswith(content)
645645
assert len(appended_content) > len(content)
646-
except Exception:
647-
raise
648646
finally:
649647
os.remove(filename)
650648

@@ -686,8 +684,6 @@ def test_feedback_to_output_true(base_app) -> None:
686684
with open(filename) as f:
687685
content = f.readlines()
688686
assert content[-1].startswith('Elapsed: ')
689-
except:
690-
raise
691687
finally:
692688
os.remove(filename)
693689

@@ -705,8 +701,6 @@ def test_feedback_to_output_false(base_app) -> None:
705701
content = f.readlines()
706702
assert not content[-1].startswith('Elapsed: ')
707703
assert err[0].startswith('Elapsed')
708-
except:
709-
raise
710704
finally:
711705
os.remove(filename)
712706

0 commit comments

Comments
 (0)