Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/poetry/config/file_config_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def get_property(self, key: str | Sequence[str]) -> Any:
config = config[sub_key]

def add_property(self, key: str | Sequence[str], value: Any) -> None:
with self.secure() as toml:
config: dict[str, Any] = toml
with self.secure() as config:
keys = split_key(key)

for i, sub_key in enumerate(keys):
Expand All @@ -63,8 +62,7 @@ def add_property(self, key: str | Sequence[str], value: Any) -> None:
config = config[sub_key]

def remove_property(self, key: str | Sequence[str]) -> None:
with self.secure() as toml:
config: dict[str, Any] = toml
with self.secure() as config:
keys = split_key(key)

# Descend to the leaf, recording the (parent, key) at each step.
Expand Down
10 changes: 3 additions & 7 deletions src/poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from packaging.utils import canonicalize_name
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import MAIN_GROUP
from tomlkit.toml_document import TOMLDocument

from poetry.console.commands.init import InitCommand
from poetry.console.commands.installer_command import InstallerCommand
Expand Down Expand Up @@ -148,9 +147,7 @@ def handle(self) -> int:
if optional and group != MAIN_GROUP:
raise ValueError("You can only add optional dependencies to the main group")

# tomlkit types are awkward to work with, treat content as a mostly untyped
# dictionary.
content: dict[str, Any] = self.poetry.file.read()
content = self.poetry.file.read()
project_content = content.get("project", table())
poetry_content = content.get("tool", {}).get("poetry", table())
groups_content = content.get("dependency-groups", {})
Expand Down Expand Up @@ -244,7 +241,7 @@ def handle(self) -> int:
assert isinstance(version, str)
parse_constraint(version)

constraint: dict[str, Any] = inline_table()
constraint = inline_table()
for key, value in _constraint.items():
if key == "name":
continue
Expand Down Expand Up @@ -331,7 +328,7 @@ def handle(self) -> int:

# create a second constraint for tool.poetry.dependencies with keys
# that cannot be stored in the project section
poetry_constraint: dict[str, Any] = inline_table()
poetry_constraint = inline_table()
if not isinstance(constraint, str):
for key in ["allow-prereleases", "develop", "source"]:
if value := constraint.get(key):
Expand Down Expand Up @@ -413,7 +410,6 @@ def handle(self) -> int:
status = self.installer.run()

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

return status
Expand Down
4 changes: 1 addition & 3 deletions src/poetry/console/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from packaging.utils import canonicalize_name
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import MAIN_GROUP
from tomlkit.toml_document import TOMLDocument

from poetry.console.commands.installer_command import InstallerCommand

Expand Down Expand Up @@ -63,7 +62,7 @@ def handle(self) -> int:
else:
group = self.option("group", self.default_group)

content: dict[str, Any] = self.poetry.file.read()
content = self.poetry.file.read()
project_content = content.get("project", {})
groups_content = content.get("dependency-groups", {})
poetry_content = content.get("tool", {}).get("poetry", {})
Expand Down Expand Up @@ -179,7 +178,6 @@ def handle(self) -> int:
status = self.installer.run()

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

return status
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/console/commands/self/self_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ def generate_system_pyproject(self) -> None:
package.python_versions = ".".join(str(v) for v in self.env.version_info[:3])

content = Factory.create_legacy_pyproject_from_package(package=package)
content["tool"]["poetry"]["package-mode"] = False # type: ignore[index]
content["tool"]["poetry"]["package-mode"] = False

for key in preserved:
content["tool"]["poetry"][key] = preserved[key] # type: ignore[index]
content["tool"]["poetry"][key] = preserved[key]

if preserved_groups:
content["dependency-groups"] = preserved_groups
Expand Down
5 changes: 1 addition & 4 deletions src/poetry/console/commands/source/add.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import Any
from typing import ClassVar

from cleo.helpers import argument
Expand Down Expand Up @@ -106,9 +105,7 @@ def handle(self) -> int:
)
return 1

# tomlkit types are awkward to work with, treat content as a mostly untyped
# dictionary.
content: dict[str, Any] = self.poetry.pyproject.data
content = self.poetry.pyproject.data
if "tool" not in content:
content["tool"] = table()
if "poetry" not in content["tool"]:
Expand Down
5 changes: 1 addition & 4 deletions src/poetry/console/commands/version.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import Any
from typing import ClassVar

from cleo.helpers import argument
from cleo.helpers import option
from poetry.core.version.exceptions import InvalidVersionError
from tomlkit.toml_document import TOMLDocument

from poetry.console.commands.command import Command

Expand Down Expand Up @@ -68,15 +66,14 @@ def handle(self) -> int:
)

if not self.option("dry-run"):
content: dict[str, Any] = self.poetry.file.read()
content = self.poetry.file.read()
project_content = content.get("project", {})
if "version" in project_content:
project_content["version"] = version.text
poetry_content = content.get("tool", {}).get("poetry", {})
if "version" in poetry_content:
poetry_content["version"] = version.text

assert isinstance(content, TOMLDocument)
self.poetry.file.write(content)
else:
if self.option("short"):
Expand Down
5 changes: 1 addition & 4 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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

from cleo.io.null_io import NullIO
from packaging.utils import NormalizedName
Expand Down Expand Up @@ -254,7 +253,7 @@ def create_legacy_pyproject_from_package(cls, package: Package) -> TOMLDocument:

from poetry.utils.dependency_specification import dependency_to_specification

pyproject: dict[str, Any] = tomlkit.document()
pyproject = tomlkit.document()

pyproject["tool"] = tomlkit.table(is_super_table=True)

Expand Down Expand Up @@ -350,8 +349,6 @@ def create_legacy_pyproject_from_package(cls, package: Package) -> TOMLDocument:
if extras_section:
content["extras"] = extras_section

pyproject = cast("TOMLDocument", pyproject)

return pyproject

@classmethod
Expand Down
5 changes: 2 additions & 3 deletions src/poetry/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from tomlkit import inline_table
from tomlkit import loads
from tomlkit import table
from tomlkit.toml_document import TOMLDocument

from poetry.factory import Factory
from poetry.pyproject.toml import PyProjectTOML
Expand All @@ -23,6 +22,7 @@
from collections.abc import Mapping

from tomlkit.items import InlineTable
from tomlkit.toml_document import TOMLDocument


POETRY_DEFAULT = """\
Expand Down Expand Up @@ -141,7 +141,7 @@ def generate_project_content(
) -> TOMLDocument:
template = POETRY_DEFAULT

content: dict[str, Any] = loads(template)
content = loads(template)

project_content = content["project"]
project_content["name"] = self._project
Expand Down Expand Up @@ -212,7 +212,6 @@ def generate_project_content(
build_system.add("requires", ["poetry-core" + build_system_version])
build_system.add("build-backend", "poetry.core.masonry.api")

assert isinstance(content, TOMLDocument)
content.add("build-system", build_system)

return content
Expand Down
5 changes: 1 addition & 4 deletions src/poetry/pyproject/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ 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"]
build_system = data.setdefault("build-system", table())
assert isinstance(build_system, Table)

build_system["requires"] = self._build_system.requires
Expand Down
4 changes: 2 additions & 2 deletions tests/config/test_file_config_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_file_config_source_add_property_with_list_keys(tmp_path: Path) -> None:
)
data = config_source._file.read()
repos = data["repositories"]
assert repos["my.repo"]["url"] == "https://example.com/simple/" # type: ignore[index]
assert repos["my.repo"]["url"] == "https://example.com/simple/"


def test_file_config_source_get_property_with_list_keys(tmp_path: Path) -> None:
Expand Down Expand Up @@ -141,4 +141,4 @@ def test_file_config_source_remove_property_with_list_keys(
repos = data.get("repositories", {})
assert "my.repo" not in repos
other = data["repositories"]
assert other["other"]["url"] == "https://other.com/simple/" # type: ignore[index]
assert other["other"]["url"] == "https://other.com/simple/"
3 changes: 1 addition & 2 deletions tests/console/commands/env/test_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any

import pytest
import tomlkit
Expand Down Expand Up @@ -88,7 +87,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
)

assert envs_file.exists()
envs: dict[str, Any] = envs_file.read()
envs = envs_file.read()
assert envs[venv_name]["minor"] == "3.7"
assert envs[venv_name]["patch"] == "3.7.1"

Expand Down
4 changes: 2 additions & 2 deletions tests/console/commands/self/test_remove_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def install_plugin(installed: Repository) -> None:

content = Factory.create_legacy_pyproject_from_package(package)
content["dependency-groups"] = tomlkit.table()
content["dependency-groups"][SelfCommand.ADDITIONAL_PACKAGE_GROUP] = tomlkit.array( # type: ignore[index]
content["dependency-groups"][SelfCommand.ADDITIONAL_PACKAGE_GROUP] = tomlkit.array(
"[\n]"
)
content["dependency-groups"][SelfCommand.ADDITIONAL_PACKAGE_GROUP].append( # type: ignore[index, union-attr, call-arg]
content["dependency-groups"][SelfCommand.ADDITIONAL_PACKAGE_GROUP].append(
Dependency(plugin.name, "^1.2.3").to_pep_508()
)

Expand Down
3 changes: 1 addition & 2 deletions tests/console/commands/self/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from pathlib import Path
from typing import Any

from tomlkit.items import Array

Expand All @@ -22,7 +21,7 @@ def get_self_command_dependencies(locked: bool = True) -> Array | None:

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

pyproject: dict[str, Any] = poetry.file.read()
pyproject = poetry.file.read()
content = pyproject.get("dependency-groups", {})

if SelfCommand.ADDITIONAL_PACKAGE_GROUP not in content:
Expand Down
Loading
Loading