Skip to content

Commit

Permalink
fix: achillesrasquinha#71 Package object has no attribute 'name'
Browse files Browse the repository at this point in the history
From pip 20.X.X onwards, `parse_requirements` returns a generator
of ParsedRequirement objects, instead of the InstallRequirement
objects as previously
Need to wrap `parse_requirements` to return `InstallRequirement` objects
instead
  • Loading branch information
max-nicholson committed Jan 29, 2021
1 parent 1ec3d5f commit 697973b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
33 changes: 26 additions & 7 deletions src/pipupgrade/_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pip
import json
import os.path as osp
from typing import List

# imports - module imports
from pipupgrade.util.system import which, popen
Expand All @@ -15,16 +16,34 @@

logger = get_logger()

PIP9 = int(pip.__version__.split(".")[0]) < 10
MAJOR_VERSION = int(pip.__version__.split(".")[0])

if PIP9:
# from pip import get_installed_distributions
from pip.req import parse_requirements
from pip.req.req_install import InstallRequirement
else:
if MAJOR_VERSION >= 20:
from pip._internal.req.constructors import install_req_from_parsed_requirement
from pip._internal.req.req_file import (
parse_requirements as _real_parse_requirements,
)
from pip._internal.req.req_install import InstallRequirement

def parse_requirements(
filename, session
): # type: (str, str) -> List[InstallRequirement]
"""Wrap pip internal `parse_requirements`, which now returns
`ParsedRequirement` instances, to instead return `InstallRequirement`-
as with the previous implementation
Based on https://github.com/pypa/pip/blob/a48ad5385b234097d51283b08c3d933fd81ef534/tests/unit/test_req_file.py#L50"""
for parsed_req in _real_parse_requirements(filename, session):
yield install_req_from_parsed_requirement(parsed_req)


elif MAJOR_VERSION >= 10:
# from pip._internal.utils.misc import get_installed_distributions
from pip._internal.req import parse_requirements
from pip._internal.req import parse_requirements
from pip._internal.req.req_install import InstallRequirement
else:
# from pip import get_installed_distributions
from pip.req import parse_requirements
from pip.req.req_install import InstallRequirement

from pip._vendor.pkg_resources import (
Distribution,
Expand Down
17 changes: 13 additions & 4 deletions tests/pipupgrade/test__pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from __future__ import absolute_import

# imports - standard imports
import subprocess
import os

# imports - test imports
import pytest
from testutils import PATH

# imports - module imports
from pipupgrade import _pip
Expand Down Expand Up @@ -43,8 +43,17 @@ def _assert_outerr(routerr, outerr):

_pip.call("install", "pipupgrade")
assert_pip_call(_pip.call("install", "pipupgrade", quiet = True))

_pip.call("install", "pipupgrade", log = path)
assert tempfile.read()

# assert_pip_call(_pip.call("list", output = True))
# assert_pip_call(_pip.call("list", output = True))


def test_parse_requirements():
"""`parse_requirements` returns an iterable of `InstallRequirement`"""
filepath = os.path.join(PATH["DATA"], "project", "requirements.txt")

requirements = list(_pip.parse_requirements(filepath, session="hack"))

assert all([isinstance(req, _pip.InstallRequirement) for req in requirements])

0 comments on commit 697973b

Please sign in to comment.