Skip to content

Commit

Permalink
feat: replace deprecated pkg_resources with packaging (#105) - th…
Browse files Browse the repository at this point in the history
…anks to @oh2fih

Signed-off-by: Esa Jokinen <[email protected]>

* Fix errors discovered with the unit tests

- Hashable type required. Convert list to tuple.
- Ignore incompatible types.
- Remove comments before passing the line to Req().
- Extras should be in lower case.
- Complete the list of comparison operators with '!'.

Signed-off-by: Esa Jokinen <[email protected]>

---------

Signed-off-by: Esa Jokinen <[email protected]>
  • Loading branch information
oh2fih authored Aug 12, 2024
1 parent c31f81d commit 19dddfa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 57 deletions.
103 changes: 52 additions & 51 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ keywords = [

[tool.poetry.dependencies]
python = "^3.8"
packaging = ">=23.2"
types-setuptools = ">=69.1.0"

[tool.poetry.group.dev.dependencies]
Expand Down
20 changes: 14 additions & 6 deletions requirements/requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import re
from typing import Any, Dict, List, Match, Optional, Tuple, cast

from pkg_resources import Requirement as Req
from packaging.requirements import Requirement as Req

from .fragment import get_hash_info, parse_extras_require, parse_fragment
from .vcs import VCS, VCS_SCHEMES
Expand Down Expand Up @@ -234,12 +234,20 @@ def parse_line(cls, line: str) -> 'Requirement':
req.path = groups['path'] # type: ignore
else:
# This is a requirement specifier.
# Delegate to pkg_resources and hope for the best
# Delegate to packaging.requirements and hope for the best
req.specifier = True
pkg_req = Req.parse(line)
req.name = pkg_req.unsafe_name # type: ignore
req.extras = list(pkg_req.extras)
req.specs = pkg_req.specs
line_without_comment = re.sub('#.*', '', line)
pkg_req = Req(line_without_comment)
req.name = pkg_req.name # type: ignore
req.extras = [x.lower() for x in list(pkg_req.extras)]
# Convert packaging.specifiers.SpecifierSet into
# pkg_resources specs, i.e., a list of (op,version) tuples
specs = []
for specifier in pkg_req.specifier:
spec = re.split('([=<>~!]+)', str(specifier), maxsplit=1)
spec = list(filter(None, spec))
specs.append(tuple(spec))
req.specs = specs # type: ignore
return req

@classmethod
Expand Down

0 comments on commit 19dddfa

Please sign in to comment.