From b3be4b5e23b3acdbc428bd9df94e6a610c786a5e Mon Sep 17 00:00:00 2001 From: Jeremy Heiler Date: Sun, 5 Dec 2021 15:48:30 -0500 Subject: [PATCH] Use black for loading black config This changes how black configuration is loaded. Instead of looking for `pyproject.toml` directly, use a black to load the configuration in the same way the black CLI would. This allows for other configuration files to be considered, such as `~/.black` or `~/.config/black`. --- elpy/blackutil.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/elpy/blackutil.py b/elpy/blackutil.py index 1710c4113..a866f7e84 100644 --- a/elpy/blackutil.py +++ b/elpy/blackutil.py @@ -30,6 +30,14 @@ def parse_version(*arg, **kwargs): black = None else: import black + current_version = parse_version(black.__version__) + if current_version >= parse_version("21.5b1"): + from black.files import find_pyproject_toml + elif current_version >= parse_version("20.8b0"): + from black import find_pyproject_toml + else: + find_pyproject_toml = None + except ImportError: # pragma: no cover black = None @@ -43,7 +51,10 @@ def fix_code(code, directory): # Get black config from pyproject.toml line_length = black.DEFAULT_LINE_LENGTH string_normalization = True - pyproject_path = os.path.join(directory, "pyproject.toml") + if find_pyproject_toml: + pyproject_path = find_pyproject_toml(directory) + else: + pyproject_path = os.path.join(directory, "pyproject.toml") if toml is not None and os.path.exists(pyproject_path): pyproject_config = toml.load(pyproject_path) black_config = pyproject_config.get("tool", {}).get("black", {})