Skip to content

Commit

Permalink
Use black for loading black config
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
jeremyheiler committed Dec 7, 2021
1 parent 8d0de31 commit b3be4b5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion elpy/blackutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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", {})
Expand Down

0 comments on commit b3be4b5

Please sign in to comment.