Skip to content

Commit

Permalink
dependency groups: rename implicit group "default" to "main"
Browse files Browse the repository at this point in the history
This is done in order to avoid ambiguity between "default group" and "default dependencies" (the groups that are considered by a command by default)
  • Loading branch information
radoering committed Apr 18, 2022
1 parent a6438da commit b481fe8
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 15 deletions.
10 changes: 7 additions & 3 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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 = "*"
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,7 @@ def __init__(
self._optional = optional

if not groups:
groups = ["default"]
groups = [MAIN_GROUP]

self._groups = frozenset(groups)

Expand Down
3 changes: 3 additions & 0 deletions src/poetry/core/packages/dependency_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Your Name <you@example.com>"]
[tool.poetry.dependencies]
python = "^3.7"

[tool.poetry.group.default.dependencies]
[tool.poetry.group.main.dependencies]
aiohttp = "^2.17.0"

[tools.poetry]
Expand Down
8 changes: 4 additions & 4 deletions tests/packages/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b481fe8

Please sign in to comment.