diff --git a/pipenv_setup/setup_updater.py b/pipenv_setup/setup_updater.py index 6753c2f..447e256 100644 --- a/pipenv_setup/setup_updater.py +++ b/pipenv_setup/setup_updater.py @@ -5,7 +5,8 @@ from io import BytesIO from subprocess import PIPE, Popen from tokenize import OP -from typing import Any, List, Tuple +from types import ModuleType +from typing import Any, List, Optional, Tuple from vistir.compat import Path @@ -149,11 +150,14 @@ def _get_formatting_module(): # type: () -> Optional[ModuleType] try: import black except: - import autopep8 + try: + import autopep8 + except ImportError: + return None return autopep8 - else: - return black + + return black def format_file(file): # type: (Path) -> None diff --git a/tests/setup_updater_test.py b/tests/setup_updater_test.py index 8f5719b..8efd5c9 100644 --- a/tests/setup_updater_test.py +++ b/tests/setup_updater_test.py @@ -101,3 +101,13 @@ def test_autopep8(monkeypatch, dummy_import): # type: (MonkeyPatch, Callable) - is_autopep8 = setup_updater._get_formatting_module() assert is_autopep8 is autopep8 + + +def test_none(monkeypatch, extra_dummy_import): # type: (MonkeyPatch, Callable) -> None + """ + verify `None` is returned if neither `black` nor `autopep8` is present + """ + patch_import(monkeypatch, extra_dummy_import) + is_none = setup_updater._get_formatting_module() + + assert is_none is None