try-except tomllib import (#2987)

See #2965 

I left the version check in place because mypy doesn't generally like try-excepted imports.
This commit is contained in:
Jelle Zijlstra 2022-04-02 08:29:32 -07:00 committed by GitHub
parent 5436810921
commit 1af29fbfa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 2 deletions

View File

@ -39,6 +39,9 @@
<!-- Changes to how Black is packaged, such as dependency requirements -->
- Use `tomli` instead of `tomllib` on Python 3.11 builds where `tomllib` is not
available (#2987)
### Parser
<!-- Changes to the parser or to version autodetection -->

View File

@ -98,7 +98,7 @@ def find_python_files(base: Path) -> List[Path]:
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'",

View File

@ -22,7 +22,11 @@
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