From f903034a584c5ef3c67cae5aa0aef1b7cc2db194 Mon Sep 17 00:00:00 2001 From: dongfangtianyu <7629022+dongfangtianyu@users.noreply.github.com> Date: Wed, 24 Feb 2021 13:41:30 +0800 Subject: [PATCH] fix :Failed to pull configuration information from pyproject.toml --- isort/settings.py | 2 +- tests/unit/test_settings.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/isort/settings.py b/isort/settings.py index bc0782c55..1ea12179e 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -647,7 +647,7 @@ def _find_config(path: str) -> Tuple[str, Dict[str, Any]]: def _get_config_data(file_path: str, sections: Tuple[str]) -> Dict[str, Any]: settings: Dict[str, Any] = {} - with open(file_path) as config_file: + with open(file_path, encoding="utf-8") as config_file: if file_path.endswith(".toml"): config = toml.load(config_file) for section in sections: diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py index 157a087f4..6ef8f70ec 100644 --- a/tests/unit/test_settings.py +++ b/tests/unit/test_settings.py @@ -189,6 +189,31 @@ def test_editorconfig_without_sections(tmpdir): assert not loaded_settings +def test_get_config_data_with_toml_and_utf8(tmpdir): + test_config = tmpdir.join("pyproject.toml") + # Exception: UnicodeDecodeError: 'gbk' codec can't decode byte 0x84 in position 57 + test_config.write_text( + """ +[tool.poetry] + +description = "基于FastAPI + Mysql的 TodoList" # Exception: UnicodeDecodeError +name = "TodoList" +version = "0.1.0" + +[tool.isort] + +multi_line_output = 3 + +""", + "utf8", + ) + loaded_settings = settings._get_config_data( + str(test_config), sections=settings.CONFIG_SECTIONS["pyproject.toml"] + ) + assert loaded_settings + assert str(tmpdir) in loaded_settings["source"] + + def test_as_bool(): assert settings._as_bool("TrUe") is True assert settings._as_bool("true") is True