Use tomllib on Python 3.11 (#4476)

* Use tomllib on Python 3.11

* changelog
This commit is contained in:
Shantanu
2022-09-05 03:00:56 -07:00
committed by GitHub
parent 317bef33b0
commit 5a2ddec9b2
2 changed files with 15 additions and 10 deletions
+1
View File
@@ -0,0 +1 @@
Use `tomllib` on Python 3.11 when parsing `mypy` configuration.
+14 -10
View File
@@ -1,3 +1,4 @@
import sys
from configparser import ConfigParser
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type as TypingType, Union
@@ -820,18 +821,21 @@ def parse_toml(config_file: str) -> Optional[Dict[str, Any]]:
return None
read_mode = 'rb'
try:
import tomli as toml_
except ImportError:
# older versions of mypy have toml as a dependency, not tomli
read_mode = 'r'
if sys.version_info >= (3, 11):
import tomllib as toml_
else:
try:
import toml as toml_ # type: ignore[no-redef]
except ImportError: # pragma: no cover
import warnings
import tomli as toml_
except ImportError:
# older versions of mypy have toml as a dependency, not tomli
read_mode = 'r'
try:
import toml as toml_ # type: ignore[no-redef]
except ImportError: # pragma: no cover
import warnings
warnings.warn('No TOML parser installed, cannot read configuration from `pyproject.toml`.')
return None
warnings.warn('No TOML parser installed, cannot read configuration from `pyproject.toml`.')
return None
with open(config_file, read_mode) as rf:
return toml_.load(rf) # type: ignore[arg-type]