diff --git a/build-requirements.txt b/build-requirements.txt index aad1f8e59105..b4d024ee7f38 100644 --- a/build-requirements.txt +++ b/build-requirements.txt @@ -1,3 +1,2 @@ -r mypy-requirements.txt types-typed-ast>=1.4.0,<1.5.0 -types-toml>=0.0 diff --git a/mypy-requirements.txt b/mypy-requirements.txt index ba0b689e2f98..e7e2af80089b 100644 --- a/mypy-requirements.txt +++ b/mypy-requirements.txt @@ -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 diff --git a/mypy/config_parser.py b/mypy/config_parser.py index dc135f1c4a6c..d5552186ecc7 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -1,5 +1,4 @@ import argparse -from collections import OrderedDict import configparser import glob as fileglob from io import StringIO @@ -7,9 +6,9 @@ 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 @@ -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: @@ -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. diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index df5c556409a3..17f386e933c1 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -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: diff --git a/setup.py b/setup.py index ca3a7ebbace1..726229bea1fe 100644 --- a/setup.py +++ b/setup.py @@ -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'},