Skip to content

Commit

Permalink
fix: message where missing dependency name doesn't match install extra (
Browse files Browse the repository at this point in the history
#2921)

* Fix faulty exception wording

* Fix another spotted mismatch

* PR review

---------

Co-authored-by: Peter Schutt <peter.github@proton.me>
  • Loading branch information
2 people authored and cofin committed Jan 11, 2024
1 parent 3dee7d3 commit 1618eed
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion litestar/contrib/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from jinja2 import Environment, FileSystemLoader, pass_context
from jinja2 import TemplateNotFound as JinjaTemplateNotFound
except ImportError as e:
raise MissingDependencyException("jinja2") from e
raise MissingDependencyException("jinja2", extra="jinja") from e

if TYPE_CHECKING:
from pathlib import Path
Expand Down
2 changes: 1 addition & 1 deletion litestar/contrib/prometheus/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
try:
import prometheus_client # noqa: F401
except ImportError as e:
raise MissingDependencyException("prometheus_client") from e
raise MissingDependencyException("prometheus_client", "prometheus-client", "prometheus") from e


if TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion litestar/contrib/prometheus/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
try:
import prometheus_client # noqa: F401
except ImportError as e:
raise MissingDependencyException("prometheus_client") from e
raise MissingDependencyException("prometheus_client", "prometheus-client", "prometheus") from e

from prometheus_client import (
CONTENT_TYPE_LATEST,
Expand Down
2 changes: 1 addition & 1 deletion litestar/contrib/prometheus/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
try:
import prometheus_client # noqa: F401
except ImportError as e:
raise MissingDependencyException("prometheus_client") from e
raise MissingDependencyException("prometheus_client", "prometheus-client", "prometheus") from e

from prometheus_client import Counter, Gauge, Histogram

Expand Down
4 changes: 2 additions & 2 deletions litestar/exceptions/base_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class MissingDependencyException(LitestarException, ImportError):
This exception is raised only when a module depends on a dependency that has not been installed.
"""

def __init__(self, package: str, install_package: str | None = None) -> None:
def __init__(self, package: str, install_package: str | None = None, extra: str | None = None) -> None:
super().__init__(
f"Package {package!r} is not installed but required. You can install it by running "
f"'pip install litestar[{install_package or package}]' to install litestar with the required extra "
f"'pip install litestar[{extra or install_package or package}]' to install litestar with the required extra "
f"or 'pip install {install_package or package}' to install the package separately"
)

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ def test_missing_dependency_exception() -> None:


def test_missing_dependency_exception_differing_package_name() -> None:
exc = MissingDependencyException("some_package", "install_via_this")
exc = MissingDependencyException("some_package", "install_via_this", "other-extra")
expected = (
"Package 'some_package' is not installed but required. You can install it by running 'pip install "
"litestar[install_via_this]' to install litestar with the required extra or 'pip install install_via_this' to "
"litestar[other-extra]' to install litestar with the required extra or 'pip install install_via_this' to "
"install the package separately"
)

Expand Down

0 comments on commit 1618eed

Please sign in to comment.