Skip to content

Use tomllib on Python 3.11 #12305

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 2 commits into from
Mar 8, 2022
Merged
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
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
@@ -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
@@ -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:
@@ -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:
11 changes: 7 additions & 4 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
@@ -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

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

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

Fine to remove it since configparser.py already unconditionally imports tomllib anyway.

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:
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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={