Skip to content

Commit

Permalink
poetry.toml_file -> poetry.file
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Apr 1, 2023
1 parent 6723772 commit aa235b8
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 129 deletions.
4 changes: 2 additions & 2 deletions src/poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def handle(self) -> int:

# tomlkit types are awkward to work with, treat content as a mostly untyped
# dictionary.
content: dict[str, Any] = self.poetry.toml_file.read()
content: dict[str, Any] = self.poetry.file.read()
poetry_content = content["tool"]["poetry"]

if group == MAIN_GROUP:
Expand Down Expand Up @@ -266,7 +266,7 @@ def handle(self) -> int:

if status == 0 and not self.option("dry-run"):
assert isinstance(content, TOMLDocument)
self.poetry.toml_file.write(content)
self.poetry.file.write(content)

return status

Expand Down
11 changes: 2 additions & 9 deletions src/poetry/console/commands/check.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from pathlib import Path

from poetry.console.commands.command import Command


Expand Down Expand Up @@ -64,13 +62,8 @@ def handle(self) -> int:
from poetry.pyproject.toml import PyProjectTOML

# Load poetry config and display errors, if any
if isinstance(self.poetry.file, Path):
toml_path = self.poetry.file
else:
# TODO: backward compatibility, can be simplified if poetry-core with
# https://github.com/python-poetry/poetry-core/pull/483 is available
toml_path = self.poetry.file.path # type: ignore[assignment]
config = PyProjectTOML(toml_path).poetry_config
poetry_file = self.poetry.file.path
config = PyProjectTOML(poetry_file).poetry_config
check_result = Factory.validate(config, strict=True)

# Validate trove classifiers
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/console/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def handle(self) -> int:
else:
group = self.option("group", self.default_group)

content: dict[str, Any] = self.poetry.toml_file.read()
content: dict[str, Any] = self.poetry.file.read()
poetry_content = content["tool"]["poetry"]

if group is None:
Expand Down Expand Up @@ -122,7 +122,7 @@ def handle(self) -> int:

if not self.option("dry-run") and status == 0:
assert isinstance(content, TOMLDocument)
self.poetry.toml_file.write(content)
self.poetry.file.write(content)

return status

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 @@ -78,7 +78,7 @@ def generate_system_pyproject(self) -> None:
content["tool"]["poetry"][key] = preserved[key] # type: ignore[index]

pyproject = PyProjectTOML(self.system_pyproject)
pyproject.file.write(content)
pyproject.toml_file.write(content)

def reset_poetry(self) -> None:
with directory(self.system_pyproject.parent):
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/console/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def handle(self) -> int:
)

if not self.option("dry-run"):
content: dict[str, Any] = self.poetry.toml_file.read()
content: dict[str, Any] = self.poetry.file.read()
poetry_content = content["tool"]["poetry"]
poetry_content["version"] = version.text

assert isinstance(content, TOMLDocument)
self.poetry.toml_file.write(content)
self.poetry.file.write(content)
else:
if self.option("short"):
self.line(self.poetry.package.pretty_version)
Expand Down
21 changes: 9 additions & 12 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
import re

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import cast
Expand All @@ -25,6 +24,7 @@

if TYPE_CHECKING:
from collections.abc import Iterable
from pathlib import Path

from cleo.io.io import IO
from poetry.core.packages.package import Package
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 @@ -81,15 +85,8 @@ def create_poetry(

config.merge({"repositories": repositories})

if isinstance(base_poetry.file, Path):
toml_path = base_poetry.file
else:
# TODO: backward compatibility, can be simplified if poetry-core with
# https://github.com/python-poetry/poetry-core/pull/483 is available
toml_path = base_poetry.file.path # type: ignore[assignment]

poetry = Poetry(
toml_path,
poetry_file,
base_poetry.local_config,
base_poetry.package,
locker,
Expand Down
16 changes: 6 additions & 10 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 Expand Up @@ -249,7 +245,7 @@ def _add_dist_info(self, added_files: list[Path]) -> None:
json.dumps(
{
"dir_info": {"editable": True},
"url": self._poetry.file.parent.absolute().as_uri(),
"url": self._poetry.file.path.parent.absolute().as_uri(),
}
)
)
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def pyproject(self) -> PyProjectTOML:
return cast("PyProjectTOML", pyproject)

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

@property
Expand Down
2 changes: 1 addition & 1 deletion tests/console/commands/self/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_self_command_dependencies(locked: bool = True) -> TOMLTable:

poetry = Factory().create_poetry(system_pyproject_file.parent, disable_plugins=True)

content = poetry.toml_file.read()["tool"]["poetry"]
content = poetry.file.read()["tool"]["poetry"]

assert "group" in content
assert SelfCommand.ADDITIONAL_PACKAGE_GROUP in content["group"]
Expand Down
Loading

0 comments on commit aa235b8

Please sign in to comment.