Skip to content

Commit

Permalink
feat(core): Use tomllib on Python 3.11 (#1072)
Browse files Browse the repository at this point in the history
* docs: 📝 Fix typo in `pip install pdm` description (#1061)

* Use tomllib on Python 3.11

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* news

* use a compatibility module

* missed one import

Co-authored-by: t106362512 <[email protected]>
Co-authored-by: hauntsaninja <>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored and frostming committed Jun 24, 2022
1 parent 2a8bbcf commit 9a2f2b6
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 26 deletions.
1 change: 1 addition & 0 deletions news/1072.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use tomllib on Python 3.11
4 changes: 2 additions & 2 deletions pdm/builders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterable, Mapping

import tomli
from pep517.wrappers import Pep517HookCaller

from pdm.compat import tomllib
from pdm.exceptions import BuildError
from pdm.models.in_process import get_sys_config_paths
from pdm.models.requirements import parse_requirement
Expand Down Expand Up @@ -174,7 +174,7 @@ def __init__(self, src_dir: str | Path, environment: Environment) -> None:
logger.debug("Preparing isolated env for PEP 517 build...")
try:
with open(os.path.join(src_dir, "pyproject.toml"), "rb") as f:
spec = tomli.load(f)
spec = tomllib.load(f)
except FileNotFoundError:
spec = {}
except Exception as e:
Expand Down
10 changes: 8 additions & 2 deletions pdm/cli/completions/pdm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,19 @@ function getPyPIPackages() {
}

function getPdmPackages() {
& $PDM_PYTHON -c "import os, re, tomli
& $PDM_PYTHON -c "
import sys
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
import os, re
PACKAGE_REGEX = re.compile(r'^[A-Za-z][A-Za-z0-9._-]*')
def get_packages(lines):
return [PACKAGE_REGEX.match(line).group() for line in lines]
with open('pyproject.toml', 'rb') as f:
data = tomli.load(f)
data = tomllib.load(f)
packages = get_packages(data.get('project', {}).get('dependencies', []))
for reqs in data.get('project', {}).get('optional-dependencies', {}).values():
packages.extend(get_packages(reqs))
Expand Down
9 changes: 7 additions & 2 deletions pdm/cli/completions/pdm.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,18 @@ _pdm_groups() {

_get_packages_with_python() {
command ${PDM_PYTHON} - << EOF
import os, re, tomli
import sys
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
import os, re
PACKAGE_REGEX = re.compile(r'^[A-Za-z][A-Za-z0-9._-]*')
def get_packages(lines):
return [PACKAGE_REGEX.match(line).group() for line in lines]
with open('pyproject.toml', 'rb') as f:
data = tomli.load(f)
data = tomllib.load(f)
packages = get_packages(data.get('project', {}).get('dependencies', []))
for reqs in data.get('project', {}).get('optional-dependencies', {}).values():
packages.extend(get_packages(reqs))
Expand Down
8 changes: 8 additions & 0 deletions pdm/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sys

if sys.version_info >= (3, 11):
import tomllib as tomllib
else:
import tomli

tomllib = tomli
9 changes: 4 additions & 5 deletions pdm/formats/flit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from pathlib import Path
from typing import Any, Dict, List, Mapping, Optional, Tuple, cast

import tomli

from pdm.compat import tomllib
from pdm.formats.base import (
MetaConverter,
Unset,
Expand All @@ -22,8 +21,8 @@
def check_fingerprint(project: Optional[Project], filename: PathLike) -> bool:
with open(filename, "rb") as fp:
try:
data = tomli.load(fp)
except tomli.TOMLDecodeError:
data = tomllib.load(fp)
except tomllib.TOMLDecodeError:
return False

return "tool" in data and "flit" in data["tool"]
Expand Down Expand Up @@ -145,7 +144,7 @@ def convert(
) -> Tuple[Mapping, Mapping]:
with open(filename, "rb") as fp, cd(os.path.dirname(os.path.abspath(filename))):
converter = FlitMetaConverter(
tomli.load(fp)["tool"]["flit"], project.core.ui if project else None
tomllib.load(fp)["tool"]["flit"], project.core.ui if project else None
)
return converter.convert()

Expand Down
11 changes: 6 additions & 5 deletions pdm/formats/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from pathlib import Path
from typing import Any, Dict, List, Mapping, Optional, Set, Tuple, Union, cast

import tomli

from pdm._types import RequirementDict, Source
from pdm.compat import tomllib
from pdm.formats.base import (
MetaConverter,
Unset,
Expand All @@ -22,8 +21,8 @@
def check_fingerprint(project: Project, filename: PathLike) -> bool:
with open(filename, "rb") as fp:
try:
data = tomli.load(fp)
except tomli.TOMLDecodeError:
data = tomllib.load(fp)
except tomllib.TOMLDecodeError:
return False

return (
Expand Down Expand Up @@ -168,7 +167,9 @@ def convert(
project: Project, filename: Path, options: Optional[Namespace]
) -> Tuple[Mapping[str, Any], Mapping[str, Any]]:
with open(filename, "rb") as fp:
converter = LegacyMetaConverter(tomli.load(fp)["tool"]["pdm"], project.core.ui)
converter = LegacyMetaConverter(
tomllib.load(fp)["tool"]["pdm"], project.core.ui
)
return converter.convert()


Expand Down
4 changes: 2 additions & 2 deletions pdm/formats/pipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from os import PathLike
from typing import Any

import tomli
from packaging.markers import default_environment

from pdm._types import RequirementDict
from pdm.compat import tomllib
from pdm.formats.base import make_array
from pdm.models.markers import Marker
from pdm.models.requirements import Requirement
Expand Down Expand Up @@ -45,7 +45,7 @@ def convert(
project: Project, filename: PathLike, options: Namespace | None
) -> tuple[dict[str, Any], dict[str, Any]]:
with open(filename, "rb") as fp:
data = tomli.load(fp)
data = tomllib.load(fp)
result = {}
settings = {}
if "pipenv" in data:
Expand Down
9 changes: 4 additions & 5 deletions pdm/formats/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Mapping

import tomli

from pdm._types import RequirementDict, Source
from pdm.compat import tomllib
from pdm.formats.base import (
MetaConverter,
Unset,
Expand All @@ -32,8 +31,8 @@
def check_fingerprint(project: Project | None, filename: Path | str) -> bool:
with open(filename, "rb") as fp:
try:
data = tomli.load(fp)
except tomli.TOMLDecodeError:
data = tomllib.load(fp)
except tomllib.TOMLDecodeError:
return False

return "tool" in data and "poetry" in data["tool"]
Expand Down Expand Up @@ -202,7 +201,7 @@ def convert(
) -> tuple[Mapping[str, Any], Mapping[str, Any]]:
with open(filename, "rb") as fp, cd(os.path.dirname(os.path.abspath(filename))):
converter = PoetryMetaConverter(
tomli.load(fp)["tool"]["poetry"], project.core.ui if project else None
tomllib.load(fp)["tool"]["poetry"], project.core.ui if project else None
)
return converter.convert()

Expand Down
4 changes: 2 additions & 2 deletions pdm/formats/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def parse_requirement_file(


def check_fingerprint(project: Project, filename: PathLike) -> bool:
import tomli
from pdm.compat import tomllib

with open(filename, "rb") as fp:
try:
tomli.load(fp)
tomllib.load(fp)
except ValueError:
# the file should be a requirements.txt if it not a TOML document.
return True
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies = [
"python-dotenv>=0.15",
"resolvelib>=0.8,<0.9",
"shellingham>=1.3.2",
"tomli>=1.1.0",
"tomli>=1.1.0; python_version < \"3.11\"",
"tomlkit>=0.8.0,<1",
"typing-extensions; python_version < \"3.8\"",
"wheel>=0.36.2",
Expand Down

0 comments on commit 9a2f2b6

Please sign in to comment.