From 9b25a44e45423776f76a02eb2da0a205ea685818 Mon Sep 17 00:00:00 2001 From: Matt Usifer Date: Wed, 10 May 2023 09:01:44 -0400 Subject: [PATCH] Add support for tomli to parse pyproject.toml --- elpy/blackutil.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/elpy/blackutil.py b/elpy/blackutil.py index e09ed417..ec14aa3e 100644 --- a/elpy/blackutil.py +++ b/elpy/blackutil.py @@ -17,6 +17,11 @@ def parse_version(*arg, **kwargs): import os +try: + import tomli +except ImportError: + tomli = None + try: import toml except ImportError: @@ -55,8 +60,15 @@ def fix_code(code, directory): pyproject_path = find_pyproject_toml((directory,)) else: pyproject_path = os.path.join(directory, "pyproject.toml") - if toml and pyproject_path and os.path.exists(pyproject_path): - pyproject_config = toml.load(pyproject_path) + if pyproject_path and os.path.exists(pyproject_path): + if tomli: + with open(pyproject_path, "rb") as pyproject_file: + pyproject_config = tomli.load(pyproject_file) + elif toml: + pyproject_config = toml.load(pyproject_path) + else: + err("pyproject.toml file found, but tomli was not installed") + black_config = pyproject_config.get("tool", {}).get("black", {}) if "line-length" in black_config: line_length = black_config["line-length"]