Skip to content

Commit 32c1c2e

Browse files
committed
Merge branch 'main' of https://github.com/bazel-contrib/rules_python into feat.custom.toolchain.override
2 parents d428325 + e73dccf commit 32c1c2e

File tree

75 files changed

+1560
-557
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1560
-557
lines changed

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13.3

CHANGELOG.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ END_UNRELEASED_TEMPLATE
6666
* (rules) On Windows, {obj}`--bootstrap_impl=system_python` is forced. This
6767
allows setting `--bootstrap_impl=script` in bazelrc for mixed-platform
6868
environments.
69-
* (rules) {obj}`pip_compile` now generates a `.test` target. The `_test` target is deprecated
70-
and will be removed in the next major release.
69+
* (rules) {obj}`compile_pip_requirements` now generates a `.test` target. The
70+
`_test` target is deprecated and will be removed in the next major release.
7171
([#2794](https://github.com/bazel-contrib/rules_python/issues/2794)
7272
* (py_wheel) py_wheel always creates zip64-capable wheel zips
7373

@@ -91,6 +91,7 @@ END_UNRELEASED_TEMPLATE
9191
also retrieved from the URL as opposed to only the `--hash` parameter. Fixes
9292
[#2363](https://github.com/bazel-contrib/rules_python/issues/2363).
9393
* (pypi) `whl_library` now infers file names from its `urls` attribute correctly.
94+
* (py_test, py_binary) Allow external files to be used for main
9495

9596
{#v0-0-0-added}
9697
### Added
@@ -102,6 +103,8 @@ END_UNRELEASED_TEMPLATE
102103
* (pypi) Starlark-based evaluation of environment markers (requirements.txt conditionals)
103104
available (not enabled by default) for improved multi-platform build support.
104105
Set the `RULES_PYTHON_ENABLE_PIPSTAR=1` environment variable to enable it.
106+
* (utils) Add a way to run a REPL for any `rules_python` target that returns
107+
a `PyInfo` provider.
105108
* (toolchains) Arbitrary python-build-standalone runtimes can be registered
106109
and activated with custom flags. See the [Registering custom runtimes]
107110
docs and {obj}`single_version_platform_override()` API docs for more
@@ -192,7 +195,7 @@ END_UNRELEASED_TEMPLATE
192195
packages through SimpleAPI unless they are pulled through direct URL
193196
references. Fixes [#2023](https://github.com/bazel-contrib/rules_python/issues/2023).
194197
In case you see issues with `rules_python` being too eager to fetch the SimpleAPI
195-
metadata, you can use the newly added {attr}`pip.parse.experimental_skip_sources`
198+
metadata, you can use the newly added {attr}`pip.parse.simpleapi_skip`
196199
to skip metadata fetching for those packages.
197200
* (uv) A {obj}`lock` rule that is the replacement for the
198201
{obj}`compile_pip_requirements`. This may still have rough corners
@@ -253,7 +256,7 @@ END_UNRELEASED_TEMPLATE
253256

254257
{#v1-3-0-added}
255258
### Added
256-
* (python) {attr}`python.defaults` has been added to allow users to
259+
* (python) {obj}`python.defaults` has been added to allow users to
257260
set the default python version in the root module by reading the
258261
default version number from a file or an environment variable.
259262
* {obj}`//python/bin:python`: convenience target for directly running an
@@ -273,7 +276,7 @@ END_UNRELEASED_TEMPLATE
273276
and py_library rules
274277
([#1647](https://github.com/bazel-contrib/rules_python/issues/1647))
275278
* (rules) Added env-var to allow additional interpreter args for stage1 bootstrap.
276-
See {obj}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable.
279+
See {any}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable.
277280
Only applicable for {obj}`--bootstrap_impl=script`.
278281
* (rules) Added {obj}`interpreter_args` attribute to `py_binary` and `py_test`,
279282
which allows pass arguments to the interpreter before the regular args.
@@ -379,7 +382,7 @@ END_UNRELEASED_TEMPLATE
379382
values. Fixes [#2466](https://github.com/bazel-contrib/rules_python/issues/2466).
380383
* (py_proto_library) Fix import paths in Bazel 8.
381384
* (whl_library) Now the changes to the dependencies are correctly tracked when
382-
PyPI packages used in {bzl:obj}`whl_library` during the `repository_rule` phase
385+
PyPI packages used in `whl_library` during the repository rule phase
383386
change. Fixes [#2468](https://github.com/bazel-contrib/rules_python/issues/2468).
384387
+ (gazelle) Gazelle no longer ignores `setup.py` files by default. To restore
385388
this behavior, apply the `# gazelle:python_ignore_files setup.py` directive.
@@ -398,7 +401,7 @@ END_UNRELEASED_TEMPLATE
398401
* (pypi) Freethreaded packages are now fully supported in the
399402
{obj}`experimental_index_url` usage or the regular `pip.parse` usage.
400403
To select the free-threaded interpreter in the repo phase, please use
401-
the documented [env](/environment-variables.html) variables.
404+
the documented [env](environment-variables) variables.
402405
Fixes [#2386](https://github.com/bazel-contrib/rules_python/issues/2386).
403406
* (toolchains) Use the latest astrahl-sh toolchain release [20241206] for Python versions:
404407
* 3.9.21
@@ -492,7 +495,7 @@ Other changes:
492495
for the latest toolchain versions for each minor Python version. You can control
493496
the toolchain selection by using the
494497
{bzl:obj}`//python/config_settings:py_linux_libc` build flag.
495-
* (providers) Added {obj}`py_runtime_info.site_init_template` and
498+
* (providers) Added {obj}`PyRuntimeInfo.site_init_template` and
496499
{obj}`PyRuntimeInfo.site_init_template` for specifying the template to use to
497500
initialize the interpreter via venv startup hooks.
498501
* (runfiles) (Bazel 7.4+) Added support for spaces and newlines in runfiles paths
@@ -690,8 +693,8 @@ Other changes:
690693
* (bzlmod) The default value for the {obj}`--python_version` flag will now be
691694
always set to the default python toolchain version value.
692695
* (bzlmod) correctly wire the {attr}`pip.parse.extra_pip_args` all the
693-
way to {obj}`whl_library`. What is more we will pass the `extra_pip_args` to
694-
{obj}`whl_library` for `sdist` distributions when using
696+
way to `whl_library`. What is more we will pass the `extra_pip_args` to
697+
`whl_library` for `sdist` distributions when using
695698
{attr}`pip.parse.experimental_index_url`. See
696699
[#2239](https://github.com/bazel-contrib/rules_python/issues/2239).
697700
* (whl_filegroup): Provide per default also the `RECORD` file
@@ -739,8 +742,8 @@ Other changes:
739742

740743
{#v0-37-0-removed}
741744
### Removed
742-
* (precompiling) {obj}`--precompile_add_to_runfiles` has been removed.
743-
* (precompiling) {obj}`--pyc_collection` has been removed. The `pyc_collection`
745+
* (precompiling) `--precompile_add_to_runfiles` has been removed.
746+
* (precompiling) `--pyc_collection` has been removed. The `pyc_collection`
744747
attribute now bases its default on {obj}`--precompile`.
745748
* (precompiling) The {obj}`precompile=if_generated_source` value has been removed.
746749
* (precompiling) The {obj}`precompile_source_retention=omit_if_generated_source` value has been removed.
@@ -792,7 +795,7 @@ Other changes:
792795
in extra_requires in py_wheel rule.
793796
* (rules) Prevent pytest from trying run the generated stage2
794797
bootstrap .py file when using {obj}`--bootstrap_impl=script`
795-
* (toolchain) The {bzl:obj}`gen_python_config_settings` has been fixed to include
798+
* (toolchain) The `gen_python_config_settings` has been fixed to include
796799
the flag_values from the platform definitions.
797800

798801
{#v0-36-0-added}
@@ -1207,9 +1210,9 @@ Other changes:
12071210
depend on legacy labels instead of the hub repo aliases and you use the
12081211
`experimental_requirement_cycles`, now is a good time to migrate.
12091212

1210-
[python_default_visibility]: gazelle/README.md#directive-python_default_visibility
1213+
[python_default_visibility]: https://github.com/bazel-contrib/rules_python/tree/main/gazelle/README.md#directive-python_default_visibility
12111214
[test_file_pattern_issue]: https://github.com/bazel-contrib/rules_python/issues/1816
1212-
[test_file_pattern_docs]: gazelle/README.md#directive-python_test_file_pattern
1215+
[test_file_pattern_docs]: https://github.com/bazel-contrib/rules_python/tree/main/gazelle/README.md#directive-python_test_file_pattern
12131216
[20240224]: https://github.com/indygreg/python-build-standalone/releases/tag/20240224.
12141217
[20240415]: https://github.com/indygreg/python-build-standalone/releases/tag/20240415.
12151218

docs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ sphinx_stardocs(
113113
"//python/private:builders_util_bzl",
114114
"//python/private:py_binary_rule_bzl",
115115
"//python/private:py_cc_toolchain_rule_bzl",
116+
"//python/private:py_info_bzl",
116117
"//python/private:py_library_rule_bzl",
117118
"//python/private:py_runtime_rule_bzl",
118119
"//python/private:py_test_rule_bzl",

docs/_includes/py_console_script_binary.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ py_console_script_binary(
4848
)
4949
```
5050

51+
#### Adding a Shebang Line
52+
53+
You can specify a shebang line for the generated binary, useful for Unix-like
54+
systems where the shebang line determines which interpreter is used to execute
55+
the script, per [PEP441]:
56+
57+
```starlark
58+
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
59+
60+
py_console_script_binary(
61+
name = "black",
62+
pkg = "@pip//black",
63+
shebang = "#!/usr/bin/env python3",
64+
)
65+
```
66+
67+
Note that to execute via the shebang line, you need to ensure the specified
68+
Python interpreter is available in the environment.
69+
70+
5171
#### Using a specific Python Version directly from a Toolchain
5272
:::{deprecated} 1.1.0
5373
The toolchain specific `py_binary` and `py_test` symbols are aliases to the regular rules.
@@ -70,4 +90,5 @@ py_console_script_binary(
7090
```
7191

7292
[specification]: https://packaging.python.org/en/latest/specifications/entry-points/
73-
[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule
93+
[`py_console_script_binary.binary_rule`]: #py_console_script_binary_binary_rule
94+
[PEP441]: https://peps.python.org/pep-0441/#minimal-tooling-the-zipapp-module

docs/api/rules_python/python/bin/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
A target to directly run a Python interpreter.
1111

1212
By default, it uses the Python version that toolchain resolution matches
13-
(typically the one marked `is_default=True` in `MODULE.bazel`).
13+
(typically the one set with `python.defaults(python_version = ...)` in
14+
`MODULE.bazel`).
1415

1516
This runs a Python interpreter in a similar manner as when running `python3`
1617
on the command line. It can be invoked using `bazel run`. Remember that in

docs/conf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@
125125
primary_domain = None # The default is 'py', which we don't make much use of
126126
nitpicky = True
127127

128+
nitpick_ignore_regex = [
129+
# External xrefs aren't setup: ignore missing xref warnings
130+
# External xrefs to sphinx isn't setup: ignore missing xref warnings
131+
("py:.*", "(sphinx|docutils|ast|enum|collections|typing_extensions).*"),
132+
]
133+
128134
# --- Intersphinx configuration
129135

130136
intersphinx_mapping = {

DEVELOPING.md renamed to docs/devguide.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# For Developers
1+
# Dev Guide
22

33
This document covers tips and guidance for working on the rules_python code
44
base. A primary audience for it is first time contributors.
@@ -39,7 +39,7 @@ more important for tests to balance understandability and maintainability.
3939

4040
### sh_py_run_test
4141

42-
The [`sh_py_run_test`](tests/support/sh_py_run_test.bzl) rule is a helper to
42+
The {gh-path}`sh_py_run_test <tests/support/sh_py_run_test.bzl` rule is a helper to
4343
make it easy to run a Python program with custom build settings using a shell
4444
script to perform setup and verification. This is best to use when verifying
4545
behavior needs certain environment variables or directory structures to
@@ -56,6 +56,8 @@ Python binaries and tests with custom build flags. This is best to use when
5656
verifying behavior that requires specific flags to be set and when the program
5757
itself can verify the desired state.
5858

59+
They are located in {gh-path}`tests/support/py_reconfig.bzl`
60+
5961
When adding a test, you may find the flag you need to set isn't supported by
6062
the rule. To have it support setting a new flag:
6163

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and the older way of using `WORKSPACE`.
77
It assumes you have a `requirements.txt` file with your PyPI dependencies.
88

99
For more details information about configuring `rules_python`, see:
10-
* [Configuring the runtime](toolchains)
10+
* [Configuring the runtime](configuring-toolchains)
1111
* [Configuring third party dependencies (pip/pypi)](pypi-dependencies)
1212
* [API docs](api/index)
1313

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ pip
101101
coverage
102102
precompiling
103103
gazelle
104+
REPL <repl>
104105
Extending <extending>
105106
Contributing <contributing>
107+
devguide
106108
support
107109
Changelog <changelog>
108110
api/index

docs/repl.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Getting a REPL or Interactive Shell
2+
3+
rules_python provides a REPL to help with debugging and developing. The goal of
4+
the REPL is to present an environment identical to what a {bzl:obj}`py_binary` creates
5+
for your code.
6+
7+
## Usage
8+
9+
Start the REPL with the following command:
10+
```console
11+
$ bazel run @rules_python//python/bin:repl
12+
Python 3.11.11 (main, Mar 17 2025, 21:02:09) [Clang 20.1.0 ] on linux
13+
Type "help", "copyright", "credits" or "license" for more information.
14+
>>>
15+
```
16+
17+
Settings like `//python/config_settings:python_version` will influence the exact
18+
behaviour.
19+
```console
20+
$ bazel run @rules_python//python/bin:repl --@rules_python//python/config_settings:python_version=3.13
21+
Python 3.13.2 (main, Mar 17 2025, 21:02:54) [Clang 20.1.0 ] on linux
22+
Type "help", "copyright", "credits" or "license" for more information.
23+
>>>
24+
```
25+
26+
See [//python/config_settings](api/rules_python/python/config_settings/index)
27+
and [Environment Variables](environment-variables) for more settings.
28+
29+
## Importing Python targets
30+
31+
The `//python/bin:repl_dep` command line flag gives the REPL access to a target
32+
that provides the {bzl:obj}`PyInfo` provider.
33+
34+
```console
35+
$ bazel run @rules_python//python/bin:repl --@rules_python//python/bin:repl_dep=@rules_python//tools:wheelmaker
36+
Python 3.11.11 (main, Mar 17 2025, 21:02:09) [Clang 20.1.0 ] on linux
37+
Type "help", "copyright", "credits" or "license" for more information.
38+
>>> import tools.wheelmaker
39+
>>>
40+
```
41+
42+
## Customizing the shell
43+
44+
By default, the `//python/bin:repl` target will invoke the shell from the `code`
45+
module. It's possible to switch to another shell by writing a custom "stub" and
46+
pointing the target at the necessary dependencies.
47+
48+
### IPython Example
49+
50+
For an IPython shell, create a file as follows.
51+
52+
```python
53+
import IPython
54+
IPython.start_ipython()
55+
```
56+
57+
Assuming the file is called `ipython_stub.py` and the `pip.parse` hub's name is
58+
`my_deps`, set this up in the .bazelrc file:
59+
```
60+
# Allow the REPL stub to import ipython. In this case, @my_deps is the hub name
61+
# of the pip.parse() call.
62+
build --@rules_python//python/bin:repl_stub_dep=@my_deps//ipython
63+
64+
# Point the REPL at the stub created above.
65+
build --@rules_python//python/bin:repl_stub=//path/to:ipython_stub.py
66+
```

0 commit comments

Comments
 (0)