From 0d4f1fab342017dddd8b914f71cf6938ac7ac622 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Fri, 23 Jul 2021 22:47:40 -0700 Subject: [PATCH] Make all exceptions pickleable --- CHANGELOG.md | 1 + isort/exceptions.py | 9 +++++++-- tests/unit/test_exceptions.py | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe69ecc22..8004d9c01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/ ### 5.9.3 TBD - Improved text of skipped file message to mention gitignore feature. + - Made all exceptions pickleable. - Fixed #1779: Pylama integration ignores pylama specific isort config overrides. - Fixed #1781: `--from-first` CLI flag shouldn't take any arguments. - Fixed #1785: `_ast` module incorrectly excluded from stdlib definition. diff --git a/isort/exceptions.py b/isort/exceptions.py index 1cd9251d2..6be82406a 100644 --- a/isort/exceptions.py +++ b/isort/exceptions.py @@ -1,4 +1,5 @@ """All isort specific exception classes should be defined here""" +from functools import partial from pathlib import Path from typing import Any, Dict, List, Type, Union @@ -8,6 +9,9 @@ class ISortError(Exception): """Base isort exception object from which all isort sourced exceptions should inherit""" + def __reduce__(self): # type: ignore + return (partial(type(self), **self.__dict__), ()) + class InvalidSettingsPath(ISortError): """Raised when a settings path is provided that is neither a valid file or directory""" @@ -48,13 +52,14 @@ class FileSkipped(ISortError): def __init__(self, message: str, file_path: str): super().__init__(message) + self.message = message self.file_path = file_path class FileSkipComment(FileSkipped): """Raised when an entire file is skipped due to a isort skip file comment""" - def __init__(self, file_path: str): + def __init__(self, file_path: str, **kwargs: str): super().__init__( f"{file_path} contains a file skip comment and was skipped.", file_path=file_path ) @@ -63,7 +68,7 @@ def __init__(self, file_path: str): class FileSkipSetting(FileSkipped): """Raised when an entire file is skipped due to provided isort settings""" - def __init__(self, file_path: str): + def __init__(self, file_path: str, **kwargs: str): super().__init__( f"{file_path} was skipped as it's listed in 'skip' setting" " or matches a glob in 'skip_glob' setting", diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index d3a063a7d..f153e3751 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -1,3 +1,5 @@ +import pickle + from isort import exceptions @@ -8,6 +10,9 @@ def setup_class(self): def test_init(self): assert isinstance(self.instance, exceptions.ISortError) + def test_pickleable(self): + assert isinstance(pickle.loads(pickle.dumps(self.instance)), exceptions.ISortError) + class TestExistingSyntaxErrors(TestISortError): def setup_class(self):