Skip to content

Commit

Permalink
canonicalized extras names
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Sep 30, 2022
1 parent db9647e commit 75f7a38
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from cleo.helpers import option
from packaging.utils import canonicalize_name
from poetry.console.commands.group_command import GroupCommand
from poetry.core.packages.dependency_group import MAIN_GROUP

Expand Down Expand Up @@ -84,9 +85,13 @@ def handle(self) -> None:

# Checking extras
extras = {
extra for extra_opt in self.option("extras") for extra in extra_opt.split()
canonicalize_name(extra)
for extra_opt in self.option("extras")
for extra in extra_opt.split()
}
invalid_extras = extras - {
canonicalize_name(extra) for extra in self.poetry.package.extras.keys()
}
invalid_extras = extras - self.poetry.package.extras.keys()
if invalid_extras:
raise ValueError(
f"Extra [{', '.join(sorted(invalid_extras))}] is not specified."
Expand Down
5 changes: 3 additions & 2 deletions src/poetry_plugin_export/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
if TYPE_CHECKING:
from pathlib import Path

from packaging.utils import NormalizedName
from poetry.poetry import Poetry


Expand All @@ -33,14 +34,14 @@ def __init__(self, poetry: Poetry) -> None:
self._with_hashes = True
self._with_credentials = False
self._with_urls = True
self._extras: bool | list[str] | None = []
self._extras: bool | list[NormalizedName] | None = []
self._groups: Iterable[str] = [MAIN_GROUP]

@classmethod
def is_format_supported(cls, fmt: str) -> bool:
return fmt in cls.EXPORT_METHODS

def with_extras(self, extras: bool | list[str] | None) -> Exporter:
def with_extras(self, extras: bool | list[NormalizedName] | None) -> Exporter:
self._extras = extras

return self
Expand Down
12 changes: 10 additions & 2 deletions src/poetry_plugin_export/walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING

from packaging.utils import canonicalize_name
from poetry.core.semver.util import constraint_regions
from poetry.core.version.markers import AnyMarker
from poetry.core.version.markers import SingleMarker
Expand All @@ -14,6 +15,7 @@
from collections.abc import Iterator
from collections.abc import Sequence

from packaging.utils import NormalizedName
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package
from poetry.core.version.markers import BaseMarker
Expand Down Expand Up @@ -51,7 +53,7 @@ def get_project_dependency_packages(
locker: Locker,
project_requires: list[Dependency],
project_python_marker: BaseMarker | None = None,
extras: bool | Sequence[str] | None = None,
extras: bool | Sequence[NormalizedName] | None = None,
) -> Iterator[DependencyPackage]:
# Apply the project python marker to all requirements.
if project_python_marker is not None:
Expand All @@ -68,10 +70,16 @@ def get_project_dependency_packages(
extra_package_names: set[str] | None = None

if extras is not True:
locked_extras = {
canonicalize_name(extra): [
canonicalize_name(dependency) for dependency in dependencies
]
for extra, dependencies in locker.lock_data.get("extras", {}).items()
}
extra_package_names = set(
get_extra_package_names(
repository.packages,
locker.lock_data.get("extras", {}),
locked_extras, # type: ignore[arg-type]
extras or (),
)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/command/test_command_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_export_includes_extras_by_flag(
def test_export_reports_invalid_extras(tester: CommandTester, do_lock: None) -> None:
with pytest.raises(ValueError) as error:
tester.execute("--format requirements.txt --extras 'SUS AMONGUS'")
expected = "Extra [AMONGUS, SUS] is not specified."
expected = "Extra [amongus, sus] is not specified."
assert str(error.value) == expected


Expand Down
5 changes: 3 additions & 2 deletions tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@


if TYPE_CHECKING:
from packaging.utils import NormalizedName
from poetry.poetry import Poetry
from pytest_mock import MockerFixture

Expand Down Expand Up @@ -920,7 +921,7 @@ def test_exporter_exports_requirements_txt_without_optional_packages(
],
),
(
["feature_bar"],
["feature-bar"],
[
f"bar==4.5.6 ; {MARKER_PY}",
f"foo==1.2.3 ; {MARKER_PY}",
Expand All @@ -932,7 +933,7 @@ def test_exporter_exports_requirements_txt_without_optional_packages(
def test_exporter_exports_requirements_txt_with_optional_packages(
tmp_dir: str,
poetry: Poetry,
extras: bool | list[str] | None,
extras: bool | list[NormalizedName] | None,
lines: list[str],
) -> None:
poetry.locker.mock_lock_data( # type: ignore[attr-defined]
Expand Down

0 comments on commit 75f7a38

Please sign in to comment.