From 0aff0a36ceb4160f8af699dd27e241d398a26f5c Mon Sep 17 00:00:00 2001 From: Carlos Mocholi Date: Thu, 6 Jan 2022 15:52:08 +0100 Subject: [PATCH 1/4] Fix rich bug --- pytorch_lightning/callbacks/progress/rich_progress.py | 3 +-- pytorch_lightning/callbacks/rich_model_summary.py | 2 +- tests/callbacks/test_rich_model_summary.py | 2 +- tests/callbacks/test_rich_progress_bar.py | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pytorch_lightning/callbacks/progress/rich_progress.py b/pytorch_lightning/callbacks/progress/rich_progress.py index 46b5437934013..570c6d7df669c 100644 --- a/pytorch_lightning/callbacks/progress/rich_progress.py +++ b/pytorch_lightning/callbacks/progress/rich_progress.py @@ -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 @@ -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`." ) diff --git a/pytorch_lightning/callbacks/rich_model_summary.py b/pytorch_lightning/callbacks/rich_model_summary.py index cce4eb316a3b0..14c078a273ece 100644 --- a/pytorch_lightning/callbacks/rich_model_summary.py +++ b/pytorch_lightning/callbacks/rich_model_summary.py @@ -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) diff --git a/tests/callbacks/test_rich_model_summary.py b/tests/callbacks/test_rich_model_summary.py index 5ab091bd01445..88c5f9ab531f0 100644 --- a/tests/callbacks/test_rich_model_summary.py +++ b/tests/callbacks/test_rich_model_summary.py @@ -35,7 +35,7 @@ def test_rich_model_summary_callback(): def test_rich_progress_bar_import_error(): if not _RICH_AVAILABLE: - with pytest.raises(ImportError, match="`RichModelSummary` requires `rich` to be installed."): + with pytest.raises(ModuleNotFoundError, match="`RichModelSummary` requires `rich` to be installed."): Trainer(callbacks=RichModelSummary()) diff --git a/tests/callbacks/test_rich_progress_bar.py b/tests/callbacks/test_rich_progress_bar.py index f2e75006f7ecb..d9a9ad8c3c726 100644 --- a/tests/callbacks/test_rich_progress_bar.py +++ b/tests/callbacks/test_rich_progress_bar.py @@ -85,7 +85,7 @@ def predict_dataloader(self): def test_rich_progress_bar_import_error(): if not _RICH_AVAILABLE: - with pytest.raises(ImportError, match="`RichProgressBar` requires `rich` >= 10.2.2."): + with pytest.raises(ModuleNotFoundError, match="`RichProgressBar` requires `rich` >= 10.2.2."): Trainer(callbacks=RichProgressBar()) From 995664ce47a3675b105616d34cdb4a82cd7583f2 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Sat, 8 Jan 2022 00:56:46 +0900 Subject: [PATCH 2/4] Mock _RICH_AVAILABLE --- tests/callbacks/test_rich_model_summary.py | 11 ++++++----- tests/callbacks/test_rich_progress_bar.py | 10 +++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/callbacks/test_rich_model_summary.py b/tests/callbacks/test_rich_model_summary.py index 88c5f9ab531f0..c596557eed0dc 100644 --- a/tests/callbacks/test_rich_model_summary.py +++ b/tests/callbacks/test_rich_model_summary.py @@ -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 @@ -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(ModuleNotFoundError, 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) diff --git a/tests/callbacks/test_rich_progress_bar.py b/tests/callbacks/test_rich_progress_bar.py index d9a9ad8c3c726..7c6d2b656d08e 100644 --- a/tests/callbacks/test_rich_progress_bar.py +++ b/tests/callbacks/test_rich_progress_bar.py @@ -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 @@ -83,11 +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(ModuleNotFoundError, 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) def test_rich_progress_bar_custom_theme(tmpdir): From b3fe48b3613bf6cd5311201cc6a7f44ae753f5a1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 7 Jan 2022 16:00:38 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/callbacks/test_rich_progress_bar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/callbacks/test_rich_progress_bar.py b/tests/callbacks/test_rich_progress_bar.py index 7c6d2b656d08e..6b47e9558f92f 100644 --- a/tests/callbacks/test_rich_progress_bar.py +++ b/tests/callbacks/test_rich_progress_bar.py @@ -89,6 +89,7 @@ def test_rich_progress_bar_import_error(monkeypatch): with pytest.raises(ModuleNotFoundError, match="`RichProgressBar` requires `rich` >= 10.2.2."): RichProgressBar() + @RunIf(rich=True) def test_rich_progress_bar_custom_theme(tmpdir): """Test to ensure that custom theme styles are used.""" From 89aae799a2718b6cba49ce063bdb9bece0539579 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Sat, 8 Jan 2022 01:13:13 +0900 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e2bde5be2d0b..047b3fc957b99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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))