Skip to content

Commit

Permalink
Switch from toml to tomli for TOML v1 support (#10824)
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin authored Jul 17, 2021
1 parent 3bef7b8 commit aede3ae
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
1 change: 0 additions & 1 deletion build-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
-r mypy-requirements.txt
types-typed-ast>=1.4.0,<1.5.0
types-toml>=0.0
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.7.4
mypy_extensions>=0.4.3,<0.5.0
typed_ast>=1.4.0,<1.5.0
toml
tomli<2.0.0
15 changes: 7 additions & 8 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import argparse
from collections import OrderedDict
import configparser
import glob as fileglob
from io import StringIO
import os
import re
import sys

import toml
import tomli
from typing import (Any, Callable, Dict, List, Mapping, MutableMapping, Optional, Sequence,
TextIO, Tuple, Union, cast)
TextIO, Tuple, Union)
from typing_extensions import Final

from mypy import defaults
Expand Down Expand Up @@ -169,20 +168,20 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
continue
try:
if is_toml(config_file):
toml_data = cast("OrderedDict[str, Any]",
toml.load(config_file, _dict=OrderedDict))
with open(config_file, encoding="utf-8") as f:
toml_data = tomli.load(f)
# Filter down to just mypy relevant toml keys
toml_data = toml_data.get('tool', {})
if 'mypy' not in toml_data:
continue
toml_data = OrderedDict({'mypy': toml_data['mypy']})
toml_data = {'mypy': toml_data['mypy']}
parser: MutableMapping[str, Any] = destructure_overrides(toml_data)
config_types = toml_config_types
else:
config_parser.read(config_file)
parser = config_parser
config_types = ini_config_types
except (toml.TomlDecodeError, configparser.Error, ConfigTOMLValueError) as err:
except (tomli.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 Expand Up @@ -252,7 +251,7 @@ def is_toml(filename: str) -> bool:
return filename.lower().endswith('.toml')


def destructure_overrides(toml_data: "OrderedDict[str, Any]") -> "OrderedDict[str, Any]":
def destructure_overrides(toml_data: Dict[str, Any]) -> Dict[str, Any]:
"""Take the new [[tool.mypy.overrides]] section array in the pyproject.toml file,
and convert it back to a flatter structure that the existing config_parser can handle.
Expand Down
6 changes: 3 additions & 3 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ 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 toml
with open(metadata_fnam, 'r') as f:
metadata = toml.load(f)
import tomli
with open(metadata_fnam, 'r', encoding="utf-8") as f:
metadata = tomli.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 @@ -193,7 +193,7 @@ def run(self):
install_requires=["typed_ast >= 1.4.0, < 1.5.0; python_version<'3.8'",
'typing_extensions>=3.7.4',
'mypy_extensions >= 0.4.3, < 0.5.0',
'toml',
'tomli<2.0.0',
],
# Same here.
extras_require={'dmypy': 'psutil >= 4.0', 'python2': 'typed_ast >= 1.4.0, < 1.5.0'},
Expand Down

0 comments on commit aede3ae

Please sign in to comment.