Skip to content

Commit 9f6a653

Browse files
committed
Remove redundant code paths
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: - `PipSession` is now only found in `pip._internal._network.session`. - `install_req_from_line` is always used. - `search_packages_info` now always gives us a generator that yields `pip._internal.commands.show._PackageInfo` objects (not `dict`).
1 parent 42294c1 commit 9f6a653

File tree

7 files changed

+64
-85
lines changed

7 files changed

+64
-85
lines changed

pip_check_reqs/common.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313

1414
from . import __version__
1515

16-
# Between different versions of pip the location of PipSession has changed.
17-
try:
18-
from pip._internal.network.session import PipSession
19-
except ImportError: # pragma: no cover
20-
from pip._internal.download import PipSession
16+
from pip._internal.network.session import PipSession
17+
from pip._internal.req.constructors import install_req_from_line
2118
from pip._internal.req.req_file import parse_requirements
2219

2320

@@ -137,16 +134,9 @@ def find_required_modules(options, requirements_filename: str):
137134
explicit = set()
138135
for requirement in parse_requirements(requirements_filename,
139136
session=PipSession()):
140-
try:
141-
requirement_name = requirement.name
142-
# The type of "requirement" changed between pip versions.
143-
# We exclude the "except" from coverage so that on any pip version we
144-
# can report 100% coverage.
145-
except AttributeError: # pragma: no cover
146-
from pip._internal.req.constructors import install_req_from_line
147-
requirement_name = install_req_from_line(
148-
requirement.requirement,
149-
).name
137+
requirement_name = install_req_from_line(
138+
requirement.requirement,
139+
).name
150140

151141
if options.ignore_reqs(requirement):
152142
log.debug('ignoring requirement: %s', requirement_name)
@@ -193,13 +183,10 @@ def ignorer(ignore_cfg):
193183
def f(candidate, ignore_cfg=ignore_cfg):
194184
for ignore in ignore_cfg:
195185
try:
196-
from pip._internal.req.constructors import (
197-
install_req_from_line,
198-
)
199-
candidate_path = install_req_from_line( # pragma: no cover
186+
candidate_path = install_req_from_line(
200187
candidate.requirement,
201188
).name
202-
except (ImportError, AttributeError):
189+
except AttributeError:
203190
try:
204191
candidate_path = candidate.name
205192
except AttributeError:

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: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88

99
from packaging.utils import canonicalize_name
1010
from pip._internal.commands.show import search_packages_info
11-
# Between different versions of pip the location of PipSession has changed.
12-
try:
13-
from pip._internal.network.session import PipSession
14-
except ImportError: # pragma: no cover
15-
from pip._internal.download import PipSession
11+
from pip._internal.network.session import PipSession
12+
from pip._internal.req.constructors import install_req_from_line
1613
from pip._internal.req.req_file import parse_requirements
1714

1815
from pip_check_reqs import common
@@ -34,26 +31,21 @@ def find_missing_reqs(options, requirements_filename):
3431
)
3532

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

5850
log.debug('installed package: %s (at %s)', package_name,
5951
package_location)
@@ -89,16 +81,9 @@ def find_missing_reqs(options, requirements_filename):
8981
requirements_filename,
9082
session=PipSession(),
9183
):
92-
try:
93-
requirement_name = requirement.name
94-
# The type of "requirement" changed between pip versions.
95-
# We exclude the "except" from coverage so that on any pip version we
96-
# can report 100% coverage.
97-
except AttributeError: # pragma: no cover
98-
from pip._internal.req.constructors import install_req_from_line
99-
requirement_name = install_req_from_line(
100-
requirement.requirement,
101-
).name
84+
requirement_name = install_req_from_line(
85+
requirement.requirement,
86+
).name
10287

10388
log.debug('found requirement: %s', requirement_name)
10489
explicit.add(canonicalize_name(requirement_name))

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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from pip_check_reqs import find_missing_reqs, common
1414

15+
from .package_info_mock import _PackageInfo
16+
1517

1618
@pytest.fixture
1719
def fake_opts():
@@ -67,11 +69,11 @@ class FakePathDistribution:
6769
pretend.call_recorder(lambda **kwargs: installed_distributions),
6870
)
6971
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']),
72+
_PackageInfo(name='spam',
73+
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']),
7577
]
7678

7779
monkeypatch.setattr(find_missing_reqs, 'search_packages_info',

0 commit comments

Comments
 (0)