Skip to content

Commit

Permalink
python 3.11 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and radoering committed Apr 3, 2023
1 parent 2602a84 commit 46b6e7b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/poetry/core/pyproject/toml.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

import tomli

from poetry.core.pyproject.tables import BuildSystem
from poetry.core.utils._compat import tomllib


class PyProjectTOML:
Expand All @@ -26,7 +25,7 @@ def data(self) -> dict[str, Any]:
self._data = {}
else:
with self.file.open("rb") as f:
self._data = tomli.load(f)
self._data = tomllib.load(f)

return self._data

Expand Down
8 changes: 8 additions & 0 deletions src/poetry/core/utils/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@


WINDOWS = sys.platform == "win32"

if sys.version_info < (3, 11):
# compatibility for python <3.11
import tomli as tomllib
else:
import tomllib # nopycln: import

__all__ = ["tomllib"]
6 changes: 3 additions & 3 deletions tests/pyproject/test_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
from pathlib import Path

import pytest
import tomli

from poetry.core.pyproject.exceptions import PyProjectException
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.utils._compat import tomllib


def test_pyproject_toml_simple(
pyproject_toml: Path, build_system_section: str, poetry_section: str
) -> None:
with pyproject_toml.open("rb") as f:
data = tomli.load(f)
data = tomllib.load(f)
assert PyProjectTOML(pyproject_toml).data == data


Expand All @@ -35,7 +35,7 @@ def test_pyproject_toml_poetry_config(
) -> None:
pyproject = PyProjectTOML(pyproject_toml)
with pyproject_toml.open("rb") as f:
doc = tomli.load(f)
doc = tomllib.load(f)
config = doc["tool"]["poetry"]

assert pyproject.is_poetry_project()
Expand Down
14 changes: 7 additions & 7 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from typing import cast

import pytest
import tomli

from packaging.utils import canonicalize_name

from poetry.core.constraints.version import parse_constraint
from poetry.core.factory import Factory
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.utils._compat import tomllib
from poetry.core.version.markers import SingleMarker


Expand Down Expand Up @@ -187,7 +187,7 @@ def test_create_poetry_with_multi_constraints_dependency() -> None:
def test_validate() -> None:
complete = fixtures_dir / "complete.toml"
with complete.open("rb") as f:
doc = tomli.load(f)
doc = tomllib.load(f)
content = doc["tool"]["poetry"]

assert Factory.validate(content) == {"errors": [], "warnings": []}
Expand All @@ -196,7 +196,7 @@ def test_validate() -> None:
def test_validate_fails() -> None:
complete = fixtures_dir / "complete.toml"
with complete.open("rb") as f:
doc = tomli.load(f)
doc = tomllib.load(f)
content = doc["tool"]["poetry"]
content["authors"] = "this is not a valid array"

Expand All @@ -210,7 +210,7 @@ def test_validate_without_strict_fails_only_non_strict() -> None:
fixtures_dir / "project_failing_strict_validation" / "pyproject.toml"
)
with project_failing_strict_validation.open("rb") as f:
doc = tomli.load(f)
doc = tomllib.load(f)
content = doc["tool"]["poetry"]

assert Factory.validate(content) == {
Expand All @@ -229,7 +229,7 @@ def test_validate_strict_fails_strict_and_non_strict() -> None:
fixtures_dir / "project_failing_strict_validation" / "pyproject.toml"
)
with project_failing_strict_validation.open("rb") as f:
doc = tomli.load(f)
doc = tomllib.load(f)
content = doc["tool"]["poetry"]

assert Factory.validate(content, strict=True) == {
Expand Down Expand Up @@ -271,7 +271,7 @@ def test_validate_strict_fails_strict_and_non_strict() -> None:
def test_strict_validation_success_on_multiple_readme_files() -> None:
with_readme_files = fixtures_dir / "with_readme_files" / "pyproject.toml"
with with_readme_files.open("rb") as f:
doc = tomli.load(f)
doc = tomllib.load(f)
content = doc["tool"]["poetry"]

assert Factory.validate(content, strict=True) == {"errors": [], "warnings": []}
Expand All @@ -280,7 +280,7 @@ def test_strict_validation_success_on_multiple_readme_files() -> None:
def test_strict_validation_fails_on_readme_files_with_unmatching_types() -> None:
with_readme_files = fixtures_dir / "with_readme_files" / "pyproject.toml"
with with_readme_files.open("rb") as f:
doc = tomli.load(f)
doc = tomllib.load(f)
content = doc["tool"]["poetry"]
content["readme"][0] = "README.md"

Expand Down
5 changes: 3 additions & 2 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from typing import Any
from typing import Generator

import tomli
import tomli_w

from poetry.core.utils._compat import tomllib


__toml_build_backend_patch__ = {
"build-system": {
Expand Down Expand Up @@ -46,7 +47,7 @@ def temporary_project_directory(
shutil.copytree(str(path), dst)
toml = dst / "pyproject.toml"
with toml.open("rb") as f:
data = tomli.load(f)
data = tomllib.load(f)
data.update(toml_patch or __toml_build_backend_patch__)
with toml.open("wb") as f:
tomli_w.dump(data, f)
Expand Down

0 comments on commit 46b6e7b

Please sign in to comment.