Skip to content

Commit

Permalink
poetry show <package> now also shows which packages depend on it (#2351)
Browse files Browse the repository at this point in the history
* Add required_by list to pip show <package> command

* Update poetry show pendulum documentation

* Handle package name discrepancies

* Lets not force typing then

* issue-1906 Add the test back after rebase

* issue-1906 Ignore trailing spaces in the tests
  • Loading branch information
snejus authored Sep 19, 2021
1 parent 61301a1 commit 3519d2b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
5 changes: 4 additions & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,13 @@ name : pendulum
version : 1.4.2
description : Python datetimes made easy

dependencies:
dependencies
- python-dateutil >=2.6.1
- tzlocal >=1.4
- pytzdata >=2017.2.2

required by
- calendar >=1.4.0
```

### Options
Expand Down
15 changes: 15 additions & 0 deletions poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ def handle(self) -> Optional[int]:

return 0

required_by = {}
for locked in locked_packages:
dependencies = {d.name: d.pretty_constraint for d in locked.requires}

if pkg.name in dependencies:
required_by[locked.pretty_name] = dependencies[pkg.name]

rows = [
["<info>name</>", " : <c1>{}</>".format(pkg.pretty_name)],
["<info>version</>", " : <b>{}</b>".format(pkg.pretty_version)],
Expand All @@ -199,6 +206,14 @@ def handle(self) -> Optional[int]:
)
)

if required_by:
self.line("")
self.line("<info>required by</info>")
for parent, requires_version in required_by.items():
self.line(
" - <c1>{}</c1> <b>{}</b>".format(parent, requires_version)
)

return 0

show_latest = self.option("latest")
Expand Down
75 changes: 75 additions & 0 deletions tests/console/commands/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,3 +1419,78 @@ def test_show_tree_no_dev(tester, poetry, installed):
"""

assert expected == tester.io.fetch_output()


def test_show_required_by_deps(tester, poetry, installed):
poetry.package.add_dependency(Factory.create_dependency("cachy", "^0.2.0"))
poetry.package.add_dependency(Factory.create_dependency("pendulum", "2.0.0"))

cachy2 = get_package("cachy", "0.2.0")
cachy2.add_dependency(Factory.create_dependency("msgpack-python", ">=0.5 <0.6"))

pendulum = get_package("pendulum", "2.0.0")
pendulum.add_dependency(Factory.create_dependency("CachY", "^0.2.0"))

installed.add_package(cachy2)
installed.add_package(pendulum)

poetry.locker.mock_lock_data(
{
"package": [
{
"name": "cachy",
"version": "0.2.0",
"description": "",
"category": "main",
"optional": False,
"platform": "*",
"python-versions": "*",
"checksum": [],
"dependencies": {"msgpack-python": ">=0.5 <0.6"},
},
{
"name": "pendulum",
"version": "2.0.0",
"description": "Pendulum package",
"category": "main",
"optional": False,
"platform": "*",
"python-versions": "*",
"checksum": [],
"dependencies": {"cachy": ">=0.2.0 <0.3.0"},
},
{
"name": "msgpack-python",
"version": "0.5.1",
"description": "",
"category": "main",
"optional": False,
"platform": "*",
"python-versions": "*",
"checksum": [],
},
],
"metadata": {
"python-versions": "*",
"platform": "*",
"content-hash": "123456789",
"hashes": {"cachy": [], "pendulum": [], "msgpack-python": []},
},
}
)

tester.execute("cachy")

expected = """\
name : cachy
version : 0.2.0
description :
dependencies
- msgpack-python >=0.5 <0.6
required by
- pendulum >=0.2.0 <0.3.0
""".splitlines()
actual = [line.rstrip() for line in tester.io.fetch_output().splitlines()]
assert actual == expected

0 comments on commit 3519d2b

Please sign in to comment.