Skip to content

Commit

Permalink
use the standard library tomllib on sufficiently new python
Browse files Browse the repository at this point in the history
In python 3.11 it is no longer necessary to vendor (?) a toml
implementation. The vendored one is now part of the standard library.
Simply use that instead.

Benefits include:
- micro-optimization when using the isort library API in an application
  that already uses tomllib, to avoid reading yet another one from
  on-disk
- seamless support for automatically dropping tomli once the minimum
  python version is upgraded, via tools such as pyupgrade
- distributors who patch out isort to use the system tomli, do not
  actually have to package tomli for versions of python that have
  tomllib
  • Loading branch information
eli-schwartz committed Dec 1, 2023
1 parent b67a6a5 commit 6054e94
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@
from .wrap_modes import from_string as wrap_mode_from_string

if TYPE_CHECKING:
tomli: Any
tomllib: Any
else:
from ._vendored import tomli
if sys.version_info >= (3, 11):
import tomllib
else:
from ._vendored import tomli

_SHEBANG_RE = re.compile(rb"^#!.*\bpython[23w]?\b")
CYTHON_EXTENSIONS = frozenset({"pyx", "pxd"})
Expand Down Expand Up @@ -831,7 +834,7 @@ def _get_config_data(file_path: str, sections: Tuple[str, ...]) -> Dict[str, Any

if file_path.endswith(".toml"):
with open(file_path, "rb") as bin_config_file:
config = tomli.load(bin_config_file)
config = tomllib.load(bin_config_file)
for section in sections:
config_section = config
for key in section.split("."):
Expand Down

0 comments on commit 6054e94

Please sign in to comment.