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

Fix support for pip versions pre-21.3 #74

Merged
merged 1 commit into from Oct 28, 2021
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -2,6 +2,10 @@
Release History
---------------

2.3.2

- Fixed support for pip < 21.3

2.3.1

- Fixed `--skip-incompatible` skipping other requirements too.
Expand Down
2 changes: 1 addition & 1 deletion pip_check_reqs/__init__.py
@@ -1 +1 @@
__version__ = '2.3.1'
__version__ = '2.3.2'
78 changes: 41 additions & 37 deletions pip_check_reqs/common.py
Expand Up @@ -14,46 +14,50 @@
except ImportError: # pragma: no cover
from pip._internal.download import PipSession
from pip._internal.req.req_file import parse_requirements
from pip._internal.utils.compat import stdlib_pkgs
from pip._internal.metadata import get_default_environment, get_environment
from pip._internal.metadata.pkg_resources import Distribution as _Dist
from pip._vendor.pkg_resources import Distribution
try:
from pip._internal.utils.misc import get_installed_distributions
except ImportError: # pip>=21.3
from pip._internal.utils.compat import stdlib_pkgs
from pip._internal.metadata import get_default_environment, get_environment
from pip._internal.metadata.pkg_resources import Distribution as _Dist
from pip._vendor.pkg_resources import Distribution

# get_installed_distributions was removed in pip 21.3.
# This is a copy from pip.
# See
# https://github.com/pypa/pip/commit/d051a00fc57037104fca85ad8ebf2cdbd1e32d24#diff-058e40cb3a9ea705f655937e48f3a053f5dc7c500b7f1b2aae76e9bd673faf64.
#
# This is mocked in all tests (unfortunately) and so we do not cover this
# function.
def get_installed_distributions(
local_only: bool = True,
skip: Container[str] = stdlib_pkgs,
include_editables: bool = True,
editables_only: bool = False,
user_only: bool = False,
paths: Optional[List[str]] = None,
) -> List[Distribution]: # pragma: no cover
"""Return a list of installed Distribution objects.

Left for compatibility until direct pkg_resources uses are refactored
out.
"""
if paths is None:
env = get_default_environment()
else:
env = get_environment(paths)

log = logging.getLogger(__name__)
dists = env.iter_installed_distributions(
local_only=local_only,
skip=skip,
include_editables=include_editables,
editables_only=editables_only,
user_only=user_only,
)
return [cast(_Dist, dist)._dist for dist in dists]


# get_installed_distributions was removed in pip 21.3.
# This is a copy from pip.
# See
# https://github.com/pypa/pip/commit/d051a00fc57037104fca85ad8ebf2cdbd1e32d24#diff-058e40cb3a9ea705f655937e48f3a053f5dc7c500b7f1b2aae76e9bd673faf64.
#
# This is mocked in all tests (unfortunately) and so we do not cover this
# function.
def get_installed_distributions(
local_only: bool = True,
skip: Container[str] = stdlib_pkgs,
include_editables: bool = True,
editables_only: bool = False,
user_only: bool = False,
paths: Optional[List[str]] = None,
) -> List[Distribution]: # pragma: no cover
"""Return a list of installed Distribution objects.

Left for compatibility until direct pkg_resources uses are refactored out.
"""
if paths is None:
env = get_default_environment()
else:
env = get_environment(paths)

dists = env.iter_installed_distributions(
local_only=local_only,
skip=skip,
include_editables=include_editables,
editables_only=editables_only,
user_only=user_only,
)
return [cast(_Dist, dist)._dist for dist in dists]
log = logging.getLogger(__name__)


class FoundModule:
Expand Down