Skip to content

Commit

Permalink
Use tomllib on Python 3.11 (#12305)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Mar 8, 2022
1 parent 8650f5c commit 5d82d5b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion mypy-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
typing_extensions>=3.10
mypy_extensions>=0.4.3
typed_ast>=1.4.0,<2; python_version<'3.8'
tomli>=1.1.0
tomli>=1.1.0; python_version<'3.11'
12 changes: 8 additions & 4 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import re
import sys

import tomli
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

from typing import (Any, Callable, Dict, List, Mapping, MutableMapping, Optional, Sequence,
TextIO, Tuple, Union)
from typing_extensions import Final, TypeAlias as _TypeAlias
Expand Down Expand Up @@ -178,8 +182,8 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
continue
try:
if is_toml(config_file):
with open(config_file, encoding="utf-8") as f:
toml_data = tomli.loads(f.read())
with open(config_file, "rb") as f:
toml_data = tomllib.load(f)
# Filter down to just mypy relevant toml keys
toml_data = toml_data.get('tool', {})
if 'mypy' not in toml_data:
Expand All @@ -191,7 +195,7 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
config_parser.read(config_file)
parser = config_parser
config_types = ini_config_types
except (tomli.TOMLDecodeError, configparser.Error, ConfigTOMLValueError) as err:
except (tomllib.TOMLDecodeError, configparser.Error, ConfigTOMLValueError) as err:
print("%s: %s" % (config_file, err), file=stderr)
else:
if config_file in defaults.SHARED_CONFIG_FILES and 'mypy' not in parser:
Expand Down
11 changes: 7 additions & 4 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import sys
from enum import Enum, unique

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

from typing import Dict, Iterator, List, NamedTuple, Optional, Set, Tuple, Union
from typing_extensions import Final, TypeAlias as _TypeAlias

Expand Down Expand Up @@ -449,10 +454,8 @@ def _is_compatible_stub_package(self, stub_dir: str) -> bool:
"""
metadata_fnam = os.path.join(stub_dir, 'METADATA.toml')
if os.path.isfile(metadata_fnam):
# Delay import for a possible minor performance win.
import tomli
with open(metadata_fnam, encoding="utf-8") as f:
metadata = tomli.loads(f.read())
with open(metadata_fnam, "rb") as f:
metadata = tomllib.load(f)
if self.python_major_ver == 2:
return bool(metadata.get('python2', False))
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def run(self):
install_requires=["typed_ast >= 1.4.0, < 2; python_version<'3.8'",
'typing_extensions>=3.10',
'mypy_extensions >= 0.4.3',
'tomli>=1.1.0',
"tomli>=1.1.0; python_version<'3.11'",
],
# Same here.
extras_require={
Expand Down

0 comments on commit 5d82d5b

Please sign in to comment.