From 40fd35bf17b085c0de3a399dceafa167d6e62375 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Wed, 14 Oct 2020 17:46:46 +0900 Subject: [PATCH] Use explicit exception chaining (#261) --- pl_bolts/callbacks/vision/confused_logit.py | 4 ++-- pl_bolts/datamodules/base_dataset.py | 4 ++-- pl_bolts/datamodules/imagenet_dataset.py | 4 ++-- pl_bolts/datamodules/mnist_dataset.py | 4 ++-- pl_bolts/models/regression/linear_regression.py | 4 ++-- pl_bolts/models/regression/logistic_regression.py | 4 ++-- pl_bolts/models/self_supervised/cpc/cpc_module.py | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pl_bolts/callbacks/vision/confused_logit.py b/pl_bolts/callbacks/vision/confused_logit.py index b3b07007f7..3e6f92da50 100644 --- a/pl_bolts/callbacks/vision/confused_logit.py +++ b/pl_bolts/callbacks/vision/confused_logit.py @@ -64,12 +64,12 @@ def on_train_batch_end(self, trainer, pl_module, batch, batch_idx, dataloader_id x, y = batch try: logits = pl_module.last_logits - except AttributeError as e: + except AttributeError as err: m = """please track the last_logits in the training_step like so: def training_step(...): self.last_logits = your_logits """ - raise AttributeError(m) + raise AttributeError(m) from err # only check when it has opinions (ie: the logit > 5) if logits.max() > self.min_logit_value: diff --git a/pl_bolts/datamodules/base_dataset.py b/pl_bolts/datamodules/base_dataset.py index 3397778859..a5c1e50898 100644 --- a/pl_bolts/datamodules/base_dataset.py +++ b/pl_bolts/datamodules/base_dataset.py @@ -54,5 +54,5 @@ def _download_from_url(self, base_url: str, data_folder: str, file_name: str): fpath = os.path.join(data_folder, file_name) try: urllib.request.urlretrieve(url, fpath) - except HTTPError: - raise RuntimeError(f'Failed download from {url}') + except HTTPError as err: + raise RuntimeError(f'Failed download from {url}') from err diff --git a/pl_bolts/datamodules/imagenet_dataset.py b/pl_bolts/datamodules/imagenet_dataset.py index 6238e41a7c..4a6cbd6677 100644 --- a/pl_bolts/datamodules/imagenet_dataset.py +++ b/pl_bolts/datamodules/imagenet_dataset.py @@ -20,10 +20,10 @@ try: from torchvision.datasets import ImageNet from torchvision.datasets.imagenet import load_meta_file -except ModuleNotFoundError: +except ModuleNotFoundError as err: raise ModuleNotFoundError( # pragma: no-cover 'You want to use `torchvision` which is not installed yet, install it with `pip install torchvision`.' - ) + ) from err class UnlabeledImagenet(ImageNet): diff --git a/pl_bolts/datamodules/mnist_dataset.py b/pl_bolts/datamodules/mnist_dataset.py index 4ecd00c954..0144f96345 100644 --- a/pl_bolts/datamodules/mnist_dataset.py +++ b/pl_bolts/datamodules/mnist_dataset.py @@ -3,10 +3,10 @@ try: from torchvision import transforms as transform_lib from torchvision.datasets import MNIST -except ModuleNotFoundError: +except ModuleNotFoundError as err: raise ModuleNotFoundError( # pragma: no-cover 'You want to use `torchvision` which is not installed yet, install it with `pip install torchvision`.' - ) + ) from err try: from PIL import Image diff --git a/pl_bolts/models/regression/linear_regression.py b/pl_bolts/models/regression/linear_regression.py index f69fb7020e..241ff9f33e 100644 --- a/pl_bolts/models/regression/linear_regression.py +++ b/pl_bolts/models/regression/linear_regression.py @@ -126,10 +126,10 @@ def cli_main(): # create dataset try: from sklearn.datasets import load_boston - except ModuleNotFoundError: + except ModuleNotFoundError as err: raise ModuleNotFoundError( # pragma: no-cover 'You want to use `sklearn` which is not installed yet, install it with `pip install sklearn`.' - ) + ) from err X, y = load_boston(return_X_y=True) # these are numpy arrays loaders = SklearnDataModule(X, y) diff --git a/pl_bolts/models/regression/logistic_regression.py b/pl_bolts/models/regression/logistic_regression.py index 047c05747d..d39949c5a1 100644 --- a/pl_bolts/models/regression/logistic_regression.py +++ b/pl_bolts/models/regression/logistic_regression.py @@ -132,10 +132,10 @@ def cli_main(): # Example: Iris dataset in Sklearn (4 features, 3 class labels) try: from sklearn.datasets import load_iris - except ModuleNotFoundError: + except ModuleNotFoundError as err: raise ModuleNotFoundError( # pragma: no-cover 'You want to use `sklearn` which is not installed yet, install it with `pip install sklearn`.' - ) + ) from err X, y = load_iris(return_X_y=True) loaders = SklearnDataModule(X, y) diff --git a/pl_bolts/models/self_supervised/cpc/cpc_module.py b/pl_bolts/models/self_supervised/cpc/cpc_module.py index 84fe1be0e4..195f84e781 100644 --- a/pl_bolts/models/self_supervised/cpc/cpc_module.py +++ b/pl_bolts/models/self_supervised/cpc/cpc_module.py @@ -168,10 +168,10 @@ def validation_step(self, batch, batch_nb): def shared_step(self, batch): try: from pl_bolts.datamodules.stl10_datamodule import STL10DataModule - except ModuleNotFoundError: + except ModuleNotFoundError as err: raise ModuleNotFoundError( # pragma: no-cover 'You want to use `torchvision` which is not installed yet, install it with `pip install torchvision`.' - ) + ) from err if isinstance(self.datamodule, STL10DataModule): unlabeled_batch = batch[0]