Skip to content

Commit

Permalink
Merge branch 'main' into fix-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus authored Jan 6, 2025
2 parents ce42931 + 4df8d63 commit c4d0512
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 38 deletions.
25 changes: 14 additions & 11 deletions src/poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ def activated_groups(self) -> set[str]:
def _alternative_sync_command(self) -> str:
return "poetry sync"

@property
def _with_synchronization(self) -> bool:
with_synchronization = self.option("sync")
if with_synchronization:
self.line_error(
"<warning>The `<fg=yellow;options=bold>--sync</>` option is"
" deprecated and slated for removal in the next minor release"
" after June 2025, use the"
f" `<fg=yellow;options=bold>{self._alternative_sync_command}</>`"
" command instead.</warning>"
)
return bool(with_synchronization)

def handle(self) -> int:
from poetry.core.masonry.utils.module import ModuleOrPackageNotFoundError

Expand Down Expand Up @@ -150,20 +163,10 @@ def handle(self) -> int:

self.installer.extras(extras)

with_synchronization = self.option("sync")
if with_synchronization:
self.line_error(
"<warning>The `<fg=yellow;options=bold>--sync</>` option is"
" deprecated and slated for removal in the next minor release"
" after June 2025, use the"
f" `<fg=yellow;options=bold>{self._alternative_sync_command}</>`"
" command instead.</warning>"
)

self.installer.only_groups(self.activated_groups)
self.installer.skip_directory(self.option("no-directory"))
self.installer.dry_run(self.option("dry-run"))
self.installer.requires_synchronization(with_synchronization)
self.installer.requires_synchronization(self._with_synchronization)
self.installer.executor.enable_bytecode_compilation(self.option("compile"))
self.installer.verbose(self.io.is_verbose())

Expand Down
4 changes: 4 additions & 0 deletions src/poetry/console/commands/self/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ class SelfSyncCommand(SelfInstallCommand):
You can add more packages using the <c1>self add</c1> command and remove them using \
the <c1>self remove</c1> command.
"""

@property
def _with_synchronization(self) -> bool:
return True
4 changes: 4 additions & 0 deletions src/poetry/console/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ class SyncCommand(InstallCommand):
If you want to use Poetry only for dependency management but not for packaging,
you can set the "package-mode" to false in your pyproject.toml file.
"""

@property
def _with_synchronization(self) -> bool:
return True
2 changes: 0 additions & 2 deletions src/poetry/utils/env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from poetry.utils.env.script_strings import GET_ENV_PATH_ONELINER
from poetry.utils.env.script_strings import GET_ENVIRONMENT_INFO
from poetry.utils.env.script_strings import GET_PATHS
from poetry.utils.env.script_strings import GET_PATHS_FOR_GENERIC_ENVS
from poetry.utils.env.script_strings import GET_PYTHON_VERSION_ONELINER
from poetry.utils.env.script_strings import GET_SYS_PATH
from poetry.utils.env.site_packages import SitePackages
Expand Down Expand Up @@ -97,7 +96,6 @@ def build_environment(
"GET_SYS_PATH",
"GET_ENV_PATH_ONELINER",
"GET_PYTHON_VERSION_ONELINER",
"GET_PATHS_FOR_GENERIC_ENVS",
"EnvError",
"EnvCommandError",
"IncorrectEnvError",
Expand Down
13 changes: 8 additions & 5 deletions src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,10 @@ def os(self) -> str:
@property
def site_packages(self) -> SitePackages:
if self._site_packages is None:
# we disable write checks if no user site exist
fallbacks = [self.usersite] if self.usersite else []
self._site_packages = SitePackages(
self.purelib,
self.platlib,
fallbacks,
skip_write_checks=not fallbacks,
self.fallbacks,
)
return self._site_packages

Expand Down Expand Up @@ -214,8 +211,14 @@ def platlib(self) -> Path:

return self._platlib

@cached_property
def fallbacks(self) -> list[Path]:
paths = [Path(path) for path in self.paths.get("fallbacks", [])]
paths += [self.usersite] if self.usersite else []
return paths

def _get_lib_dirs(self) -> list[Path]:
return [self.purelib, self.platlib]
return [self.purelib, self.platlib, *self.fallbacks]

def is_path_relative_to_lib(self, path: Path) -> bool:
for lib_path in self._get_lib_dirs():
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/utils/env/generic_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import TYPE_CHECKING
from typing import Any

from poetry.utils.env.script_strings import GET_PATHS_FOR_GENERIC_ENVS
from poetry.utils.env.script_strings import GET_PATHS
from poetry.utils.env.virtual_env import VirtualEnv


Expand Down Expand Up @@ -78,7 +78,7 @@ def find_executables(self) -> None:
self._pip_executable = pip_executable

def get_paths(self) -> dict[str, str]:
output = self.run_python_script(GET_PATHS_FOR_GENERIC_ENVS)
output = self.run_python_script(GET_PATHS)

paths: dict[str, str] = json.loads(output)
return paths
Expand Down
12 changes: 5 additions & 7 deletions src/poetry/utils/env/script_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,16 @@ def _version_nodot(version):

GET_PATHS = """\
import json
import sysconfig
print(json.dumps(sysconfig.get_paths()))
"""

GET_PATHS_FOR_GENERIC_ENVS = """\
import json
import site
import sysconfig
paths = sysconfig.get_paths().copy()
paths["fallbacks"] = [
p for p in site.getsitepackages()
if p and p not in {paths.get("purelib"), paths.get("platlib")}
]
if site.check_enableusersite():
paths["usersite"] = site.getusersitepackages()
paths["userbase"] = site.getuserbase()
Expand Down
3 changes: 1 addition & 2 deletions src/poetry/utils/env/site_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def __init__(
purelib: Path,
platlib: Path | None = None,
fallbacks: list[Path] | None = None,
skip_write_checks: bool = False,
) -> None:
self._purelib = purelib
self._platlib = platlib or purelib
Expand All @@ -40,7 +39,7 @@ def __init__(
if path not in self._candidates:
self._candidates.append(path)

self._writable_candidates = None if not skip_write_checks else self._candidates
self._writable_candidates: list[Path] | None = None

@property
def path(self) -> Path:
Expand Down
11 changes: 3 additions & 8 deletions tests/console/commands/self/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,9 @@ def test_self_install(

tester.execute()

expected_output = """\
Updating dependencies
Resolving dependencies...
Writing lock file
"""

assert tester.io.fetch_output() == expected_output
output = tester.io.fetch_output()
assert output.startswith("Updating dependencies")
assert output.endswith("Writing lock file\n")
assert tester.io.fetch_error() == ""


Expand Down
15 changes: 15 additions & 0 deletions tests/console/commands/self/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

from cleo.exceptions import CleoNoSuchOptionError

from poetry.console.commands.self.sync import SelfSyncCommand

# import all tests from the self install command
# and run them for sync by overriding the command fixture
from tests.console.commands.self.test_install import * # noqa: F403


if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture


@pytest.fixture # type: ignore[no-redef]
Expand All @@ -28,3 +31,15 @@ def test_sync_deprecation() -> None:
def test_sync_option_not_available(tester: CommandTester) -> None:
with pytest.raises(CleoNoSuchOptionError):
tester.execute("--sync")


def test_synced_installer(tester: CommandTester, mocker: MockerFixture) -> None:
assert isinstance(tester.command, SelfSyncCommand)
mock = mocker.patch(
"poetry.console.commands.install.InstallCommand.installer",
new_callable=mocker.PropertyMock,
)

tester.execute()

mock.return_value.requires_synchronization.assert_called_with(True)
15 changes: 15 additions & 0 deletions tests/console/commands/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

from cleo.exceptions import CleoNoSuchOptionError

from poetry.console.commands.sync import SyncCommand

# import all tests from the install command
# and run them for sync by overriding the command fixture
from tests.console.commands.test_install import * # noqa: F403


if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture


@pytest.fixture # type: ignore[no-redef]
Expand All @@ -28,3 +31,15 @@ def test_sync_option_is_passed_to_the_installer() -> None:
def test_sync_option_not_available(tester: CommandTester) -> None:
with pytest.raises(CleoNoSuchOptionError):
tester.execute("--sync")


def test_synced_installer(tester: CommandTester, mocker: MockerFixture) -> None:
assert isinstance(tester.command, SyncCommand)
mock = mocker.patch(
"poetry.console.commands.install.InstallCommand.installer",
new_callable=mocker.PropertyMock,
)

tester.execute()

mock.return_value.requires_synchronization.assert_called_with(True)
2 changes: 1 addition & 1 deletion tests/utils/env/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def test_env_system_packages_are_relative_to_lib(

# These are the virtual environments' base env packages,
# in this case the system site packages.
for dist in metadata.distributions(path=[str(env.parent_env.site_packages.path)]):
for dist in env.parent_env.site_packages.distributions():
assert (
env.is_path_relative_to_lib(
Path(str(dist._path)) # type: ignore[attr-defined]
Expand Down

0 comments on commit c4d0512

Please sign in to comment.