-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve handling of optional packages (#27)
Improves handling of optional packages by: - No importing them just to check if available - Raises a more specific type of error (and message) ### Test Plan - Run unit tests
- Loading branch information
Showing
7 changed files
with
153 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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,27 @@ | ||
### IMPORTS | ||
### ============================================================================ | ||
## Future | ||
from __future__ import annotations | ||
|
||
## Standard Library | ||
|
||
## Installed | ||
|
||
## Application | ||
|
||
|
||
### CLASSES | ||
### ============================================================================ | ||
class PythonJsonLoggerError(Exception): | ||
"Generic base clas for all Python JSON Logger exceptions" | ||
|
||
|
||
class MissingPackageError(ImportError, PythonJsonLoggerError): | ||
"A required package is missing" | ||
|
||
def __init__(self, name: str, extras_name: str | None = None) -> None: | ||
msg = f"The {name!r} package is required but could not be found." | ||
if extras_name is not None: | ||
msg += f" It can be installed using 'python-json-logger[{extras_name}]'." | ||
super().__init__(msg) | ||
return |
This file contains 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 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 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,40 @@ | ||
"""Utilities for Python JSON Logger""" | ||
|
||
### IMPORTS | ||
### ============================================================================ | ||
## Future | ||
from __future__ import annotations | ||
|
||
## Standard Library | ||
import importlib.util | ||
|
||
## Installed | ||
|
||
## Application | ||
from .exception import MissingPackageError | ||
|
||
|
||
### FUNCTIONS | ||
### ============================================================================ | ||
def package_is_available( | ||
name: str, *, throw_error: bool = False, extras_name: str | None = None | ||
) -> bool: | ||
"""Determine if the given package is available for import. | ||
Args: | ||
name: Import name of the package to check. | ||
throw_error: Throw an error if the package is unavailable. | ||
extras_name: Extra dependency name to use in `throw_error`'s message. | ||
Raises: | ||
MissingPackageError: When `throw_error` is `True` and the return value would be `False` | ||
Returns: | ||
If the package is available for import. | ||
""" | ||
available = importlib.util.find_spec(name) is not None | ||
|
||
if not available and throw_error: | ||
raise MissingPackageError(name, extras_name) | ||
|
||
return available |
This file contains 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,67 @@ | ||
### IMPORTS | ||
### ============================================================================ | ||
## Future | ||
from __future__ import annotations | ||
|
||
## Standard Library | ||
|
||
## Installed | ||
import pytest | ||
|
||
## Application | ||
import pythonjsonlogger | ||
from pythonjsonlogger.utils import package_is_available | ||
from pythonjsonlogger.exception import MissingPackageError | ||
|
||
### CONSTANTS | ||
### ============================================================================ | ||
MISSING_PACKAGE_NAME = "package_name_is_definintely_not_available" | ||
MISSING_PACKAGE_EXTRA = "package_extra_that_is_unique" | ||
|
||
|
||
### TESTS | ||
### ============================================================================ | ||
def test_package_is_available(): | ||
assert package_is_available("json") | ||
return | ||
|
||
|
||
def test_package_not_available(): | ||
assert not package_is_available(MISSING_PACKAGE_NAME) | ||
return | ||
|
||
|
||
def test_package_not_available_throw(): | ||
with pytest.raises(MissingPackageError) as e: | ||
package_is_available(MISSING_PACKAGE_NAME, throw_error=True) | ||
assert MISSING_PACKAGE_NAME in e.value.msg | ||
assert MISSING_PACKAGE_EXTRA not in e.value.msg | ||
return | ||
|
||
|
||
def test_package_not_available_throw_extras(): | ||
with pytest.raises(MissingPackageError) as e: | ||
package_is_available( | ||
MISSING_PACKAGE_NAME, throw_error=True, extras_name=MISSING_PACKAGE_EXTRA | ||
) | ||
assert MISSING_PACKAGE_NAME in e.value.msg | ||
assert MISSING_PACKAGE_EXTRA in e.value.msg | ||
return | ||
|
||
|
||
## Python JSON Logger Specific | ||
## ----------------------------------------------------------------------------- | ||
if not pythonjsonlogger.ORJSON_AVAILABLE: | ||
|
||
def test_orjson_import_error(): | ||
with pytest.raises(MissingPackageError, match="orjson"): | ||
import pythonjsonlogger.orjson | ||
return | ||
|
||
|
||
if not pythonjsonlogger.MSGSPEC_AVAILABLE: | ||
|
||
def test_msgspec_import_error(): | ||
with pytest.raises(MissingPackageError, match="msgspec"): | ||
import pythonjsonlogger.msgspec | ||
return |