Skip to content

Switch from toml to tomli for TOML v1 support #10824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the discussion in #10219 it seemed like OrderedDict was only needed by Python 3.5 which is no longer supported, so guessing we don't need it anymore?

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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TOML spec requires UTF-8, so added the encoding kwarg

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