Skip to content
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

Use tomllib on Python 3.11 #4476

Merged
merged 2 commits into from Sep 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/4476-hauntsaninja.md
@@ -0,0 +1 @@
Use `tomllib` on Python 3.11 when parsing `mypy` configuration.
26 changes: 15 additions & 11 deletions pydantic/mypy.py
@@ -1,3 +1,4 @@
import sys
from configparser import ConfigParser
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type as TypingType, Union

Expand Down Expand Up @@ -820,18 +821,21 @@ def parse_toml(config_file: str) -> Optional[Dict[str, Any]]:
return None

read_mode = 'rb'
try:
import tomli as toml_
except ImportError:
# older versions of mypy have toml as a dependency, not tomli
read_mode = 'r'
if sys.version_info >= (3, 11):
import tomllib as toml_
else:
try:
import toml as toml_ # type: ignore[no-redef]
except ImportError: # pragma: no cover
import warnings

warnings.warn('No TOML parser installed, cannot read configuration from `pyproject.toml`.')
return None
import tomli as toml_
except ImportError:
# older versions of mypy have toml as a dependency, not tomli
read_mode = 'r'
try:
import toml as toml_ # type: ignore[no-redef]
except ImportError: # pragma: no cover
import warnings

warnings.warn('No TOML parser installed, cannot read configuration from `pyproject.toml`.')
return None

with open(config_file, read_mode) as rf:
return toml_.load(rf) # type: ignore[arg-type]