From b481fe8f4cf93748ac990a07012296b947ae5736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 17 Apr 2022 14:32:50 +0200 Subject: [PATCH] dependency groups: rename implicit group "default" to "main" This is done in order to avoid ambiguity between "default group" and "default dependencies" (the groups that are considered by a command by default) --- src/poetry/core/factory.py | 10 +++++++--- src/poetry/core/packages/dependency.py | 3 ++- src/poetry/core/packages/dependency_group.py | 3 +++ src/poetry/core/packages/package.py | 9 +++++---- .../README.rst | 0 .../pyproject.toml | 2 +- .../simple_project/__init__.py | 0 tests/packages/test_package.py | 8 ++++---- tests/test_factory.py | 4 ++-- 9 files changed, 24 insertions(+), 15 deletions(-) rename tests/fixtures/{project_with_groups_and_explicit_default => project_with_groups_and_explicit_main}/README.rst (100%) rename tests/fixtures/{project_with_groups_and_explicit_default => project_with_groups_and_explicit_main}/pyproject.toml (87%) rename tests/fixtures/{project_with_groups_and_explicit_default => project_with_groups_and_explicit_main}/simple_project/__init__.py (100%) diff --git a/src/poetry/core/factory.py b/src/poetry/core/factory.py index cefc2ff9e..1415ed821 100644 --- a/src/poetry/core/factory.py +++ b/src/poetry/core/factory.py @@ -78,6 +78,8 @@ def _add_package_group_dependencies( group: str | DependencyGroup, dependencies: DependencyConfig, ) -> None: + from poetry.core.packages.dependency_group import MAIN_GROUP + if isinstance(group, str): if package.has_dependency_group(group): group = package.dependency_group(group) @@ -92,7 +94,7 @@ def _add_package_group_dependencies( ) for _constraint in _constraints: if name.lower() == "python": - if group.name == "default" and isinstance(_constraint, str): + if group.name == MAIN_GROUP and isinstance(_constraint, str): package.python_versions = _constraint continue @@ -116,6 +118,7 @@ def configure_package( with_groups: bool = True, ) -> ProjectPackage: from poetry.core.packages.dependency import Dependency + from poetry.core.packages.dependency_group import MAIN_GROUP from poetry.core.packages.dependency_group import DependencyGroup from poetry.core.spdx.helpers import license_by_id @@ -151,7 +154,7 @@ def configure_package( if "dependencies" in config: cls._add_package_group_dependencies( - package=package, group="default", dependencies=config["dependencies"] + package=package, group=MAIN_GROUP, dependencies=config["dependencies"] ) if with_groups and "group" in config: @@ -229,6 +232,7 @@ def create_dependency( parse_constraint as parse_generic_constraint, ) from poetry.core.packages.dependency import Dependency + from poetry.core.packages.dependency_group import MAIN_GROUP from poetry.core.packages.directory_dependency import DirectoryDependency from poetry.core.packages.file_dependency import FileDependency from poetry.core.packages.url_dependency import URLDependency @@ -239,7 +243,7 @@ def create_dependency( from poetry.core.version.markers import parse_marker if groups is None: - groups = ["default"] + groups = [MAIN_GROUP] if constraint is None: constraint = "*" diff --git a/src/poetry/core/packages/dependency.py b/src/poetry/core/packages/dependency.py index 7c11ca5a7..3f978690e 100644 --- a/src/poetry/core/packages/dependency.py +++ b/src/poetry/core/packages/dependency.py @@ -12,6 +12,7 @@ from poetry.core.packages.constraints import ( parse_constraint as parse_generic_constraint, ) +from poetry.core.packages.dependency_group import MAIN_GROUP from poetry.core.packages.specification import PackageSpecification from poetry.core.semver.helpers import parse_constraint from poetry.core.semver.version_range_constraint import VersionRangeConstraint @@ -62,7 +63,7 @@ def __init__( self._optional = optional if not groups: - groups = ["default"] + groups = [MAIN_GROUP] self._groups = frozenset(groups) diff --git a/src/poetry/core/packages/dependency_group.py b/src/poetry/core/packages/dependency_group.py index 5839c109b..a70c90783 100644 --- a/src/poetry/core/packages/dependency_group.py +++ b/src/poetry/core/packages/dependency_group.py @@ -7,6 +7,9 @@ from poetry.core.packages.types import DependencyTypes +MAIN_GROUP = "main" + + class DependencyGroup: def __init__(self, name: str, optional: bool = False) -> None: self._name: str = name diff --git a/src/poetry/core/packages/package.py b/src/poetry/core/packages/package.py index 12f35b2ee..09edfee92 100644 --- a/src/poetry/core/packages/package.py +++ b/src/poetry/core/packages/package.py @@ -9,6 +9,7 @@ from typing import Collection from typing import Iterable +from poetry.core.packages.dependency_group import MAIN_GROUP from poetry.core.packages.specification import PackageSpecification from poetry.core.packages.utils.utils import create_nested_marker from poetry.core.semver.helpers import parse_constraint @@ -185,19 +186,19 @@ def maintainer_email(self) -> str: @property def requires(self) -> list[DependencyTypes]: """ - Returns the default dependencies + Returns the main dependencies """ - if not self._dependency_groups or "default" not in self._dependency_groups: + if not self._dependency_groups or MAIN_GROUP not in self._dependency_groups: return [] - return self._dependency_groups["default"].dependencies + return self._dependency_groups[MAIN_GROUP].dependencies @property def all_requires( self, ) -> list[DependencyTypes]: """ - Returns the default dependencies and group dependencies. + Returns the main dependencies and group dependencies. """ return [ dependency diff --git a/tests/fixtures/project_with_groups_and_explicit_default/README.rst b/tests/fixtures/project_with_groups_and_explicit_main/README.rst similarity index 100% rename from tests/fixtures/project_with_groups_and_explicit_default/README.rst rename to tests/fixtures/project_with_groups_and_explicit_main/README.rst diff --git a/tests/fixtures/project_with_groups_and_explicit_default/pyproject.toml b/tests/fixtures/project_with_groups_and_explicit_main/pyproject.toml similarity index 87% rename from tests/fixtures/project_with_groups_and_explicit_default/pyproject.toml rename to tests/fixtures/project_with_groups_and_explicit_main/pyproject.toml index b7c02d70f..18c6a5951 100644 --- a/tests/fixtures/project_with_groups_and_explicit_default/pyproject.toml +++ b/tests/fixtures/project_with_groups_and_explicit_main/pyproject.toml @@ -7,7 +7,7 @@ authors = ["Your Name "] [tool.poetry.dependencies] python = "^3.7" -[tool.poetry.group.default.dependencies] +[tool.poetry.group.main.dependencies] aiohttp = "^2.17.0" [tools.poetry] diff --git a/tests/fixtures/project_with_groups_and_explicit_default/simple_project/__init__.py b/tests/fixtures/project_with_groups_and_explicit_main/simple_project/__init__.py similarity index 100% rename from tests/fixtures/project_with_groups_and_explicit_default/simple_project/__init__.py rename to tests/fixtures/project_with_groups_and_explicit_main/simple_project/__init__.py diff --git a/tests/packages/test_package.py b/tests/packages/test_package.py index 297c317e0..4dd21e988 100644 --- a/tests/packages/test_package.py +++ b/tests/packages/test_package.py @@ -51,7 +51,7 @@ def test_package_authors_invalid(): ) -@pytest.mark.parametrize("groups", [["default"], ["dev"]]) +@pytest.mark.parametrize("groups", [["main"], ["dev"]]) def test_package_add_dependency_vcs_groups(groups: list[str], f: Factory): package = Package("foo", "0.1.0") @@ -73,10 +73,10 @@ def test_package_add_dependency_vcs_groups_default_main(f: Factory): "poetry", {"git": "https://github.com/python-poetry/poetry.git"} ) ) - assert dependency.groups == frozenset(["default"]) + assert dependency.groups == frozenset(["main"]) -@pytest.mark.parametrize("groups", [["default"], ["dev"]]) +@pytest.mark.parametrize("groups", [["main"], ["dev"]]) @pytest.mark.parametrize("optional", [True, False]) def test_package_url_groups_optional(groups: list[str], optional: bool, f: Factory): package = Package("foo", "0.1.0") @@ -405,7 +405,7 @@ def test_only_with_dependency_groups(package_with_groups: Package): assert len(package.requires) == 0 assert len(package.all_requires) == 2 - package = package_with_groups.with_dependency_groups(["default"], only=True) + package = package_with_groups.with_dependency_groups(["main"], only=True) assert len(package.requires) == 2 assert len(package.all_requires) == 2 diff --git a/tests/test_factory.py b/tests/test_factory.py index 4834a8e44..f2fea981a 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -249,9 +249,9 @@ def test_create_poetry_with_groups_and_legacy_dev(): assert {dependency.name for dependency in dependencies} == {"pytest", "pre-commit"} -def test_create_poetry_with_groups_and_explicit_default(): +def test_create_poetry_with_groups_and_explicit_main(): poetry = Factory().create_poetry( - fixtures_dir / "project_with_groups_and_explicit_default" + fixtures_dir / "project_with_groups_and_explicit_main" ) package = poetry.package