From 1af29fbfa507daa8166e7aac659e9b2ff2b47a3c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 2 Apr 2022 08:29:32 -0700 Subject: [PATCH] try-except tomllib import (#2987) See #2965 I left the version check in place because mypy doesn't generally like try-excepted imports. --- CHANGES.md | 3 +++ setup.py | 2 +- src/black/files.py | 6 +++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f68bc8f..f2bdbd2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -39,6 +39,9 @@ +- Use `tomli` instead of `tomllib` on Python 3.11 builds where `tomllib` is not + available (#2987) + ### Parser diff --git a/setup.py b/setup.py index e23a58c..522a42a 100644 --- a/setup.py +++ b/setup.py @@ -98,7 +98,7 @@ setup( install_requires=[ "click>=8.0.0", "platformdirs>=2", - "tomli>=1.1.0; python_version < '3.11'", + "tomli>=1.1.0; python_full_version < '3.11.0a7'", "typed-ast>=1.4.2; python_version < '3.8' and implementation_name == 'cpython'", "pathspec>=0.9.0", "dataclasses>=0.6; python_version < '3.7'", diff --git a/src/black/files.py b/src/black/files.py index 52c77c6..0382397 100644 --- a/src/black/files.py +++ b/src/black/files.py @@ -22,7 +22,11 @@ from pathspec import PathSpec from pathspec.patterns.gitwildmatch import GitWildMatchPatternError if sys.version_info >= (3, 11): - import tomllib + try: + import tomllib + except ImportError: + # Help users on older alphas + import tomli as tomllib else: import tomli as tomllib -- 2.39.5