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

Filter dependencies to only include those from a given package #2130

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
METADATA_FILENAMES = frozenset({"setup.py", "setup.cfg", "pyproject.toml"})


def _filter_dependencies(dependencies, only_from_packages):
"""
Filter dependencies to include only those required by the specified packages.
"""
filtered_dependencies = [
dep
for dep in dependencies
if dep.comes_from and dep.comes_from in only_from_packages
]
return set(filtered_dependencies)


def _determine_linesep(
strategy: str = "preserve", filenames: tuple[str, ...] = ()
) -> str:
Expand Down Expand Up @@ -101,6 +113,7 @@ def _determine_linesep(
@options.upgrade
@options.upgrade_package
@options.output_file
@options.only_from
@options.newline
@options.allow_unsafe
@options.strip_extras
Expand Down Expand Up @@ -146,6 +159,7 @@ def cli(
upgrade: bool,
upgrade_packages: tuple[str, ...],
output_file: LazyFile | IO[Any] | None,
only_from: list[str] | None,
newline: str,
allow_unsafe: bool,
strip_extras: bool | None,
Expand Down Expand Up @@ -505,6 +519,9 @@ def cli(
"or use --no-strip-extras to retain the existing behavior."
)

if only_from:
results = _filter_dependencies(results, only_from_packages=only_from)

##
# Output
##
Expand Down
6 changes: 6 additions & 0 deletions piptools/scripts/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ def _get_default_option(option_name: str) -> Any:
),
)

only_from = click.option(
"--only-from",
multiple=True,
help="Only include dependencies from given packages; may be used more than once",
)

newline = click.option(
"--newline",
type=click.Choice(("LF", "CRLF", "native", "preserve"), case_sensitive=False),
Expand Down