Skip to content

feat(pypi): restrict pip.parse hub visibility#3755

Draft
Harshal96 wants to merge 2 commits intobazel-contrib:mainfrom
Harshal96:feat/pip-parse-restricted-visibility
Draft

feat(pypi): restrict pip.parse hub visibility#3755
Harshal96 wants to merge 2 commits intobazel-contrib:mainfrom
Harshal96:feat/pip-parse-restricted-visibility

Conversation

@Harshal96
Copy link
Copy Markdown

Add a restrict_visibility_to attribute for bzlmod pip.parse so hub aliases are public only for packages listed in direct requirement files.

Keep all lockfile packages available to generated wheel repositories so transitive dependencies continue to resolve internally.

Fixes #3413

PR Instructions/requirements

  • Title uses type: description format. See CONTRIBUTING.md for types.
    • Common types are: build, docs, feat, fix, refactor, revert, test
    • Update CHANGELOG.md as applicable
  • Breaking changes include "!" after the type and a "BREAKING CHANGES:"
    section at the bottom.
    See CONTRIBUTING.md for our breaking changes process.
  • Body text describes:
    • Why this change is being made, briefly.
    • Before and after behavior, as applicable
    • References issue number, as applicable
  • Update docs and tests, as applicable
  • Delete these instructions prior to sending the PR

@Harshal96 Harshal96 requested review from aignas and rickeylev as code owners May 7, 2026 03:53
@Harshal96 Harshal96 marked this pull request as draft May 7, 2026 03:59
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the restrict_visibility_to attribute to pip.parse, allowing users to limit public hub aliases to direct dependencies specified in requirement files. Transitive dependencies not included in these files are kept internal to the generated wheel repositories, preventing their use as direct dependencies. The changes include updates to documentation, the pip.parse extension, requirement parsing logic, and visibility rendering in the hub repository, along with new unit tests. I have no feedback to provide as there were no review comments.

@Harshal96 Harshal96 force-pushed the feat/pip-parse-restricted-visibility branch from 23b1141 to a43c2f2 Compare May 7, 2026 04:04
Harshal96 added 2 commits May 6, 2026 23:04
Add a restrict_visibility_to attribute for bzlmod pip.parse so hub aliases are public only for packages listed in direct requirement files.

Keep all lockfile packages available to generated wheel repositories so transitive dependencies continue to resolve internally.

Fixes bazel-contrib#3413
Copy link
Copy Markdown
Collaborator

@aignas aignas left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution.

the platforms stored as values in the input dict. Returns the same
dict, but with values being platforms that are compatible with the
requirements line.
exposed_requirements: List of requirements files. When present, only
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we key this by_platform as well?

if not exposed_requirements:
return None

exposed = {}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This acts like a union. Is this the right thing to do here?

From the modeling perspective, the requirements may be different based on the target platform. That said, I think this approach is OK in the parse_requirements internals. Since we are asking the user to provide the source lock files.

a corresponding `python.toolchain()` configured.
""",
),
"restrict_visibility_to": attr.label_list(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I like adding this, but I imaging that we could repurpose this set of files for more labels.

  1. Restrict visibility.
  2. Create a @pypi//:lock.update target automatically that includes the srcs passed here as srcs passed to the lock file. This plumbing would be similar to what rules_jvm have with their repin.

We should also assume that we will need to support pyproject.toml files/file via the same interface in the future.

extra_pip_args = pip_attr.extra_pip_args,
get_index_urls = self._get_index_urls.get(pip_attr.python_version),
evaluate_markers = _evaluate_markers(self, pip_attr),
exposed_requirements = pip_attr.restrict_visibility_to,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we create a separate function for this?

    exposed_requirements = parse_dep_srcs(module_ctx, pip_attr.restrict_visibility_to)

That way we make the code more maintainable and easier to test.

Comment on lines +241 to +242
if exposed_package_names != None and normalized_name not in exposed_package_names:
is_exposed = False
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would like this logic to be somewhere outside the parse_requirements.bzl file. Would it be possible to move it to hub_builder.bzl? The output of parse_requirements have this is_exposed flag (that is an internal implementation detail) and if we make the lock sources mandatory, then we would be able to drop that implementation detail, which actually makes the code a little bit more complex than it needs to be.


normalized_name = normalize_name(name)
is_exposed = len(requirement_target_platforms) == len(requirements)
if exposed_package_names != None and normalized_name not in exposed_package_names:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't like the double negatives, maybe the following is easier to read?

Suggested change
if exposed_package_names != None and normalized_name not in exposed_package_names:
if not (exposed_package_names and normalized_name in exposed_package_names):

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.

pip.parse should allow hiding transitive dependencies

2 participants