Skip to content

Commit 953139d

Browse files
committed
Remove redundant code paths (2/2)
We are now constrained on `pip >= 21.2.4` (bcfd93c). This commit elliminates code paths that were there previously in order to cater to the older versions of Pip. - `search_packages_info` now always gives us a generator that yields `pip._internal.commands.show._PackageInfo` objects (not `dict`). See 2c12ce5 for the rest of the changes
1 parent 8e7b033 commit 953139d

File tree

6 files changed

+56
-52
lines changed

6 files changed

+56
-52
lines changed

pip_check_reqs/find_extra_reqs.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,21 @@ def find_extra_reqs(options, requirements_filename):
2727
)
2828

2929
for package in search_packages_info(all_pkgs):
30-
if isinstance(package, dict): # pragma: no cover
31-
package_name = package['name']
32-
package_location = package['location']
33-
package_files = package.get('files', []) or []
34-
else: # pragma: no cover
35-
package_name = package.name
36-
package_location = package.location
37-
package_files = []
38-
for item in (package.files or []):
39-
here = pathlib.Path('.').resolve()
40-
item_location_rel = (pathlib.Path(package_location) / item)
41-
item_location = item_location_rel.resolve()
42-
try:
43-
relative_item_location = item_location.relative_to(here)
44-
except ValueError:
45-
# Ideally we would use Pathlib.is_relative_to rather than
46-
# checking for a ValueError, but that is only available in
47-
# Python 3.9+.
48-
relative_item_location = item_location
49-
package_files.append(str(relative_item_location))
30+
package_name = package.name
31+
package_location = package.location
32+
package_files = []
33+
for item in (package.files or []):
34+
here = pathlib.Path('.').resolve()
35+
item_location_rel = (pathlib.Path(package_location) / item)
36+
item_location = item_location_rel.resolve()
37+
try:
38+
relative_item_location = item_location.relative_to(here)
39+
except ValueError:
40+
# Ideally we would use Pathlib.is_relative_to rather than
41+
# checking for a ValueError, but that is only available in
42+
# Python 3.9+.
43+
relative_item_location = item_location
44+
package_files.append(str(relative_item_location))
5045

5146
log.debug('installed package: %s (at %s)', package_name,
5247
package_location)

pip_check_reqs/find_missing_reqs.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,21 @@ def find_missing_reqs(options, requirements_filename):
3131
)
3232

3333
for package in search_packages_info(all_pkgs):
34-
if isinstance(package, dict): # pragma: no cover
35-
package_name = package['name']
36-
package_location = package['location']
37-
package_files = package.get('files', []) or []
38-
else: # pragma: no cover
39-
package_name = package.name
40-
package_location = package.location
41-
package_files = []
42-
for item in (package.files or []):
43-
here = pathlib.Path('.').resolve()
44-
item_location_rel = (pathlib.Path(package_location) / item)
45-
item_location = item_location_rel.resolve()
46-
try:
47-
relative_item_location = item_location.relative_to(here)
48-
except ValueError:
49-
# Ideally we would use Pathlib.is_relative_to rather than
50-
# checking for a ValueError, but that is only available in
51-
# Python 3.9+.
52-
relative_item_location = item_location
53-
package_files.append(str(relative_item_location))
34+
package_name = package.name
35+
package_location = package.location
36+
package_files = []
37+
for item in (package.files or []):
38+
here = pathlib.Path('.').resolve()
39+
item_location_rel = (pathlib.Path(package_location) / item)
40+
item_location = item_location_rel.resolve()
41+
try:
42+
relative_item_location = item_location.relative_to(here)
43+
except ValueError:
44+
# Ideally we would use Pathlib.is_relative_to rather than
45+
# checking for a ValueError, but that is only available in
46+
# Python 3.9+.
47+
relative_item_location = item_location
48+
package_files.append(str(relative_item_location))
5449

5550
log.debug('installed package: %s (at %s)', package_name,
5651
package_location)

tests/__init__.py

Whitespace-only changes.

tests/package_info_mock.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List, Optional, NamedTuple
2+
3+
4+
# A stand-in for pip._internal.commands.show._PackageInfo, as returned by
5+
# search_packages_info from the same module
6+
class _PackageInfo(NamedTuple):
7+
name: str
8+
location: str
9+
files: Optional[List[str]]

tests/test_find_extra_reqs.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from pip_check_reqs import find_extra_reqs, common
1414

15+
from .package_info_mock import _PackageInfo
16+
1517

1618
@pytest.fixture
1719
def fake_opts():
@@ -68,11 +70,10 @@ class FakePathDistribution:
6870
pretend.call_recorder(lambda **kwargs: installed_distributions),
6971
)
7072
packages_info = [
71-
dict(name='spam',
72-
location='site-spam',
73-
files=['spam/__init__.py', 'spam/shrub.py']),
74-
dict(name='shrub', location='site-spam', files=['shrub.py']),
75-
dict(name='pass', location='site-spam', files=['pass.py']),
73+
_PackageInfo(name='spam', location='site-spam',
74+
files=['spam/__init__.py', 'spam/shrub.py']),
75+
_PackageInfo(name='shrub', location='site-spam', files=['shrub.py']),
76+
_PackageInfo(name='pass', location='site-spam', files=['pass.py']),
7677
]
7778

7879
monkeypatch.setattr(find_extra_reqs, 'search_packages_info',

tests/test_find_missing_reqs.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22
from dataclasses import dataclass
33
import importlib
4+
import site
45
from typing import Dict, Optional
56

67
import logging
@@ -12,6 +13,8 @@
1213

1314
from pip_check_reqs import find_missing_reqs, common
1415

16+
from .package_info_mock import _PackageInfo
17+
1518

1619
@pytest.fixture
1720
def fake_opts():
@@ -41,11 +44,12 @@ def parse_args(self):
4144

4245

4346
def test_find_missing_reqs(monkeypatch, tmp_path: Path):
47+
site_path = site.getsitepackages()[0]
4448
imported_modules = dict(spam=common.FoundModule('spam',
45-
'site-spam/spam.py',
49+
f'{site_path}/spam.py',
4650
[('ham.py', 1)]),
4751
shrub=common.FoundModule('shrub',
48-
'site-spam/shrub.py',
52+
f'{site_path}/shrub.py',
4953
[('ham.py', 3)]),
5054
ignore=common.FoundModule('ignore', 'ignore.py',
5155
[('ham.py', 2)]))
@@ -67,11 +71,11 @@ class FakePathDistribution:
6771
pretend.call_recorder(lambda **kwargs: installed_distributions),
6872
)
6973
packages_info = [
70-
dict(name='spam',
71-
location='site-spam',
72-
files=['spam/__init__.py', 'spam/shrub.py']),
73-
dict(name='shrub', location='site-spam', files=['shrub.py']),
74-
dict(name='pass', location='site-spam', files=['pass.py']),
74+
_PackageInfo(name='spam',
75+
location=site_path,
76+
files=['spam/__init__.py', 'spam/shrub.py']),
77+
_PackageInfo(name='shrub', location=site_path, files=['shrub.py']),
78+
_PackageInfo(name='pass', location=site_path, files=['pass.py']),
7579
]
7680

7781
monkeypatch.setattr(find_missing_reqs, 'search_packages_info',

0 commit comments

Comments
 (0)