Skip to content

Commit

Permalink
Stop using pkg_resources
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
  • Loading branch information
astrojuanlu committed May 29, 2023
1 parent 66195c0 commit e628447
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 19 additions & 11 deletions kedro/framework/cli/micropkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Iterable, List, Tuple, Union

import click
import pkg_resources
from packaging.requirements import InvalidRequirement, Requirement
from rope.base.project import Project
from rope.contrib import generate
from rope.refactor.move import MoveModule
Expand Down Expand Up @@ -583,8 +583,10 @@ def _make_install_requires(requirements_txt: Path) -> list[str]:
install_requires."""
if not requirements_txt.exists():
return []
requirements = pkg_resources.parse_requirements(requirements_txt.read_text())
return [str(requirement) for requirement in requirements]
return [
str(Requirement(requirement_line))
for requirement_line in requirements_txt.read_text().splitlines()
]


def _create_nested_package(project: Project, package_path: Path) -> Path:
Expand Down Expand Up @@ -866,17 +868,23 @@ def _append_package_reqs(

def _safe_parse_requirements(
requirements: str | Iterable[str],
) -> set[pkg_resources.Requirement]:
"""Safely parse a requirement or set of requirements. This effectively replaces
pkg_resources.parse_requirements, which blows up with a ValueError as soon as it
) -> set[Requirement]:
"""Safely parse a requirement or set of requirements. This avoids blowing up when it
encounters a requirement it cannot parse (e.g. `-r requirements.txt`). This way
we can still extract all the parseable requirements out of a set containing some
unparseable requirements.
"""
parseable_requirements = set()
for requirement in pkg_resources.yield_lines(requirements):
try:
parseable_requirements.add(pkg_resources.Requirement.parse(requirement))
except ValueError:
continue
# TODO: Properly handle continuation lines,
# see https://github.com/pypa/setuptools/blob/v67.8.0/setuptools/_reqs.py
for requirement_line in requirements:
if (
requirement_line
and not requirement_line.startswith("#")
and not requirement_line.startswith("-e")
):
try:
parseable_requirements.add(Requirement(requirement_line))
except InvalidRequirement:
continue
return parseable_requirements
2 changes: 1 addition & 1 deletion tests/framework/cli/micropkg/test_micropkg_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"""

# These requirements can be used in requirements.txt but not in METADATA Requires-Dist.
# They cannot be parsed by pkg_resources.
# They cannot be parsed by packaging.
COMPLEX_REQUIREMENTS = """--extra-index-url https://this.wont.work
-r other_requirements.txt
./path/to/package.whl
Expand Down

0 comments on commit e628447

Please sign in to comment.