python.lang.correctness.exceptions.exceptions.raise-not-base-exception
Community Favorite
semgrep
Author
46,927
Download Count*
License
In Python3, a runtime TypeError
will be thrown if you attempt to raise an object or class which does not inherit from BaseException
Run Locally
Run in CI
Defintion
rules:
- id: raise-not-base-exception
message: In Python3, a runtime `TypeError` will be thrown if you attempt to
raise an object or class which does not inherit from `BaseException`
languages:
- python
severity: ERROR
pattern-either:
- pattern: raise "..."
- pattern: |
$X: BaseException
raise $X(...)
- patterns:
- pattern: raise $EXCEPTION
- metavariable-regex:
metavariable: $EXCEPTION
regex: "[0-9]*\\.?[0-9]+"
metadata:
category: correctness
technology:
- python
license: Commons Clause License Condition v1.0[LGPL-2.1-only]
Examples
exceptions.py
# ruleid:raise-not-base-exception
raise "error here"
# ruleid:raise-not-base-exception
raise 5
class Foobar:
x = 5
# todoruleid:raise-not-base-exception
raise Foobar()
class Foobar2(BaseException):
x = 5
# ok:raise-not-base-exception
raise Foobar2()
# ok:raise-not-base-exception
raise Exception()
Short Link: https://sg.run/qxAz