Skip to content

Commit

Permalink
update poetry to work with poetry-core after tomlkit is removed from …
Browse files Browse the repository at this point in the history
…there (#6616)

Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com>
  • Loading branch information
dimbleby and radoering authored Apr 3, 2023
1 parent c8e963e commit c89e994
Show file tree
Hide file tree
Showing 36 changed files with 318 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/poetry/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from typing import Any

from packaging.utils import canonicalize_name
from poetry.core.toml import TOMLFile

from poetry.config.dict_config_source import DictConfigSource
from poetry.config.file_config_source import FileConfigSource
from poetry.locations import CONFIG_DIR
from poetry.locations import DEFAULT_CACHE_DIR
from poetry.toml import TOMLFile


if TYPE_CHECKING:
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/config/file_config_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
if TYPE_CHECKING:
from collections.abc import Iterator

from poetry.core.toml.file import TOMLFile
from tomlkit.toml_document import TOMLDocument

from poetry.toml.file import TOMLFile


class FileConfigSource(ConfigSource):
def __init__(self, file: TOMLFile, auth_config: bool = False) -> None:
Expand Down
3 changes: 1 addition & 2 deletions src/poetry/console/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ def validate_classifiers(
return errors, warnings

def handle(self) -> int:
from poetry.core.pyproject.toml import PyProjectTOML

from poetry.factory import Factory
from poetry.pyproject.toml import PyProjectTOML

# Load poetry config and display errors, if any
poetry_file = self.poetry.file.path
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ def handle(self) -> int:
from pathlib import Path

from poetry.core.pyproject.exceptions import PyProjectException
from poetry.core.toml.file import TOMLFile

from poetry.config.config import Config
from poetry.config.file_config_source import FileConfigSource
from poetry.locations import CONFIG_DIR
from poetry.toml.file import TOMLFile

config = Config.create()
config_file = TOMLFile(CONFIG_DIR / "config.toml")
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def __init__(self) -> None:
def handle(self) -> int:
from pathlib import Path

from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.vcs.git import GitConfig

from poetry.config.config import Config
from poetry.layouts import layout
from poetry.pyproject.toml import PyProjectTOML
from poetry.utils.env import EnvManager

project_path = Path.cwd()
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/self/self_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

from poetry.core.packages.dependency import Dependency
from poetry.core.packages.project_package import ProjectPackage
from poetry.core.pyproject.toml import PyProjectTOML

from poetry.__version__ import __version__
from poetry.console.commands.installer_command import InstallerCommand
from poetry.factory import Factory
from poetry.pyproject.toml import PyProjectTOML
from poetry.utils.env import EnvManager
from poetry.utils.env import SystemEnv
from poetry.utils.helpers import directory
Expand Down
14 changes: 9 additions & 5 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
from poetry.core.factory import Factory as BaseFactory
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.project_package import ProjectPackage
from poetry.core.toml.file import TOMLFile

from poetry.config.config import Config
from poetry.json import validate_object
from poetry.packages.locker import Locker
from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager
from poetry.poetry import Poetry
from poetry.toml.file import TOMLFile


if TYPE_CHECKING:
Expand Down Expand Up @@ -55,15 +55,19 @@ def create_poetry(

base_poetry = super().create_poetry(cwd=cwd, with_groups=with_groups)

locker = Locker(
base_poetry.file.parent / "poetry.lock", base_poetry.local_config
# TODO: backward compatibility, can be simplified if poetry-core with
# https://github.com/python-poetry/poetry-core/pull/483 is available
poetry_file: Path = (
getattr(base_poetry, "pyproject_path", None) or base_poetry.file.path
)

locker = Locker(poetry_file.parent / "poetry.lock", base_poetry.local_config)

# Loading global configuration
config = Config.create()

# Loading local configuration
local_config_file = TOMLFile(base_poetry.file.parent / "poetry.toml")
local_config_file = TOMLFile(poetry_file.parent / "poetry.toml")
if local_config_file.exists():
if io.is_debug():
io.write_line(f"Loading configuration file {local_config_file.path}")
Expand All @@ -82,7 +86,7 @@ def create_poetry(
config.merge({"repositories": repositories})

poetry = Poetry(
base_poetry.file.path,
poetry_file,
base_poetry.local_config,
base_poetry.package,
locker,
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/inspection/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
from poetry.core.factory import Factory
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.utils.helpers import parse_requires
from poetry.core.utils.helpers import temporary_directory
from poetry.core.version.markers import InvalidMarker
from poetry.core.version.requirements import InvalidRequirement

from poetry.pyproject.toml import PyProjectTOML
from poetry.utils.env import EnvCommandError
from poetry.utils.env import ephemeral_environment
from poetry.utils.setup_reader import SetupReader
Expand Down
3 changes: 1 addition & 2 deletions src/poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,8 @@ def _prepare_git_archive(self, operation: Install | Update) -> Path:
def _install_directory_without_wheel_installer(
self, operation: Install | Update
) -> int:
from poetry.core.pyproject.toml import PyProjectTOML

from poetry.factory import Factory
from poetry.pyproject.toml import PyProjectTOML

package = operation.package
operation_message = self.get_operation_message(operation)
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/installation/pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from typing import Any

from poetry.core.constraints.version import Version
from poetry.core.pyproject.toml import PyProjectTOML

from poetry.installation.base_installer import BaseInstaller
from poetry.pyproject.toml import PyProjectTOML
from poetry.repositories.http_repository import HTTPRepository
from poetry.utils._compat import encode
from poetry.utils.helpers import remove_directory
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from typing import Any

from packaging.utils import canonicalize_name
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.utils.helpers import module_name
from tomlkit import inline_table
from tomlkit import loads
from tomlkit import table
from tomlkit.toml_document import TOMLDocument

from poetry.pyproject.toml import PyProjectTOML


if TYPE_CHECKING:
from typing import Mapping
Expand Down
14 changes: 5 additions & 9 deletions src/poetry/masonry/builders/editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import hashlib
import json
import os
import shutil

from base64 import urlsafe_b64encode
from pathlib import Path
Expand Down Expand Up @@ -44,6 +43,7 @@

class EditableBuilder(Builder):
def __init__(self, poetry: Poetry, env: Env, io: IO) -> None:
self._poetry: Poetry
super().__init__(poetry)

self._env = env
Expand Down Expand Up @@ -105,19 +105,15 @@ def _setup_build(self) -> None:
pip_install(self._path, self._env, upgrade=True, editable=True)
else:
# Temporarily rename pyproject.toml
shutil.move(
str(self._poetry.file), str(self._poetry.file.with_suffix(".tmp"))
)
renamed_pyproject = self._poetry.file.with_suffix(".tmp")
self._poetry.file.path.rename(renamed_pyproject)
try:
pip_install(self._path, self._env, upgrade=True, editable=True)
finally:
shutil.move(
str(self._poetry.file.with_suffix(".tmp")),
str(self._poetry.file),
)
renamed_pyproject.rename(self._poetry.file.path)
finally:
if not has_setup:
os.remove(str(setup))
os.remove(setup)

def _add_pth(self) -> list[Path]:
paths = {
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from poetry.core.constraints.version import parse_constraint
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package
from poetry.core.toml.file import TOMLFile
from poetry.core.version.markers import parse_marker
from poetry.core.version.requirements import InvalidRequirement
from tomlkit import array
Expand All @@ -26,6 +25,7 @@
from tomlkit import table

from poetry.__version__ import __version__
from poetry.toml.file import TOMLFile
from poetry.utils._compat import tomllib


Expand Down
22 changes: 21 additions & 1 deletion src/poetry/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

from typing import TYPE_CHECKING
from typing import Any
from typing import cast

from poetry.core.poetry import Poetry as BasePoetry

from poetry.__version__ import __version__
from poetry.config.source import Source
from poetry.pyproject.toml import PyProjectTOML


if TYPE_CHECKING:
Expand All @@ -18,6 +20,7 @@
from poetry.packages.locker import Locker
from poetry.plugins.plugin_manager import PluginManager
from poetry.repositories.repository_pool import RepositoryPool
from poetry.toml import TOMLFile


class Poetry(BasePoetry):
Expand All @@ -34,14 +37,31 @@ def __init__(
) -> None:
from poetry.repositories.repository_pool import RepositoryPool

super().__init__(file, local_config, package)
try:
super().__init__( # type: ignore[call-arg]
file, local_config, package, pyproject_type=PyProjectTOML
)
except TypeError:
# TODO: backward compatibility, can be simplified if poetry-core with
# https://github.com/python-poetry/poetry-core/pull/483 is available
super().__init__(file, local_config, package)
self._pyproject = PyProjectTOML(file)

self._locker = locker
self._config = config
self._pool = RepositoryPool()
self._plugin_manager: PluginManager | None = None
self._disable_cache = disable_cache

@property
def pyproject(self) -> PyProjectTOML:
pyproject = super().pyproject
return cast("PyProjectTOML", pyproject)

@property
def file(self) -> TOMLFile: # type: ignore[override]
return self.pyproject.file

@property
def locker(self) -> Locker:
return self._locker
Expand Down
Empty file.
62 changes: 62 additions & 0 deletions src/poetry/pyproject/toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from poetry.core.pyproject.toml import PyProjectTOML as BasePyProjectTOML
from tomlkit.api import table
from tomlkit.items import Table
from tomlkit.toml_document import TOMLDocument

from poetry.toml import TOMLFile


if TYPE_CHECKING:
from pathlib import Path


class PyProjectTOML(BasePyProjectTOML):
"""
Enhanced version of poetry-core's PyProjectTOML
which is capable of writing pyproject.toml
The poetry-core class uses tomli to read the file,
here we use tomlkit to preserve comments and formatting when writing.
"""

def __init__(self, path: Path) -> None:
super().__init__(path)
self._toml_file = TOMLFile(path=path)
self._toml_document: TOMLDocument | None = None

@property
def file(self) -> TOMLFile: # type: ignore[override]
return self._toml_file

@property
def data(self) -> TOMLDocument:
if self._toml_document is None:
if not self.file.exists():
self._toml_document = TOMLDocument()
else:
self._toml_document = self.file.read()

return self._toml_document

def save(self) -> None:
data = self.data

if self._build_system is not None:
if "build-system" not in data:
data["build-system"] = table()

build_system = data["build-system"]
assert isinstance(build_system, Table)

build_system["requires"] = self._build_system.requires
build_system["build-backend"] = self._build_system.build_backend

self.file.write(data=data)

def reload(self) -> None:
self._toml_document = None
self._build_system = None
7 changes: 7 additions & 0 deletions src/poetry/toml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import annotations

from poetry.toml.exceptions import TOMLError
from poetry.toml.file import TOMLFile


__all__ = ["TOMLError", "TOMLFile"]
8 changes: 8 additions & 0 deletions src/poetry/toml/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from poetry.core.exceptions import PoetryCoreException
from tomlkit.exceptions import TOMLKitError


class TOMLError(TOMLKitError, PoetryCoreException):
pass
Loading

0 comments on commit c89e994

Please sign in to comment.