Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: list direct dependencies in pdm outdated by default #3219

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/3218.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
By default, `pdm outdated` will only list direct dependencies. This can be changed by adding the `--include-sub` option.
23 changes: 18 additions & 5 deletions src/pdm/cli/commands/outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import asdict, dataclass
from fnmatch import fnmatch
from itertools import zip_longest
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

from pdm.cli.commands.base import BaseCommand
from pdm.cli.utils import normalize_pattern
Expand All @@ -24,6 +24,7 @@
@dataclass
class ListPackage:
package: str
groups: list[str]
installed_version: str
pinned_version: str
latest_version: str = ""
Expand All @@ -46,6 +47,7 @@
parser.add_argument(
"--json", action="store_const", const="json", dest="format", default="table", help="Output in JSON format"
)
parser.add_argument("--include-sub", action="store_true", help="Include sub-dependencies")
parser.add_argument("patterns", nargs="*", help="The packages to check", type=normalize_pattern)

@staticmethod
Expand Down Expand Up @@ -92,21 +94,31 @@
resolved = {strip_extras(k)[0]: v for k, v in project.get_locked_repository().candidates.items()}

collected: list[ListPackage] = []
project_dependencies: dict[str, list[str]] = {}
for group, dependencies in project.all_dependencies.items():
for dep in dependencies:
project_dependencies.setdefault(cast(str, dep.key), []).append(group)

for name, distribution in installed.items():
if not self._match_pattern(name, options.patterns):
continue
if project.name and name == normalize_name(project.name):
continue
constrained_version = resolved.pop(name).version or "" if name in resolved else ""
collected.append(ListPackage(name, distribution.version or "", constrained_version))
if not options.include_sub and name not in project_dependencies:
continue
groups = project_dependencies.get(name, [])
collected.append(ListPackage(name, groups, distribution.version or "", constrained_version))

for name, candidate in resolved.items():
if not self._match_pattern(name, options.patterns):
continue
if candidate.req.marker and not candidate.req.marker.matches(environment.spec):
continue
collected.append(ListPackage(name, "", candidate.version or ""))
if not options.include_sub and name not in project_dependencies:
continue
groups = project_dependencies.get(name, [])
collected.append(ListPackage(name, groups, "", candidate.version or ""))

Check warning on line 121 in src/pdm/cli/commands/outdated.py

View check run for this annotation

Codecov / codecov/patch

src/pdm/cli/commands/outdated.py#L119-L121

Added lines #L119 - L121 were not covered by tests

with environment.get_finder() as finder, ThreadPoolExecutor() as executor:
for package in collected:
Expand All @@ -121,11 +133,12 @@
else:
rows = [
(
package.package,
f"[bold]{package.package}[/]",
", ".join(package.groups),
package.installed_version,
self._render_version(package.pinned_version, package.installed_version),
self._render_version(package.latest_version, package.installed_version),
)
for package in collected
]
project.core.ui.display_columns(rows, header=["Package", "Installed", "Pinned", "Latest"])
project.core.ui.display_columns(rows, header=["Package", "Groups", "Installed", "Pinned", "Latest"])
12 changes: 9 additions & 3 deletions tests/cli/test_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ def test_outdated(project, pdm, index):
"""

result = pdm(["outdated"], obj=project, strict=True, cleanup=False)
assert "| requests | 2.19.1 | 2.19.1 | 2.20.0 |" in result.stdout
assert "| requests | default | 2.19.1 | 2.19.1 | 2.20.0 |" in result.stdout

result = pdm(["outdated", "re*"], obj=project, strict=True, cleanup=False)
assert "| requests | 2.19.1 | 2.19.1 | 2.20.0 |" in result.stdout
assert "| requests | default | 2.19.1 | 2.19.1 | 2.20.0 |" in result.stdout

result = pdm(["outdated", "--json"], obj=project, strict=True, cleanup=False)
json_output = json.loads(result.stdout)
assert json_output == [
{"package": "requests", "installed_version": "2.19.1", "pinned_version": "2.19.1", "latest_version": "2.20.0"}
{
"package": "requests",
"groups": ["default"],
"installed_version": "2.19.1",
"pinned_version": "2.19.1",
"latest_version": "2.20.0",
}
]
Loading