10000 feat: add alias generation for pip parse by corypaik · Pull Request #814 · bazel-contrib/rules_python · GitHub
[go: up one dir, main page]

Skip to content

feat: add alias generation for pip parse #814

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

Conversation

corypaik
Copy link

Generate alias packages and targets for pip_parse under the parent repository. This serves two purposes. It allows for users to reference packages using a simpler @pypi//package_name without the import issues associated with putting the contents of the package there (as may be the case with pip_install following this naming pattern). Additionally, this allows users to override individual libraries either by default or based on select statements.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature (please, look at the "Scope of the project" section in the README.md file)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

Currently, in order to refer to a dependency by name with pip_parse one must use the syntax @pypi_package_name//:pkg. It it also not currently possible to override the dependencies of pip repositories using local packages or those built with Bazel. The only workaround available now is to define a repository (e.g., @pypi_package_name prior to calling pip_parse. This is not ideal, and does not allow for selecting a version to use (e.g., local vs. from pip) via flags.

What is the new behavior?

The alias packages add the option to refer to dependencies under one central repository using the syntax @pypi//package_name. Additionally, it allows users to override the library targets by default or using any valid select statement.

Note that the original names (e.g., @pypi_package_name//:pkg) remain unmodified, as do all functions in requirements.bzl. The only exception is to all_requirements, which now uses the aliases instead. Happy to adjust this behavior as desired, my primary usage is @pypi//package_name and a few interactive targets that use all_requirements.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I originally was working on a version which made the creation of alias packages optional (and disabled by default). After trying to profile both with and without the alias repositories I could not find any measurable difference so I got rid of the option entirely.

I've refactored the existing tests to work with these changes and added some notes to the pip_parse example, but I'd also be happy to add additional tests if need be.

@UebelAndre
Copy link
Contributor

@corypaik would you be willing to rebase this PR? I think it's a very useful change!

@corypaik corypaik force-pushed the corypaik/feat-pip-parse-alias-generation branch from bfa2203 to 96efb1a Compare September 30, 2022 22:35
@corypaik corypaik force-pushed the corypaik/feat-pip-parse-alias-generation branch 3 times, most recently from 96efb1a to db232db Compare October 29, 2022 19:39
Generate alias packages and targets for `pip_parse` under the parent repository.
This serves two purposes. It allows for users to reference packages using a
simpler `@pypi//package_name` without the import issues associated with
putting the contents of the package there (as may be the case with `pip_install`
following this naming pattern). Additionally, this allows users to override
individual libraries either by default or based on select statements.
@corypaik corypaik force-pushed the corypaik/feat-pip-parse-alias-generation branch from db232db to d4ecd4c Compare November 27, 2022 00:03
@rickeylev
Copy link
Collaborator

Sorry, this fell off my radar.

Can you explain more about the motivating use case a bit more? Essentially, you want to allow an arbitrary select condition to control what target is used? I'm not necessarily against doing so, but it strikes me as a bit strange -- ignoring Bazel for a moment, it'd be like pip installing something, then mucking with the install or sys.path to override particular libraries, which makes me raise my eye brows.

Does pip have a builtin mechanism to ignore a particular dependency? Or maybe we should modify the requirements.txt processing to remove the desired library and replace it with the desired alias?

There's a Bazel flag to override specific repositories -- does that satisfy the same need?

Each pypi package being its own repo is most likely an intentional design decision (an unreferenced repo doesn't get loaded or fetched at all; I think it better isolates the repo-rule impact, too?). Putting everything into a single repo likely defeats that to some extent.

I think the aliases need to be moved into the respective package's repo target -- this ensures that only a single actual target for a library is used. Otherwise, what you can do is e.g. deps = ["@pypy//foo", "@pypi_foo//foo"], and end up with two different versions of the same library and behavior is undefined as to which you get. This behavior happens transitively -- i.e. if the transitive closure of a target contains both those targets, you'll end up with undefined behavior.

as may be the case with pip_install

I think we deprecated pip_install in favor of pip_parse? I don't think we should add features to something we've deprecated.

aignas added a commit to aignas/rules_python that referenced this pull request Jan 27, 2023
This is related to bazel-contrib#814 as it is bringing user-friendly alias generation
and it is doing this in a breaking way for the `bzlmod` users who are
not using the `requirement` macro. The labels that they use will change
from `@@repo//:{name}_pkg` to `@@repo//{name}`.

I am intentionally leaving the requirement macros in the
requirements.bzl unchanged as bazel-contrib#989 might change the approach again.

Since these aliases can be used by bzlmod and non-bzlmod users, we can
then support the same generated `gazelle_python.yaml` for both bzlmod
and non-bzlmod users.
@github-actions
Copy link

This Pull Request has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

@github-actions github-actions bot added the Can Close? Will close in 30 days if there is no new activity label May 31, 2023
@github-actions
Copy link

This PR was automatically closed because it went 30 days without a reply since it was labeled "Can Close?"

@github-actions github-actions bot closed this Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Can Close? Will close in 30 days if there is no new activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0