A linter that ensures all raised Exceptions include an error with a link to more information
Project description
flake8-error-link
Have you ever encountered an error when using a package and then gone to Google
to find out how to solve the error? Wouldn't your users prefer to go directly
to your documentation that tells them exactly what went wrong and how to
resolve that error? flake8-error-link
checks the way exceptions are raised in
your code base to ensure that a link with more information is provided.
Getting Started
python -m venv venv
source ./venv/bin/activate
pip install flake8 flake8-error-link
flake8 source.py
On the following code:
# source.py
raise Exception
This will produce warnings such as:
source.py:1:0: ELI001 builtin exceptions should be raised with a link to more information: https://github.com/jdkandersson/flake8-error-link#fix-eli001
This can be resolved by changing the code to:
# source.py
raise Exception("more information: https://github.com/jdkandersson/flake8-error-link#fix-eli001")
Configuration
The plugin adds the following configurations to flake8
:
--error-link-regex
: The regular expression to use to verify the way exceptions are reased, defaults tomore information: (mailto\:|(news|(ht|f)tp(s?))\:\/\/){1}\S+
Rules
A few rules have been defined to allow for selective suppression:
ELI001
: checks that any builtin exceptions that are raised with constant arguments include a link to more information.ELI002
: checks that any custom exceptions that are raised with constant arguments include a link to more information.ELI003
: checks that any exceptions that are raised with variable arguments include a constant argument with a link to more information.ELI004
: checks that any exceptions that are re-raised include a constant argument with a link to more information.
Fix ELI001
This linting rule is trigger by raising an inbuilt exception without providing a constant that includes a link to more information as one of the arguments to the constructor. For example:
raise Exception
raise ValueError
raise Exception()
raise Exception("oh no! :(")
These examples can be fixed by using something like:
raise Exception(
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli001"
)
raise ValueError(
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli001"
)
raise Exception(
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli001"
)
raise Exception(
"oh no! :(",
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli001",
)
Fix ELI002
This linting rule is trigger by raising a custom exception without providing a constant that include a link to more information as one of the arguments to the constructor. For example:
class CustomError(Exception):
pass
raise CustomError
raise CustomError()
raise CustomError("bummer...")
These examples can be fixed by using something like:
class CustomError(Exception):
pass
raise CustomError(
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli002"
)
raise CustomError(
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli002"
)
raise CustomError(
"bummer...",
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli002",
)
Fix ELI003
This linting rule is trigger by raising an exception and passing at least one argument without providing a constant that include a link to more information as one of the arguments to the constructor. For example:
message = "gotcha"
def get_message():
return message
raise Exception(get_message())
raise Exception(f"{message} quite badly")
These examples can be fixed by using something like:
message = "gotcha"
def get_message():
return message
raise Exception(
get_message(),
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli003",
)
raise Exception(
f"{message} quite badly, more information: https://github.com/jdkandersson/flake8-error-link#fix-eli003"
)
Fix ELI004
This linting rule is trigger by re-raising an exception. For example:
try:
raise Exception(
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli004"
)
except Exception:
raise
This example can be fixed by using something like:
try:
raise Exception(
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli004"
)
except Exception as exc:
raise Exception(
"more information: https://github.com/jdkandersson/flake8-error-link#fix-eli004"
) from exc
This rule can be spurious at times if an exception is re-raisesd that already has a more information link. Regardless, it is usually a good idea to include a specific link for a problem. The context is usually different when an exception is re-raised so it could be useful to include documentation for that context rather then relying on any link provided by the original exception.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flake8_error_link-1.2.4.tar.gz
.
File metadata
- Download URL: flake8_error_link-1.2.4.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.11.1 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b29d61e7e6ad1545df479ceb71a2d5140b0c73f2a6f0f4f0ebaee2f34df9e25c |
|
MD5 | 5f5fd8ae2033b47a29a8a6092604d1af |
|
BLAKE2b-256 | 890585840184d8448af12e163b1c10350f8bcb12c3a12c18e3a51da152b0c79e |
File details
Details for the file flake8_error_link-1.2.4-py3-none-any.whl
.
File metadata
- Download URL: flake8_error_link-1.2.4-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.11.1 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 930702b5b000b6c7b6d6a250854297e303fd500c772fdf1e3e7070346dff526d |
|
MD5 | b44316c0a2c2b2c7e70ab20fefd2d55e |
|
BLAKE2b-256 | 18c3a18638de8c5b1812058c40914927b11e1a0d43cd7264cbfa982a1839400b |