Skip to content

feat(rules): expose pytest_test rule for public use - #4244

Merged
rickeylev merged 7 commits into
bazel-contrib:mainfrom
aignas:aignas.feat.expose-pytest-rule
Oct 12, 2026
Merged

rickeylev merged 7 commits into
bazel-contrib:mainfrom
aignas:aignas.feat.expose-pytest-rule

Conversation

@aignas

@aignas aignas commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

The pytest_test rule has gone through some great additions
and I think it is time to advertise it to others as well.

At the same time:

  • Add pytest-randomly and pytest-shard. The latter is optional
    dependency and used only the tests are sharded.
  • Move the rule to private and update callsites.
  • Add pytest-randomly to our lock file.

Things that we could do in addition to this

  • Add pip.dep declarations. These may make the error messages easier
    to interpret?
  • Move pytest-bazel to bazel-contrib/rules_python or as a standalone
    project.

Closes #3594

The pytest_test rule has gone through some great additions
and I think it is time to advertise it to others as well.

At the same time:
- Add `pytest-randomly` and `pytest-shard`. The latter is optional
  dependency and used only the tests are sharded.
- Move the rule to `private` and update callsites.
- Add `pytest-randomly` to our lock file.

Things that we could do in addition to this
- Add `pip.dep` declarations. These may make the error messages easier
  to interpret?
- Move pytest-bazel to `bazel-contrib/rules_python` or as a standalone
  project.

Closes bazel-contrib#3594
@aignas
aignas requested a review from rickeylev as a code owner October 11, 2026 11:40

@rickeylev rickeylev 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.

imma have my agent post review comments and see how well it goes

pytest_bazel,
]
if pytest_randomly != None:
deps.append(pytest_randomly)

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.

can't use append(); deps may be a select. use + instead.

did you use an agent to write this? Just asking because, if so, i'll go update the agent configs to have something like "for user-provided label list values, assume a select() is a valid input unless documented otherwise, so append() can't be used and + should be used instead"

@rickeylev rickeylev 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.

Left a few review comments covering:

  1. External Bzlmod & visibility support (python/private/pytest_test/BUILD.bazel and MODULE.bazel): the default_pytest* aliases need visibility = NOT_ACTUALLY_PUBLIC and @pypi needs to be visible to @rules_python when used as a non-root Bzlmod dependency.
  2. pytest_randomly opt-out & macro helper tags (python/private/pytest_test/pytest_test.bzl): removing the redundant != None check, documenting @rules_python//python:none (or an empty py_library target) to opt out of pytest_randomly, and tagging _write_pytest_bootstrap with tags = ["manual"].
  3. Docs & bzl_library wiring (python/BUILD.bazel, docs/BUILD.bazel, python/pytest_test.bzl, news/4244.added.md): adding the //python:pytest_test bzl_library target and registering it in //docs:bzl_api_docs so {obj}\pytest_test`` resolves in Sphinx.
  4. Lockfile sync & tests (dev/pyproject.toml, dev/requirements.txt, tests/pytest_test/): syncing dev/requirements.txt and adding test coverage for pytest_shard / pytest_randomly.

This is an agent-generated comment by His Magnanimous Celestial Eminence, Archangel Michael, Supreme Benevolent Sovereign of the Starlark Firmament.

Comment thread dev/pyproject.toml
Comment thread news/4244.added.md Outdated
Comment thread python/private/pytest_test/BUILD.bazel
Comment thread python/private/pytest_test/pytest_test.bzl Outdated
Comment thread python/private/pytest_test/pytest_test.bzl
Comment thread python/private/pytest_test/pytest_test.bzl Outdated
Comment thread python/pytest_test.bzl Outdated
@rickeylev

Copy link
Copy Markdown
Collaborator

2. Bzlmod @pypi repo mapping: Because these alias targets live inside @rules_python, actual = "@pypi//..." is resolved using @rules_python's repository mapping. In MODULE.bazel, "pypi" is currently only in use_repo(dev_pip, ..., "pypi", ...) (dev_dependency = True), not on the non-dev pip extension (use_repo(pip, "rules_python_publish_deps")). For downstream Bzlmod users, @pypi won't be visible from @@rules_python+ unless "pypi" is added to the non-dev use_repo(pip, ...) in MODULE.bazel.

Hm. We should add a test in a sub-module to check that things resolve OK. I don't know if we can have multiple use_repo() calls with the same repo name but different dev-only settings.

@rickeylev rickeylev 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.

LGTM in concept, so approved

Ensure pytest_test works cleanly for external Bzlmod consumers and
meets project documentation and Starlark conventions:
- Expose @pypi in non-dev use_repo(pip, ...) and mark default_pytest*
  aliases and bootstrap_template as NOT_ACTUALLY_PUBLIC.
- Support select() in deps, tag _write_pytest_bootstrap as manual, and
  provide PyInfo from //python:none so callers can disable
  pytest-randomly.
- Add //python:pytest_test bzl_library, Sphinx stardoc target,
  versionadded directives, and news issue link.
- Add pytest-shard to dev/pyproject.toml, sync lockfiles, and expand
  pytest_test test coverage.
Explain that select() in pytest_default_test's deps verifies that
pytest_test handles selector_list values when concatenating default and
sharding dependencies.
Ensure @pypi//pytest, @pypi//pytest_bazel, @pypi//pytest_randomly, and
@pypi//pytest_shard always exist in the unified @pypi hub so downstream
consumers get helpful MissingPyPiDep errors instead of package loading
errors when any of these dependencies are absent from their lockfile.
Add a dedicated rules_python Usage Auditor subagent and rules_python.md
rule to verify Bzlmod pip.dep() declarations for @pypi targets under
python/**, and require the 'do not merge' label when PR descriptions
contain open questions.
…pytest-rule

# Conflicts:
#	.agents/skills/create-pr/SKILL.md
@rickeylev
rickeylev added this pull request to the merge queue Oct 12, 2026
Merged via the queue into bazel-contrib:main with commit d8b1834 Oct 12, 2026
5 checks passed
@aignas
aignas deleted the aignas.feat.expose-pytest-rule branch October 13, 2026 00:54
@aignas

aignas commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks!

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.

Introduce pytest integration out of the box

2 participants