From 04b111110fdcfff747e4d1765b2c7045d84e21c0 Mon Sep 17 00:00:00 2001 From: hirosassa Date: Wed, 17 Feb 2021 22:00:26 +0900 Subject: [PATCH] raise import error --- yapf/yapflib/file_resources.py | 9 +++++++-- yapf/yapflib/style.py | 7 ++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/yapf/yapflib/file_resources.py b/yapf/yapflib/file_resources.py index eebfb9319..84a8a5427 100644 --- a/yapf/yapflib/file_resources.py +++ b/yapf/yapflib/file_resources.py @@ -105,12 +105,17 @@ def GetDefaultStyleForDir(dirname, default_style=style.DEFAULT_STYLE): pass # It's okay if it's not there. else: with fd: - import toml + try: + import toml + except ImportError: + raise errors.YapfError( + "toml package is needed for using pyproject.toml as a configuration file" + ) pyproject_toml = toml.load(config_file) style_dict = pyproject_toml.get('tool', {}).get('yapf', None) if style_dict is not None: - return config_file + return config_file if (not dirname or not os.path.basename(dirname) or dirname == os.path.abspath(os.path.sep)): diff --git a/yapf/yapflib/style.py b/yapf/yapflib/style.py index c7d447506..6bd2372f7 100644 --- a/yapf/yapflib/style.py +++ b/yapf/yapflib/style.py @@ -743,7 +743,12 @@ def _CreateConfigParserFromConfigFile(config_filename): with open(config_filename) as style_file: config = py3compat.ConfigParser() if config_filename.endswith(PYPROJECT_TOML): - import toml + try: + import toml + except ImportError: + raise errors.YapfError( + "toml package is needed for using pyproject.toml as a configuration file" + ) pyproject_toml = toml.load(style_file) style_dict = pyproject_toml.get("tool", {}).get("yapf", None)