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

Add --required-by option to list command #10036

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/10036.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``required-by`` option to the ``list`` command, in order to restrict the listed
packages to some package requirements (useful for eg. with ``--outdated``)
22 changes: 22 additions & 0 deletions src/pip/_internal/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def add_options(self):
"installed packages.",
)

self.cmd_opts.add_option(
'--required-by',
action='store',
dest='required_by',
help="List packages that are dependencies the given package.",
)

self.cmd_opts.add_option(
'--exclude-editable',
action='store_false',
Expand Down Expand Up @@ -161,6 +168,9 @@ def run(self, options, args):
if options.not_required:
packages = self.get_not_required(packages, options)

if options.required_by:
packages = self.get_required_by(packages, options)

if options.outdated:
packages = self.get_outdated(packages, options)
elif options.uptodate:
Expand Down Expand Up @@ -194,6 +204,18 @@ def get_not_required(self, packages, options):
# get_uptodate
return list({pkg for pkg in packages if pkg.key not in dep_keys})

def get_required_by(self, packages, options):
# type: (List[Distribution], Values) -> List[Distribution]
dep_keys = set() # type: Set[Distribution]
for dist in packages:
if dist.project_name == options.required_by:
dep_keys = set(requirement.key for requirement in dist.requires())

# Create a set to remove duplicate packages, and cast it to a list
# to keep the return type consistent with get_outdated and
# get_uptodate
return list({pkg for pkg in packages if pkg.key in dep_keys})

def iter_packages_latest_infos(self, packages, options):
# type: (List[Distribution], Values) -> Iterator[Distribution]
with self._build_session(options) as session:
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@ def test_outdated_not_required_flag(script, data):
assert [] == json.loads(result.stdout)


def test_required_by_flag(script, data):
"""
test the behavior of --required-by flag in the list command
"""
script.pip(
'install', '-f', data.find_links, '--no-index', 'require_simple==1.0'
)
result = script.pip(
'list', '-f', data.find_links, '--no-index',
'--required-by', 'require-simple', '--format=json',
)
assert [{'name': 'simple', 'version': '3.0'}] == json.loads(result.stdout)


def test_outdated_pre(script, data):
script.pip('install', '-f', data.find_links, '--no-index', 'simple==1.0')

Expand Down