Skip to content

Commit

Permalink
fix: allow no-marker option to work in pdm export command (#3152)
Browse files Browse the repository at this point in the history
* fix: allow no-marker option to work in pdm export command

Signed-off-by: Frost Ming <[email protected]>

* add test

Signed-off-by: Frost Ming <[email protected]>

* add news

Signed-off-by: Frost Ming <[email protected]>

* improve the test

Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Sep 10, 2024
1 parent cfa4913 commit 0baff50
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions news/3152.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Now when `--no-markers` is used, the exported requirements can only work on the current platform.
10 changes: 5 additions & 5 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [
"python-dotenv>=0.15",
"resolvelib>=1.0.1",
"installer<0.8,>=0.7",
"truststore; python_version >= \"3.10\"",
"truststore>=0.9; python_version >= \"3.10\"",
"tomli>=1.1.0; python_version < \"3.11\"",
"importlib-resources>=5; python_version < \"3.9\"",
"importlib-metadata>=3.6; python_version < \"3.10\"",
Expand Down
9 changes: 8 additions & 1 deletion src/pdm/cli/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
options.hashes = False
selection = GroupSelection.from_options(project, options)
if options.markers is False:
project.core.ui.deprecated("The --no-markers option is deprecated and has no effect.")
project.core.ui.warn(
"The --no-markers option is on, the exported requirements can only work on the current platform"
)
packages: Iterable[Requirement] | Iterable[Candidate]
if options.pyproject:
packages = [r for group in selection for r in project.get_dependencies(group)]
Expand All @@ -86,6 +88,7 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
groups = set(selection)
packages = []
seen_extras: set[str] = set()
this_spec = project.environment.spec
for candidate in candidates:
if groups.isdisjoint(candidate.req.groups):
continue
Expand All @@ -97,6 +100,10 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
continue
elif candidate.req.extras:
continue
if not options.markers and candidate.req.marker:
if not candidate.req.marker.matches(this_spec):
continue
candidate.req.marker = None
packages.append(candidate) # type: ignore[arg-type]

content = FORMATS[options.format].export(project, packages, options)
Expand Down
15 changes: 13 additions & 2 deletions tests/cli/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,20 @@ def test_show_update_hint(pdm, project, monkeypatch):

@pytest.mark.usefixtures("repository")
def test_export_with_platform_markers(pdm, project):
pdm(["add", "--no-sync", 'urllib3; sys_platform == "fake"'], obj=project, strict=True)
pdm(
["add", "--no-sync", 'urllib3; sys_platform == "fake"', 'idna; python_version >= "3.7"'],
obj=project,
strict=True,
)
result = pdm(["export", "--no-hashes"], obj=project, strict=True)
assert 'urllib3==1.22; sys_platform == "fake"' in result.output.splitlines()
result_lines = result.output.splitlines()
assert 'urllib3==1.22; sys_platform == "fake"' in result_lines
assert 'idna==2.7; python_version >= "3.7"' in result_lines

result = pdm(["export", "--no-hashes", "--no-markers"], obj=project, strict=True)
result_lines = result.output.splitlines()
assert not any(line.startswith("urllib3") for line in result_lines)
assert "idna==2.7" in result_lines


@pytest.mark.usefixtures("repository", "vcs")
Expand Down

0 comments on commit 0baff50

Please sign in to comment.