Skip to content

Commit 5d82d5b

Browse files
authored
Use tomllib on Python 3.11 (#12305)
1 parent 8650f5c commit 5d82d5b

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

mypy-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
typing_extensions>=3.10
22
mypy_extensions>=0.4.3
33
typed_ast>=1.4.0,<2; python_version<'3.8'
4-
tomli>=1.1.0
4+
tomli>=1.1.0; python_version<'3.11'

mypy/config_parser.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import re
77
import sys
88

9-
import tomli
9+
if sys.version_info >= (3, 11):
10+
import tomllib
11+
else:
12+
import tomli as tomllib
13+
1014
from typing import (Any, Callable, Dict, List, Mapping, MutableMapping, Optional, Sequence,
1115
TextIO, Tuple, Union)
1216
from typing_extensions import Final, TypeAlias as _TypeAlias
@@ -178,8 +182,8 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
178182
continue
179183
try:
180184
if is_toml(config_file):
181-
with open(config_file, encoding="utf-8") as f:
182-
toml_data = tomli.loads(f.read())
185+
with open(config_file, "rb") as f:
186+
toml_data = tomllib.load(f)
183187
# Filter down to just mypy relevant toml keys
184188
toml_data = toml_data.get('tool', {})
185189
if 'mypy' not in toml_data:
@@ -191,7 +195,7 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
191195
config_parser.read(config_file)
192196
parser = config_parser
193197
config_types = ini_config_types
194-
except (tomli.TOMLDecodeError, configparser.Error, ConfigTOMLValueError) as err:
198+
except (tomllib.TOMLDecodeError, configparser.Error, ConfigTOMLValueError) as err:
195199
print("%s: %s" % (config_file, err), file=stderr)
196200
else:
197201
if config_file in defaults.SHARED_CONFIG_FILES and 'mypy' not in parser:

mypy/modulefinder.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
import sys
1313
from enum import Enum, unique
1414

15+
if sys.version_info >= (3, 11):
16+
import tomllib
17+
else:
18+
import tomli as tomllib
19+
1520
from typing import Dict, Iterator, List, NamedTuple, Optional, Set, Tuple, Union
1621
from typing_extensions import Final, TypeAlias as _TypeAlias
1722

@@ -449,10 +454,8 @@ def _is_compatible_stub_package(self, stub_dir: str) -> bool:
449454
"""
450455
metadata_fnam = os.path.join(stub_dir, 'METADATA.toml')
451456
if os.path.isfile(metadata_fnam):
452-
# Delay import for a possible minor performance win.
453-
import tomli
454-
with open(metadata_fnam, encoding="utf-8") as f:
455-
metadata = tomli.loads(f.read())
457+
with open(metadata_fnam, "rb") as f:
458+
metadata = tomllib.load(f)
456459
if self.python_major_ver == 2:
457460
return bool(metadata.get('python2', False))
458461
else:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def run(self):
198198
install_requires=["typed_ast >= 1.4.0, < 2; python_version<'3.8'",
199199
'typing_extensions>=3.10',
200200
'mypy_extensions >= 0.4.3',
201-
'tomli>=1.1.0',
201+
"tomli>=1.1.0; python_version<'3.11'",
202202
],
203203
# Same here.
204204
extras_require={

0 commit comments

Comments
 (0)