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

Make PipRequirement a subclass of packaging.Requirement #542

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ianrochapg
Copy link

PipRequirement implemented pkg_resources which is very lean and also deprecated. Removing pkg_resources and subclassing directly from packaging.Requirement.

setuptools has been removed because it was only used by pkg_resources, which has also been removed.

Resolves #486

Maintainers will complete the following section

  • Commit messages are descriptive enough
  • Code coverage from testing does not decrease and new code is covered
  • Docs updated (if applicable)
  • Docs links in the code are still valid (if docs were updated)

Note: if the contribution is external (not from an organization member), the CI
pipeline will not run automatically. After verifying that the CI is safe to run:

@slimreaper35
Copy link
Collaborator

slimreaper35 commented May 9, 2024

You forgot to run make pip-compile to regenerate requirements files (it takes pyproject.toml as an input).

Also, we can probably delete --allow-unsafe option.
https://github.com/containerbuildsystem/cachi2/blob/main/Makefile#L53

cachi2/core/package_managers/pip.py Outdated Show resolved Hide resolved
RequirementParseError,
pkg_resources.extern.packaging.requirements.InvalidRequirement,
) as exc:
parsed: Sequence[Requirement] = list(_parse_requirements(to_be_parsed))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this effectively just vendors the old pkg_resources code same way as pkg_resources vendors packaging from which it builds upon. IOW you can drop the whole _parse_requirements parsing into a list and instead do plain packaging.Requirement(to_be_parsed). Note that tests will have to be adjusted since we're checking some corner cases that relate to _parse_requirements behaviour which no longer apply to the usage of packaging.Requirement.

Comment on lines +1464 to +1460
def _standardize_name(name: str) -> str:
return re.sub("[^A-Za-z0-9.]+", "-", name)


def _standardize_version(version: str) -> str:
try:
return str(packaging.version.Version(version))
except packaging.version.InvalidVersion:
version = version.replace(" ", ".")
return re.sub("[^A-Za-z0-9.]+", "-", version)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing about vendoring with ^these 2 - part of the issue is investigating the proper replacements for these and by quickly going through packaging's docs canonicalize_name and version seem like candidates worth exploring whether they accomplish what we need.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ianrochapg I see you pushed a new revision, have you looked at the canonicalize functions I pointed to in my earlier comment to see if it worked and integration tests passed? If so, that would be obviously a win for us because we wouldn't need the 2 helpers you're introducing.

pyproject.toml Outdated Show resolved Hide resolved
@@ -1323,9 +1321,9 @@ def from_line(cls, line: str, options: list[str]) -> Optional["PipRequirement"]:

requirement.download_line = to_be_parsed
requirement.options = options
requirement.package = req.project_name
requirement.package = _standardize_name(req.name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not objecting to this line but I'm unclear whether packaging.Requirement already doesn't have the name attribute normalized so we could potentially just do req.name, but I admit I'm unsure on this one particularly.

PipRequirement implemented pkg_resources which is very lean
and also deprecated. Removing pkg_resources and subclassing
directly from packaging.Requirement.

setuptools has been removed because it was only used by
pkg_resources, which has also been removed.

Resolves containerbuildsystem#486

Signed-off-by: Ian Rocha <iarocha@redhat.com>
@brunoapimentel
Copy link
Contributor

/ok-to-test

@eskultety
Copy link
Collaborator

@ianrochapg if you don't plan on not actually working on subclassing packaging.Requirement inside our code, that's fine, but in that case the subject of both the PR and the commit need to drop any mention of "subclassing". Additionally, the 'resolves' reference should be dropped as well from the PR description since this PR is only one of the steps towards resolving the issue, but not quite, so on its own the issue should not get closed automatically via this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make PipRequirement a subclass of packaging.Requirement
5 participants