Skip to content

Commit bffc6dc

Browse files
committed
removing tomlkit from poetry-core
1 parent b28339d commit bffc6dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+373
-120
lines changed

src/poetry/config/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from typing import Any
1212

1313
from packaging.utils import canonicalize_name
14-
from poetry.core.toml import TOMLFile
1514

1615
from poetry.config.dict_config_source import DictConfigSource
1716
from poetry.config.file_config_source import FileConfigSource
1817
from poetry.locations import CONFIG_DIR
1918
from poetry.locations import DEFAULT_CACHE_DIR
19+
from poetry.toml import TOMLFile
2020

2121

2222
if TYPE_CHECKING:

src/poetry/config/file_config_source.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
if TYPE_CHECKING:
1414
from collections.abc import Iterator
1515

16-
from poetry.core.toml.file import TOMLFile
1716
from tomlkit.toml_document import TOMLDocument
1817

18+
from poetry.toml.file import TOMLFile
19+
1920

2021
class FileConfigSource(ConfigSource):
2122
def __init__(self, file: TOMLFile, auth_config: bool = False) -> None:

src/poetry/console/commands/add.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def handle(self) -> int:
119119

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

125125
if group == MAIN_GROUP:
@@ -261,7 +261,7 @@ def handle(self) -> int:
261261

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

266266
return status
267267

src/poetry/console/commands/check.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ def validate_classifiers(
5656
return errors, warnings
5757

5858
def handle(self) -> int:
59-
from poetry.core.pyproject.toml import PyProjectTOML
60-
6159
from poetry.factory import Factory
60+
from poetry.pyproject.toml import PyProjectTOML
6261

6362
# Load poetry config and display errors, if any
6463
poetry_file = Factory.locate(Path.cwd())

src/poetry/console/commands/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ def handle(self) -> int:
129129
from pathlib import Path
130130

131131
from poetry.core.pyproject.exceptions import PyProjectException
132-
from poetry.core.toml.file import TOMLFile
133132

134133
from poetry.config.config import Config
135134
from poetry.config.file_config_source import FileConfigSource
136135
from poetry.locations import CONFIG_DIR
136+
from poetry.toml.file import TOMLFile
137137

138138
config = Config.create()
139139
config_file = TOMLFile(CONFIG_DIR / "config.toml")

src/poetry/console/commands/init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ def __init__(self) -> None:
7171
def handle(self) -> int:
7272
from pathlib import Path
7373

74-
from poetry.core.pyproject.toml import PyProjectTOML
7574
from poetry.core.vcs.git import GitConfig
7675

7776
from poetry.layouts import layout
77+
from poetry.pyproject.toml import PyProjectTOML
7878
from poetry.utils.env import SystemEnv
7979

8080
pyproject = PyProjectTOML(Path.cwd() / "pyproject.toml")

src/poetry/console/commands/remove.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def handle(self) -> int:
5151
else:
5252
group = self.option("group", self.default_group)
5353

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

5757
if group is None:
@@ -117,7 +117,7 @@ def handle(self) -> int:
117117

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

122122
return status
123123

src/poetry/console/commands/self/self_command.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
from poetry.core.packages.dependency import Dependency
77
from poetry.core.packages.project_package import ProjectPackage
8-
from poetry.core.pyproject.toml import PyProjectTOML
98

109
from poetry.__version__ import __version__
1110
from poetry.console.commands.installer_command import InstallerCommand
1211
from poetry.factory import Factory
12+
from poetry.pyproject.toml import PyProjectTOML
1313
from poetry.utils.env import EnvManager
1414
from poetry.utils.env import SystemEnv
1515
from poetry.utils.helpers import directory

src/poetry/console/commands/version.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ def handle(self) -> int:
7373
)
7474

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

8080
assert isinstance(content, TOMLDocument)
81-
self.poetry.file.write(content)
81+
self.poetry.toml_file.write(content)
8282
else:
8383
if self.option("short"):
8484
self.line(self.poetry.package.pretty_version)

src/poetry/factory.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
from poetry.core.factory import Factory as BaseFactory
1313
from poetry.core.packages.dependency_group import MAIN_GROUP
1414
from poetry.core.packages.project_package import ProjectPackage
15-
from poetry.core.toml.file import TOMLFile
1615

1716
from poetry.config.config import Config
1817
from poetry.json import validate_object
1918
from poetry.packages.locker import Locker
2019
from poetry.plugins.plugin import Plugin
2120
from poetry.plugins.plugin_manager import PluginManager
2221
from poetry.poetry import Poetry
22+
from poetry.toml.file import TOMLFile
2323

2424

2525
if TYPE_CHECKING:
@@ -81,7 +81,7 @@ def create_poetry(
8181
config.merge({"repositories": repositories})
8282

8383
poetry = Poetry(
84-
base_poetry.file.path,
84+
base_poetry.file,
8585
base_poetry.local_config,
8686
base_poetry.package,
8787
locker,

src/poetry/inspection/info.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
from poetry.core.factory import Factory
1818
from poetry.core.packages.dependency import Dependency
1919
from poetry.core.packages.package import Package
20-
from poetry.core.pyproject.toml import PyProjectTOML
2120
from poetry.core.utils.helpers import parse_requires
2221
from poetry.core.utils.helpers import temporary_directory
2322
from poetry.core.version.markers import InvalidMarker
2423

24+
from poetry.pyproject.toml import PyProjectTOML
2525
from poetry.utils.env import EnvCommandError
2626
from poetry.utils.env import ephemeral_environment
2727
from poetry.utils.setup_reader import SetupReader

src/poetry/installation/executor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
from cleo.io.null_io import NullIO
1818
from poetry.core.packages.file_dependency import FileDependency
1919
from poetry.core.packages.utils.link import Link
20-
from poetry.core.pyproject.toml import PyProjectTOML
2120

2221
from poetry.installation.chef import Chef
2322
from poetry.installation.chooser import Chooser
2423
from poetry.installation.operations import Install
2524
from poetry.installation.operations import Uninstall
2625
from poetry.installation.operations import Update
26+
from poetry.pyproject.toml import PyProjectTOML
2727
from poetry.utils._compat import decode
2828
from poetry.utils.authenticator import Authenticator
2929
from poetry.utils.env import EnvCommandError
@@ -558,7 +558,7 @@ def _install_directory(self, operation: Install | Update) -> int:
558558
package_poetry = None
559559
if pyproject.is_poetry_project():
560560
with contextlib.suppress(RuntimeError):
561-
package_poetry = Factory().create_poetry(pyproject.file.path.parent)
561+
package_poetry = Factory().create_poetry(pyproject.file.parent)
562562

563563
if package_poetry is not None:
564564
# Even if there is a build system specified

src/poetry/installation/pip_installer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def install_directory(self, package: Package) -> str | int:
226226
package_poetry = None
227227
if pyproject.is_poetry_project():
228228
with contextlib.suppress(RuntimeError):
229-
package_poetry = Factory().create_poetry(pyproject.file.path.parent)
229+
package_poetry = Factory().create_poetry(pyproject.file.parent)
230230

231231
if package_poetry is not None:
232232
# Even if there is a build system specified

src/poetry/layouts/layout.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
from typing import Any
66

77
from packaging.utils import canonicalize_name
8-
from poetry.core.pyproject.toml import PyProjectTOML
98
from poetry.core.utils.helpers import module_name
109
from tomlkit import inline_table
1110
from tomlkit import loads
1211
from tomlkit import table
1312
from tomlkit.toml_document import TOMLDocument
1413

14+
from poetry.pyproject.toml import PyProjectTOML
15+
1516

1617
if TYPE_CHECKING:
1718
from typing import Mapping

src/poetry/masonry/builders/editable.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def _add_dist_info(self, added_files: list[Path]) -> None:
249249
json.dumps(
250250
{
251251
"dir_info": {"editable": True},
252-
"url": self._poetry.file.path.parent.as_uri(),
252+
"url": self._poetry.file.parent.as_uri(),
253253
}
254254
)
255255
)

src/poetry/packages/locker.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from poetry.core.constraints.version import parse_constraint
1717
from poetry.core.packages.dependency import Dependency
1818
from poetry.core.packages.package import Package
19-
from poetry.core.toml.file import TOMLFile
2019
from poetry.core.version.markers import parse_marker
2120
from poetry.core.version.requirements import InvalidRequirement
2221
from tomlkit import array
@@ -25,6 +24,7 @@
2524
from tomlkit import inline_table
2625
from tomlkit import table
2726

27+
from poetry.toml.file import TOMLFile
2828
from poetry.utils._compat import tomllib
2929

3030

src/poetry/poetry.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
from typing import TYPE_CHECKING
44
from typing import Any
5+
from typing import cast
56

67
from poetry.core.poetry import Poetry as BasePoetry
78

89
from poetry.__version__ import __version__
910
from poetry.config.source import Source
11+
from poetry.pyproject.toml import PyProjectTOML
1012

1113

1214
if TYPE_CHECKING:
@@ -18,6 +20,7 @@
1820
from poetry.packages.locker import Locker
1921
from poetry.plugins.plugin_manager import PluginManager
2022
from poetry.repositories.repository_pool import RepositoryPool
23+
from poetry.toml import TOMLFile
2124

2225

2326
class Poetry(BasePoetry):
@@ -34,14 +37,23 @@ def __init__(
3437
) -> None:
3538
from poetry.repositories.repository_pool import RepositoryPool
3639

37-
super().__init__(file, local_config, package)
40+
super().__init__(file, local_config, package, pyproject_type=PyProjectTOML)
3841

3942
self._locker = locker
4043
self._config = config
4144
self._pool = RepositoryPool()
4245
self._plugin_manager: PluginManager | None = None
4346
self._disable_cache = disable_cache
4447

48+
@property
49+
def pyproject(self) -> PyProjectTOML:
50+
pyproject = super().pyproject
51+
return cast("PyProjectTOML", pyproject)
52+
53+
@property
54+
def toml_file(self) -> TOMLFile:
55+
return self.pyproject.toml_file
56+
4557
@property
4658
def locker(self) -> Locker:
4759
return self._locker

src/poetry/pyproject/toml.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from poetry.core.pyproject.toml import PyProjectTOML as BasePyProjectTOML
6+
from tomlkit.api import table
7+
from tomlkit.items import Table
8+
from tomlkit.toml_document import TOMLDocument
9+
10+
from poetry.toml import TOMLFile
11+
12+
13+
if TYPE_CHECKING:
14+
from pathlib import Path
15+
16+
17+
# Enhanced version of poetry-core's PyProjectTOML which is capable of writing
18+
# pyproject.toml
19+
#
20+
# The poetry-core class uses tomli to read the file, here we use tomlkit so as to
21+
# preserve comments and formatting when writing.
22+
class PyProjectTOML(BasePyProjectTOML):
23+
def __init__(self, path: str | Path) -> None:
24+
super().__init__(path)
25+
self._toml_file = TOMLFile(path=path)
26+
self._toml_document: TOMLDocument | None = None
27+
28+
@property
29+
def toml_file(self) -> TOMLFile:
30+
return self._toml_file
31+
32+
@property
33+
def data(self) -> TOMLDocument:
34+
if self._toml_document is None:
35+
if not self._file.exists():
36+
self._toml_document = TOMLDocument()
37+
else:
38+
self._toml_document = self.toml_file.read()
39+
40+
return self._toml_document
41+
42+
def save(self) -> None:
43+
data = self.data
44+
45+
if self._build_system is not None:
46+
if "build-system" not in data:
47+
data["build-system"] = table()
48+
49+
build_system = data["build-system"]
50+
assert isinstance(build_system, Table)
51+
52+
build_system["requires"] = self._build_system.requires
53+
build_system["build-backend"] = self._build_system.build_backend
54+
55+
self.toml_file.write(data=data)
56+
57+
def reload(self) -> None:
58+
self._toml_document = None
59+
self._build_system = None

src/poetry/toml/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import annotations
2+
3+
from poetry.toml.exceptions import TOMLError
4+
from poetry.toml.file import TOMLFile
5+
6+
7+
__all__ = ["TOMLError", "TOMLFile"]

src/poetry/toml/exceptions.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
from poetry.core.exceptions import PoetryCoreException
4+
from tomlkit.exceptions import TOMLKitError
5+
6+
7+
class TOMLError(TOMLKitError, PoetryCoreException):
8+
pass

0 commit comments

Comments
 (0)