From c6b52f8f12ab6d2c2ceb289305b3572125202fe2 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 3 Sep 2022 19:27:46 -0700 Subject: [PATCH] Use tomllib on Python 3.11 --- pydantic/mypy.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/pydantic/mypy.py b/pydantic/mypy.py index 2619259094..8835275f51 100644 --- a/pydantic/mypy.py +++ b/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 @@ -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]