Skip to content
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

Fix inconsistent exceptions raised with no rich installed #11360

Merged
merged 6 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- `Trainer.logged_metrics` now always contains scalar tensors, even when a Python scalar was logged ([#11270](https://github.com/PyTorchLightning/pytorch-lightning/pull/11270))


- Changed `MisconfigurationException` to `ModuleNotFoundError` when `rich` isn't available ([#11360](https://github.com/PyTorchLightning/pytorch-lightning/pull/11360))


### Deprecated

- Deprecated `ClusterEnvironment.master_{address,port}` in favor of `ClusterEnvironment.main_{address,port}` ([#10103](https://github.com/PyTorchLightning/pytorch-lightning/issues/10103))
Expand Down
3 changes: 1 addition & 2 deletions pytorch_lightning/callbacks/progress/rich_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from typing import Any, Dict, Optional, Union

from pytorch_lightning.callbacks.progress.base import ProgressBarBase
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.imports import _RICH_AVAILABLE

Task, Style = None, None
Expand Down Expand Up @@ -231,7 +230,7 @@ def __init__(
console_kwargs: Optional[Dict[str, Any]] = None,
) -> None:
if not _RICH_AVAILABLE:
raise MisconfigurationException(
raise ModuleNotFoundError(
"`RichProgressBar` requires `rich` >= 10.2.2. Install it by running `pip install -U rich`."
)

Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/callbacks/rich_model_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RichModelSummary(ModelSummary):
def __init__(self, max_depth: int = 1) -> None:
if not _RICH_AVAILABLE:
raise ModuleNotFoundError(
"`RichProgressBar` requires `rich` to be installed. Install it by running `pip install -U rich`."
"`RichModelSummary` requires `rich` to be installed. Install it by running `pip install -U rich`."
)
super().__init__(max_depth)

Expand Down
11 changes: 6 additions & 5 deletions tests/callbacks/test_rich_model_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import RichModelSummary, RichProgressBar
from pytorch_lightning.utilities.imports import _RICH_AVAILABLE
from pytorch_lightning.utilities.model_summary import summarize
from tests.helpers import BoringModel
from tests.helpers.runif import RunIf
Expand All @@ -33,10 +32,12 @@ def test_rich_model_summary_callback():
assert isinstance(trainer.progress_bar_callback, RichProgressBar)


def test_rich_progress_bar_import_error():
if not _RICH_AVAILABLE:
with pytest.raises(ImportError, match="`RichModelSummary` requires `rich` to be installed."):
Trainer(callbacks=RichModelSummary())
def test_rich_progress_bar_import_error(monkeypatch):
import pytorch_lightning.callbacks.rich_model_summary as imports

monkeypatch.setattr(imports, "_RICH_AVAILABLE", False)
with pytest.raises(ModuleNotFoundError, match="`RichModelSummary` requires `rich` to be installed."):
RichModelSummary()


@RunIf(rich=True)
Expand Down
11 changes: 6 additions & 5 deletions tests/callbacks/test_rich_progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import ProgressBarBase, RichProgressBar
from pytorch_lightning.callbacks.progress.rich_progress import RichProgressBarTheme
from pytorch_lightning.utilities.imports import _RICH_AVAILABLE
from tests.helpers.boring_model import BoringModel, RandomDataset, RandomIterableDataset
from tests.helpers.runif import RunIf

Expand Down Expand Up @@ -83,10 +82,12 @@ def predict_dataloader(self):
assert progress_update.call_count == 8


def test_rich_progress_bar_import_error():
if not _RICH_AVAILABLE:
with pytest.raises(ImportError, match="`RichProgressBar` requires `rich` >= 10.2.2."):
Trainer(callbacks=RichProgressBar())
def test_rich_progress_bar_import_error(monkeypatch):
import pytorch_lightning.callbacks.progress.rich_progress as imports

monkeypatch.setattr(imports, "_RICH_AVAILABLE", False)
with pytest.raises(ModuleNotFoundError, match="`RichProgressBar` requires `rich` >= 10.2.2."):
RichProgressBar()


@RunIf(rich=True)
Expand Down