-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Python: Modernize the Unreachable Except Block query #20263
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
Merged
joefarebrother
merged 5 commits into
github:main
from
joefarebrother:python-qual-exceptions
Sep 22, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e42002e
Promote IncorrectExceptOrder.
joefarebrother f24f7d5
Add builtin subclass models, incorporate into query
joefarebrother 9edfd7a
Use generator script directly
joefarebrother eb48412
Add tests (WIP)
joefarebrother ff4c11f
Update test output. Accepting some FNs due to dataflow issue.
joefarebrother File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
738 changes: 738 additions & 0 deletions
738
python/ql/lib/semmle/python/frameworks/builtins.model.yml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
python/ql/src/meta/ClassHierarchy/process-builtin-exceptions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from shared_subclass_functions import wrap_in_template | ||
| import sys | ||
| import yaml | ||
| from pathlib import Path | ||
|
|
||
| py_version = sys.version.split()[0] | ||
| VERSION = f"process-builtin-exceptions 0.0.1; Python {py_version}" | ||
|
|
||
| builtins_model_path = Path(__file__).parent.parent.parent.parent / "lib/semmle/python/frameworks/builtins.model.yml" | ||
|
|
||
| def write_data(data, path: Path): | ||
| f = path.open("w+") | ||
| f.write(f"# {VERSION}\n") | ||
| yaml.dump(data, indent=2, stream=f, Dumper=yaml.CDumper) | ||
|
|
||
| builtin_names = dir(__builtins__) | ||
| builtin_dict = {x: getattr(__builtins__,x) for x in builtin_names} | ||
|
|
||
|
|
||
| builtin_exceptions = {v for v in builtin_dict.values() if type(v) is type and issubclass(v, BaseException)} | ||
|
|
||
| data = [] | ||
| for sub in builtin_exceptions: | ||
| for base in sub.__mro__: | ||
| if issubclass(base, BaseException): | ||
| basename = base.__name__ | ||
| subname = sub.__name__ | ||
| row = [f"builtins.{basename}~Subclass", f"builtins.{subname}", ""] | ||
| data.append(row) | ||
|
|
||
| write_data(wrap_in_template(data), builtins_model_path) |
2 changes: 1 addition & 1 deletion
2
python/ql/test/query-tests/Exceptions/general/CatchingBaseException.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| | exceptions_test.py:7:5:7:11 | ExceptStmt | Except block directly handles BaseException. | | ||
| | exceptions_test.py:71:5:71:25 | ExceptStmt | Except block directly handles BaseException. | | ||
| | exceptions_test.py:97:5:97:25 | ExceptStmt | Except block directly handles BaseException. | |
3 changes: 3 additions & 0 deletions
3
python/ql/test/query-tests/Exceptions/general/EmptyExcept.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| | exceptions_test.py:7:5:7:11 | ExceptStmt | 'except' clause does nothing but pass and there is no explanatory comment. | | ||
| | exceptions_test.py:13:5:13:21 | ExceptStmt | 'except' clause does nothing but pass and there is no explanatory comment. | | ||
| | exceptions_test.py:72:1:72:18 | ExceptStmt | 'except' clause does nothing but pass and there is no explanatory comment. | | ||
| | exceptions_test.py:85:1:85:17 | ExceptStmt | 'except' clause does nothing but pass and there is no explanatory comment. | | ||
| | exceptions_test.py:89:1:89:17 | ExceptStmt | 'except' clause does nothing but pass and there is no explanatory comment. | |
2 changes: 1 addition & 1 deletion
2
python/ql/test/query-tests/Exceptions/general/IllegalExceptionHandlerType.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| | exceptions_test.py:51:5:51:25 | ExceptStmt | Non-exception $@ in exception handler which will never match raised exception. | exceptions_test.py:33:1:33:28 | ControlFlowNode for ClassExpr | class 'NotException1' | | ||
| | exceptions_test.py:54:5:54:25 | ExceptStmt | Non-exception $@ in exception handler which will never match raised exception. | exceptions_test.py:36:1:36:28 | ControlFlowNode for ClassExpr | class 'NotException2' | | ||
| | exceptions_test.py:112:5:112:22 | ExceptStmt | Non-exception $@ in exception handler which will never match raised exception. | exceptions_test.py:107:12:107:14 | ControlFlowNode for FloatLiteral | instance of 'float' | | ||
| | exceptions_test.py:138:5:138:22 | ExceptStmt | Non-exception $@ in exception handler which will never match raised exception. | exceptions_test.py:133:12:133:14 | ControlFlowNode for FloatLiteral | instance of 'float' | | ||
| | pypy_test.py:14:5:14:14 | ExceptStmt | Non-exception $@ in exception handler which will never match raised exception. | pypy_test.py:14:12:14:13 | ControlFlowNode for IntegerLiteral | instance of 'int' | |
2 changes: 1 addition & 1 deletion
2
python/ql/test/query-tests/Exceptions/general/IncorrectExceptOrder.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| | exceptions_test.py:64:1:64:22 | ExceptStmt | Except block for $@ is unreachable; the more general $@ for $@ will always be executed in preference. | file://:0:0:0:0 | builtin-class AttributeError | AttributeError | exceptions_test.py:62:1:62:17 | ExceptStmt | except block | file://:0:0:0:0 | builtin-class Exception | Exception | | ||
| | exceptions_test.py:64:1:64:22 | ExceptStmt | This except block handling $@ is unreachable; as $@ for the more general $@ always subsumes it. | file://:0:0:0:0 | AttributeError | AttributeError | exceptions_test.py:62:1:62:17 | ExceptStmt | this except block | file://:0:0:0:0 | Exception | Exception | |
3 changes: 2 additions & 1 deletion
3
python/ql/test/query-tests/Exceptions/general/IncorrectExceptOrder.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| Exceptions/IncorrectExceptOrder.ql | ||
| query: Exceptions/IncorrectExceptOrder.ql | ||
| postprocess: utils/test/InlineExpectationsTestQuery.ql |
4 changes: 2 additions & 2 deletions
4
python/ql/test/query-tests/Exceptions/general/NotImplementedIsNotAnException.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| | exceptions_test.py:170:11:170:24 | NotImplemented | NotImplemented is not an Exception. Did you mean NotImplementedError? | | ||
| | exceptions_test.py:173:11:173:26 | NotImplemented() | NotImplemented is not an Exception. Did you mean NotImplementedError? | | ||
| | exceptions_test.py:196:11:196:24 | NotImplemented | NotImplemented is not an Exception. Did you mean NotImplementedError? | | ||
| | exceptions_test.py:199:11:199:26 | NotImplemented() | NotImplemented is not an Exception. Did you mean NotImplementedError? | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The many uses of
<class-stuff> or <builtin-stuff>in the various member predicates suggests that it might be better to create two separate subclasses ofExceptTypethat each override the (abstract) predicates on this type in a different way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, though that approach may be a bit longer.