Skip to content

Commit 0fc459d

Browse files
authored
Merge branch 'main' into fix/1975/uv-auth
2 parents fd339b3 + 3aea414 commit 0fc459d

File tree

95 files changed

+2188
-1502
lines changed

Some content is hidden

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

95 files changed

+2188
-1502
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ max_line_length = 100
1515
[*.{py,bzl}]
1616
indent_style = space
1717
indent_size = 4
18+
19+
# different overrides for git commit messages
20+
[.git/COMMIT_EDITMSG]
21+
max_line_length = 72

.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: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ BEGIN_UNRELEASED_TEMPLATE
2727
## Unreleased
2828
2929
[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0
30+
and the sha256 values
31+
behave differently when using `github.com` and the private mirror that mirrors
32+
`github.com`. The contents of the `.sha256` files differ. What is more, to be able
33+
to use the private mirror I need to set the `base_url` attribute in the uv.configure.
34+
The bazel downloader config is not enough.
3035
3136
{#v0-0-0-changed}
3237
### Changed
@@ -66,8 +71,8 @@ END_UNRELEASED_TEMPLATE
6671
* (rules) On Windows, {obj}`--bootstrap_impl=system_python` is forced. This
6772
allows setting `--bootstrap_impl=script` in bazelrc for mixed-platform
6873
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.
74+
* (rules) {obj}`compile_pip_requirements` now generates a `.test` target. The
75+
`_test` target is deprecated and will be removed in the next major release.
7176
([#2794](https://github.com/bazel-contrib/rules_python/issues/2794)
7277
* (py_wheel) py_wheel always creates zip64-capable wheel zips
7378

@@ -86,6 +91,12 @@ END_UNRELEASED_TEMPLATE
8691
multiple times.
8792
* (tools/wheelmaker.py) Extras are now preserved in Requires-Dist metadata when using requires_file
8893
to specify the requirements.
94+
* (pypi) Use bazel downloader for direct URL references and correctly detect the filenames from
95+
various URL formats - URL encoded version strings get correctly resolved, sha256 value can be
96+
also retrieved from the URL as opposed to only the `--hash` parameter. Fixes
97+
[#2363](https://github.com/bazel-contrib/rules_python/issues/2363).
98+
* (pypi) `whl_library` now infers file names from its `urls` attribute correctly.
99+
* (py_test, py_binary) Allow external files to be used for main
89100

90101
{#v0-0-0-added}
91102
### Added
@@ -99,6 +110,8 @@ END_UNRELEASED_TEMPLATE
99110
Set the `RULES_PYTHON_ENABLE_PIPSTAR=1` environment variable to enable it.
100111
* (uv) Handle `.netrc` and `auth_patterns` auth when downloading `uv`. Work towards
101112
[#1975](https://github.com/bazel-contrib/rules_python/issues/1975).
113+
* (utils) Add a way to run a REPL for any `rules_python` target that returns
114+
a `PyInfo` provider.
102115

103116
{#v0-0-0-removed}
104117
### Removed
@@ -185,7 +198,7 @@ END_UNRELEASED_TEMPLATE
185198
packages through SimpleAPI unless they are pulled through direct URL
186199
references. Fixes [#2023](https://github.com/bazel-contrib/rules_python/issues/2023).
187200
In case you see issues with `rules_python` being too eager to fetch the SimpleAPI
188-
metadata, you can use the newly added {attr}`pip.parse.experimental_skip_sources`
201+
metadata, you can use the newly added {attr}`pip.parse.simpleapi_skip`
189202
to skip metadata fetching for those packages.
190203
* (uv) A {obj}`lock` rule that is the replacement for the
191204
{obj}`compile_pip_requirements`. This may still have rough corners
@@ -246,7 +259,7 @@ END_UNRELEASED_TEMPLATE
246259

247260
{#v1-3-0-added}
248261
### Added
249-
* (python) {attr}`python.defaults` has been added to allow users to
262+
* (python) {obj}`python.defaults` has been added to allow users to
250263
set the default python version in the root module by reading the
251264
default version number from a file or an environment variable.
252265
* {obj}`//python/bin:python`: convenience target for directly running an
@@ -266,7 +279,7 @@ END_UNRELEASED_TEMPLATE
266279
and py_library rules
267280
([#1647](https://github.com/bazel-contrib/rules_python/issues/1647))
268281
* (rules) Added env-var to allow additional interpreter args for stage1 bootstrap.
269-
See {obj}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable.
282+
See {any}`RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS` environment variable.
270283
Only applicable for {obj}`--bootstrap_impl=script`.
271284
* (rules) Added {obj}`interpreter_args` attribute to `py_binary` and `py_test`,
272285
which allows pass arguments to the interpreter before the regular args.
@@ -372,7 +385,7 @@ END_UNRELEASED_TEMPLATE
372385
values. Fixes [#2466](https://github.com/bazel-contrib/rules_python/issues/2466).
373386
* (py_proto_library) Fix import paths in Bazel 8.
374387
* (whl_library) Now the changes to the dependencies are correctly tracked when
375-
PyPI packages used in {bzl:obj}`whl_library` during the `repository_rule` phase
388+
PyPI packages used in `whl_library` during the repository rule phase
376389
change. Fixes [#2468](https://github.com/bazel-contrib/rules_python/issues/2468).
377390
+ (gazelle) Gazelle no longer ignores `setup.py` files by default. To restore
378391
this behavior, apply the `# gazelle:python_ignore_files setup.py` directive.
@@ -391,7 +404,7 @@ END_UNRELEASED_TEMPLATE
391404
* (pypi) Freethreaded packages are now fully supported in the
392405
{obj}`experimental_index_url` usage or the regular `pip.parse` usage.
393406
To select the free-threaded interpreter in the repo phase, please use
394-
the documented [env](/environment-variables.html) variables.
407+
the documented [env](environment-variables) variables.
395408
Fixes [#2386](https://github.com/bazel-contrib/rules_python/issues/2386).
396409
* (toolchains) Use the latest astrahl-sh toolchain release [20241206] for Python versions:
397410
* 3.9.21
@@ -485,7 +498,7 @@ Other changes:
485498
for the latest toolchain versions for each minor Python version. You can control
486499
the toolchain selection by using the
487500
{bzl:obj}`//python/config_settings:py_linux_libc` build flag.
488-
* (providers) Added {obj}`py_runtime_info.site_init_template` and
501+
* (providers) Added {obj}`PyRuntimeInfo.site_init_template` and
489502
{obj}`PyRuntimeInfo.site_init_template` for specifying the template to use to
490503
initialize the interpreter via venv startup hooks.
491504
* (runfiles) (Bazel 7.4+) Added support for spaces and newlines in runfiles paths
@@ -683,8 +696,8 @@ Other changes:
683696
* (bzlmod) The default value for the {obj}`--python_version` flag will now be
684697
always set to the default python toolchain version value.
685698
* (bzlmod) correctly wire the {attr}`pip.parse.extra_pip_args` all the
686-
way to {obj}`whl_library`. What is more we will pass the `extra_pip_args` to
687-
{obj}`whl_library` for `sdist` distributions when using
699+
way to `whl_library`. What is more we will pass the `extra_pip_args` to
700+
`whl_library` for `sdist` distributions when using
688701
{attr}`pip.parse.experimental_index_url`. See
689702
[#2239](https://github.com/bazel-contrib/rules_python/issues/2239).
690703
* (whl_filegroup): Provide per default also the `RECORD` file
@@ -732,8 +745,8 @@ Other changes:
732745

733746
{#v0-37-0-removed}
734747
### Removed
735-
* (precompiling) {obj}`--precompile_add_to_runfiles` has been removed.
736-
* (precompiling) {obj}`--pyc_collection` has been removed. The `pyc_collection`
748+
* (precompiling) `--precompile_add_to_runfiles` has been removed.
749+
* (precompiling) `--pyc_collection` has been removed. The `pyc_collection`
737750
attribute now bases its default on {obj}`--precompile`.
738751
* (precompiling) The {obj}`precompile=if_generated_source` value has been removed.
739752
* (precompiling) The {obj}`precompile_source_retention=omit_if_generated_source` value has been removed.
@@ -785,7 +798,7 @@ Other changes:
785798
in extra_requires in py_wheel rule.
786799
* (rules) Prevent pytest from trying run the generated stage2
787800
bootstrap .py file when using {obj}`--bootstrap_impl=script`
788-
* (toolchain) The {bzl:obj}`gen_python_config_settings` has been fixed to include
801+
* (toolchain) The `gen_python_config_settings` has been fixed to include
789802
the flag_values from the platform definitions.
790803

791804
{#v0-36-0-added}
@@ -1200,9 +1213,9 @@ Other changes:
12001213
depend on legacy labels instead of the hub repo aliases and you use the
12011214
`experimental_requirement_cycles`, now is a good time to migrate.
12021215

1203-
[python_default_visibility]: gazelle/README.md#directive-python_default_visibility
1216+
[python_default_visibility]: https://github.com/bazel-contrib/rules_python/tree/main/gazelle/README.md#directive-python_default_visibility
12041217
[test_file_pattern_issue]: https://github.com/bazel-contrib/rules_python/issues/1816
1205-
[test_file_pattern_docs]: gazelle/README.md#directive-python_test_file_pattern
1218+
[test_file_pattern_docs]: https://github.com/bazel-contrib/rules_python/tree/main/gazelle/README.md#directive-python_test_file_pattern
12061219
[20240224]: https://github.com/indygreg/python-build-standalone/releases/tag/20240224.
12071220
[20240415]: https://github.com/indygreg/python-build-standalone/releases/tag/20240415.
12081221

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/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+
```

docs/toolchains.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
:::{default-domain} bzl
22
:::
33

4+
(configuring-toolchains)=
45
# Configuring Python toolchains and runtimes
56

67
This documents how to configure the Python toolchain and runtimes for different
@@ -43,7 +44,8 @@ you should read the dev-only library module section.
4344
bazel_dep(name="rules_python", version=...)
4445
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
4546
46-
python.toolchain(python_version = "3.12", is_default = True)
47+
python.defaults(python_version = "3.12")
48+
python.toolchain(python_version = "3.12")
4749
```
4850

4951
### Library modules
@@ -71,7 +73,8 @@ python = use_extension(
7173
dev_dependency = True
7274
)
7375
74-
python.toolchain(python_version = "3.12", is_default=True)
76+
python.defaults(python_version = "3.12")
77+
python.toolchain(python_version = "3.12")
7578
```
7679

7780
#### Library modules without version constraints
@@ -160,9 +163,13 @@ Multiple versions can be specified and used within a single build.
160163
# MODULE.bazel
161164
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
162165

166+
python.defaults(
167+
# The environment variable takes precedence if set.
168+
python_version = "3.11",
169+
python_version_env = "BAZEL_PYTHON_VERSION",
170+
)
163171
python.toolchain(
164172
python_version = "3.11",
165-
is_default = True,
166173
)
167174

168175
python.toolchain(
@@ -263,7 +270,8 @@ bazel_dep(name = "rules_python", version = "0.40.0")
263270

264271
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
265272

266-
python.toolchain(is_default = True, python_version = "3.10")
273+
python.defaults(python_version = "3.10")
274+
python.toolchain(python_version = "3.10")
267275

268276
use_repo(python, "python_3_10", "python_3_10_host")
269277
```
@@ -757,3 +765,13 @@ a fixed version.
757765
The `python` target does not provide access to any modules from `py_*`
758766
targets on its own. Please file a feature request if this is desired.
759767
:::
768+
769+
### Differences from `//python/bin:repl`
770+
771+
The `//python/bin:python` target provides access to the underlying interpreter
772+
without any hermeticity guarantees.
773+
774+
The [`//python/bin:repl` target](repl) provides an environment indentical to
775+
what `py_binary` provides. That means it handles things like the
776+
[`PYTHONSAFEPATH`](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSAFEPATH)
777+
environment variable automatically. The `//python/bin:python` target will not.

examples/bzlmod/MODULE.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ bazel_dep(name = "rules_rust", version = "0.54.1")
2828
# We next initialize the python toolchain using the extension.
2929
# You can set different Python versions in this block.
3030
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
31+
python.defaults(
32+
# Use python.defaults if you have defined multiple toolchain versions.
33+
python_version = "3.9",
34+
python_version_env = "BAZEL_PYTHON_VERSION",
35+
)
3136
python.toolchain(
3237
configure_coverage_tool = True,
33-
# Only set when you have multiple toolchain versions.
34-
is_default = True,
3538
python_version = "3.9",
3639
)
3740

examples/bzlmod/other_module/MODULE.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ PYTHON_NAME_39 = "python_3_9"
2525
PYTHON_NAME_311 = "python_3_11"
2626

2727
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
28+
python.defaults(
29+
# In a submodule this is ignored
30+
python_version = "3.11",
31+
)
2832
python.toolchain(
2933
configure_coverage_tool = True,
3034
python_version = "3.9",
3135
)
3236
python.toolchain(
3337
configure_coverage_tool = True,
34-
# In a submodule this is ignored
35-
is_default = True,
3638
python_version = "3.11",
3739
)
3840

0 commit comments

Comments
 (0)