Skip to content

Use explicit __init__.py files instead of implicit creation - #1082

Open
onnosteenbergen-samotics wants to merge 1 commit into
bazelbuild:mainfrom
onnosteenbergen-samotics:explicit-init-py
Open

onnosteenbergen-samotics wants to merge 1 commit into
bazelbuild:mainfrom
onnosteenbergen-samotics:explicit-init-py

Conversation

@onnosteenbergen-samotics

Copy link
Copy Markdown

Fixes #1081.

rules_python warns for executables that rely on implicit __init__.py creation (bazel-contrib/rules_python#2945), which shows up for every consumer of pkg_tar, pkg_zip, pkg_deb, pkg_install and verify_archive_test.

  • Add the missing pkg/private/{tar,deb}/__init__.py and put every __init__.py in the srcs of a py_library (//pkg:init, //pkg/private:init).
  • Set legacy_create_init = 0 on all py_binary/py_test targets under pkg/, including those created by the macros. The per-target attribute is used because the module-wide explicit_init_py setting requires rules_python 2.3.
  • Add //tests:explicit_init_test, which fails if a package is imported as a namespace package.

🤖 Generated with Claude Code

rules_python now warns for every executable target that relies on
implicit __init__.py creation (bazel-contrib/rules_python#2945), so
depending on rules_pkg prints that warning for build_tar, build_zip,
make_deb, and for targets created by pkg_install and
verify_archive_test.

Add the missing __init__.py files (pkg/private/tar, pkg/private/deb),
put every __init__.py in the srcs of a py_library (//pkg:init,
//pkg/private:init) that the importing libraries depend on, and set
legacy_create_init = 0 on all py_binary and py_test targets under pkg/.

The per-target attribute is used rather than the module-wide
explicit_init_py setting because the latter requires rules_python 2.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@google-cla

google-cla Bot commented Sep 28, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@onnosteenbergen-samotics

Copy link
Copy Markdown
Author

I've signed the CLA, but the co-author is Claude.ai. Don't know how to solve that one.

@tonyaiuto tonyaiuto left a comment

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.

Have you tried this without setting legacy_create_init? ISTM that it should still work without that, and be backwards compatible with older python rules

@aiuto

aiuto commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

I've signed the CLA, but the co-author is Claude.ai. Don't know how to solve that one.

The only author that matters is onnosteenbergen-samotics.
The check is failling. Did you use that github identity?

@onnosteenbergen-samotics

Copy link
Copy Markdown
Author

Re-ran the check and it still marks Claude as failed.

Will verify if legacy_init_create = 0 is required, would indeed make sense to leave it out.

@onnosteenbergen-samotics

Copy link
Copy Markdown
Author
┌─────────────────────────────────────────────────────────────────────┬──────────────┬──────────┐
│                          rules_pkg variant                          │ rules_python │ Warnings │
├─────────────────────────────────────────────────────────────────────┼──────────────┼──────────┤
│ with legacy_create_init = 0                                         │ 2.3.2        │ 0        │
├─────────────────────────────────────────────────────────────────────┼──────────────┼──────────┤
│ without                                                             │ 2.3.2        │ 5        │
├─────────────────────────────────────────────────────────────────────┼──────────────┼──────────┤
│ with                                                                │ 1.7.0        │ 0        │
├─────────────────────────────────────────────────────────────────────┼──────────────┼──────────┤
│ without                                                             │ 1.7.0        │ 0        │
├─────────────────────────────────────────────────────────────────────┼──────────────┼──────────┤
│ without, consumer passes --incompatible_default_to_explicit_init_py │ 2.3.2        │ 0        │
└─────────────────────────────────────────────────────────────────────┴──────────────┴──────────┘

The 5 warnings in the "without" run on 2.3.2:

WARNING: Target @@rules_pkg+//pkg/private/tar:build_tar is using implicit __init__.py creation.
WARNING: Target @@//:demo_install is using implicit __init__.py creation.
WARNING: Target @@rules_pkg+//pkg/private/zip:build_zip is using implicit __init__.py creation.
WARNING: Target @@//:demo_tar_test is using implicit __init__.py creation.
WARNING: Target @@rules_pkg+//pkg/private/deb:make_deb is using implicit __init__.py creation.

The tar, zip and deb outputs had the same sha256 in every run (e.g. demo_tar.tar is 0639fb21…).

  • The warning doesn't look at the files. rules_python 2.3.2 decides only from the target's legacy_create_init, then the owning module's explicit_init_py config, then the CLI flag. Whether __init__.py files exist doesn't matter, so this might be something that rules_python needs to fix.
  • rules_pkg could set explicit_init_py in its own MODULE.bazel, but that requires rules_python 2.3 or newer as a minimum. The attribute works on 1.7.0, the version rules_pkg currently requires.
  • Without the attribute, users can silence the warning themselves with the flag (last row of the table). Before this PR, that flag didn't make the packages proper Python packages.

This branch has not been deployed

No deployments
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.

Targets rely on implicit __init__.py creation, causing rules_python deprecation warnings

3 participants