Skip to content

Commit 3d13783

Browse files
authored
Merge branch 'main' into exp/pypi-simplify
2 parents 2cdde89 + 9555ba8 commit 3d13783

File tree

78 files changed

+825
-146
lines changed

Some content is hidden

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

78 files changed

+825
-146
lines changed

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ END_UNRELEASED_TEMPLATE
6060
* (gazelle) Types for exposed members of `python.ParserOutput` are now all public.
6161
* (gazelle) Removed the requirement for `__init__.py`, `__main__.py`, or `__test__.py` files to be
6262
present in a directory to generate a `BUILD.bazel` file.
63-
* (toolchain) Updated the following toolchains to build 20250702 to patch CVE-2025-47273:
63+
* (toolchain) Updated the following toolchains to build 20250708 to patch CVE-2025-47273:
6464
* 3.9.23
6565
* 3.10.18
6666
* 3.11.13
6767
* 3.12.11
68-
* 3.14.0b3
68+
* 3.14.0b4
6969
* (toolchain) Python 3.13 now references 3.13.5
7070
* (gazelle) Switched back to smacker/go-tree-sitter, fixing
7171
[#2630](https://github.com/bazel-contrib/rules_python/issues/2630)
@@ -113,7 +113,14 @@ END_UNRELEASED_TEMPLATE
113113
* 3.11.13
114114
* 3.12.11
115115
* 3.13.5
116-
* 3.14.0b3
116+
* 3.14.0b4
117+
* (gazelle): New annotation `gazelle:include_pytest_conftest`. When not set (the
118+
default) or `true`, gazelle will inject any `conftest.py` file found in the same
119+
directory as a {obj}`py_test` target to that {obj}`py_test` target's `deps`.
120+
This behavior is unchanged from previous versions. When `false`, the `:conftest`
121+
dep is not added to the {obj}`py_test` target.
122+
* (gazelle) New directive `gazelle:python_generate_proto`; when `true`,
123+
Gazelle generates `py_proto_library` rules for `proto_library`. `false` by default.
117124

118125
{#v0-0-0-removed}
119126
### Removed

docs/pypi/lock.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
:::{note}
77
Currently `rules_python` only supports `requirements.txt` format.
8+
9+
#{gh-issue}`2787` tracks `pylock.toml` support.
810
:::
911

1012
## requirements.txt
@@ -37,11 +39,33 @@ This rule generates two targets:
3739
Once you generate this fully specified list of requirements, you can install the requirements ([bzlmod](./download)/[WORKSPACE](./download-workspace)).
3840

3941
:::{warning}
40-
If you're specifying dependencies in `pyproject.toml`, make sure to include the `[build-system]` configuration, with pinned dependencies. `compile_pip_requirements` will use the build system specified to read your project's metadata, and you might see non-hermetic behavior if you don't pin the build system.
42+
If you're specifying dependencies in `pyproject.toml`, make sure to include the
43+
`[build-system]` configuration, with pinned dependencies.
44+
`compile_pip_requirements` will use the build system specified to read your
45+
project's metadata, and you might see non-hermetic behavior if you don't pin the
46+
build system.
4147

42-
Not specifying `[build-system]` at all will result in using a default `[build-system]` configuration, which uses unpinned versions ([ref](https://peps.python.org/pep-0518/#build-system-table)).
48+
Not specifying `[build-system]` at all will result in using a default
49+
`[build-system]` configuration, which uses unpinned versions
50+
([ref](https://peps.python.org/pep-0518/#build-system-table)).
4351
:::
4452

53+
54+
#### pip compile Dependency groups
55+
56+
pip-compile doesn't yet support pyproject.toml dependency groups. Follow
57+
[pip-tools #2062](https://github.com/jazzband/pip-tools/issues/2062)
58+
to see the status of their support.
59+
60+
In the meantime, support can be emulated by passing multiple files to `srcs`:
61+
62+
```starlark
63+
compile_pip_requirements(
64+
srcs = ["pyproject.toml", "requirements-dev.in"]
65+
...
66+
)
67+
```
68+
4569
### uv pip compile (bzlmod only)
4670

4771
We also have experimental setup for the `uv pip compile` way of generating lock files.

examples/bzlmod/py_proto_library/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ py_test(
66
srcs = ["test.py"],
77
main = "test.py",
88
deps = [
9-
"//py_proto_library/example.com/proto:pricetag_proto_py_pb2",
9+
"//py_proto_library/example.com/proto:pricetag_py_pb2",
1010
],
1111
)
1212

1313
py_test(
1414
name = "message_test",
1515
srcs = ["message_test.py"],
1616
deps = [
17-
"//py_proto_library/example.com/another_proto:message_proto_py_pb2",
17+
"//py_proto_library/example.com/another_proto:message_py_pb2",
1818
],
1919
)
2020

examples/bzlmod/py_proto_library/example.com/another_proto/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
22
load("@rules_python//python:proto.bzl", "py_proto_library")
33

44
py_proto_library(
5-
name = "message_proto_py_pb2",
5+
name = "message_py_pb2",
66
visibility = ["//visibility:public"],
77
deps = [":message_proto"],
88
)

examples/bzlmod/py_proto_library/example.com/proto/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
22
load("@rules_python//python:proto.bzl", "py_proto_library")
33

44
py_proto_library(
5-
name = "pricetag_proto_py_pb2",
5+
name = "pricetag_py_pb2",
66
visibility = ["//visibility:public"],
77
deps = [":pricetag_proto"],
88
)

examples/py_proto_library/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ py_test(
55
srcs = ["test.py"],
66
main = "test.py",
77
deps = [
8-
"//example.com/proto:pricetag_proto_py_pb2",
8+
"//example.com/proto:pricetag_py_pb2",
99
],
1010
)
1111

1212
py_test(
1313
name = "message_test",
1414
srcs = ["message_test.py"],
1515
deps = [
16-
"//example.com/another_proto:message_proto_py_pb2",
16+
"//example.com/another_proto:message_py_pb2",
1717
],
1818
)

examples/py_proto_library/example.com/another_proto/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
22
load("@rules_python//python:proto.bzl", "py_proto_library")
33

44
py_proto_library(
5-
name = "message_proto_py_pb2",
5+
name = "message_py_pb2",
66
visibility = ["//visibility:public"],
77
deps = [":message_proto"],
88
)

examples/py_proto_library/example.com/proto/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
22
load("@rules_python//python:proto.bzl", "py_proto_library")
33

44
py_proto_library(
5-
name = "pricetag_proto_py_pb2",
5+
name = "pricetag_py_pb2",
66
visibility = ["//visibility:public"],
77
deps = [":pricetag_proto"],
88
)

gazelle/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ Python-specific directives are as follows:
224224
| Controls whether Gazelle resolves dependencies for import statements that use paths relative to the current package. Can be "true" or "false".|
225225
| `# gazelle:python_generate_pyi_deps` | `false` |
226226
| Controls whether to generate a separate `pyi_deps` attribute for type-checking dependencies or merge them into the regular `deps` attribute. When `false` (default), type-checking dependencies are merged into `deps` for backward compatibility. When `true`, generates separate `pyi_deps`. Imports in blocks with the format `if typing.TYPE_CHECKING:`/`if TYPE_CHECKING:` and type-only stub packages (eg. boto3-stubs) are recognized as type-checking dependencies. |
227+
| [`# gazelle:python_generate_proto`](#directive-python_generate_proto) | `false` |
228+
| Controls whether to generate a `py_proto_library` for each `proto_library` in the package. By default we load this rule from the `@protobuf` repository; use `gazelle:map_kind` if you need to load this from somewhere else. |
227229

228230
#### Directive: `python_root`:
229231

@@ -484,6 +486,41 @@ def py_test(name, main=None, **kwargs):
484486
)
485487
```
486488

489+
#### Directive: `python_generate_proto`:
490+
491+
When `# gazelle:python_generate_proto true`, Gazelle will generate one
492+
`py_proto_library` for each `proto_library`, generating Python clients for
493+
protobuf in each package. By default this is turned off. Gazelle will also
494+
generate a load statement for the `py_proto_library` - attempting to detect
495+
the configured name for the `@protobuf` / `@com_google_protobuf` repo in your
496+
`MODULE.bazel`, and otherwise falling back to `@com_google_protobuf` for
497+
compatibility with `WORKSPACE`.
498+
499+
For example, in a package with `# gazelle:python_generate_proto true` and a
500+
`foo.proto`, if you have both the proto extension and the Python extension
501+
loaded into Gazelle, you'll get something like:
502+
503+
```starlark
504+
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
505+
load("@rules_proto//proto:defs.bzl", "proto_library")
506+
507+
# gazelle:python_generate_proto true
508+
509+
proto_library(
510+
name = "foo_proto",
511+
srcs = ["foo.proto"],
512+
visibility = ["//:__subpackages__"],
513+
)
514+
515+
py_proto_library(
516+
name = "foo_py_pb2",
517+
visibility = ["//:__subpackages__"],
518+
deps = [":foo_proto"],
519+
)
520+
```
521+
522+
When `false`, Gazelle will ignore any `py_proto_library`, including previously-generated or hand-created rules.
523+
487524
### Annotations
488525

489526
*Annotations* refer to comments found _within Python files_ that configure how
@@ -513,6 +550,8 @@ The annotations are:
513550
| Tells Gazelle to ignore import statements. `imports` is a comma-separated list of imports to ignore. | |
514551
| [`# gazelle:include_dep targets`](#annotation-include_dep) | N/A |
515552
| Tells Gazelle to include a set of dependencies, even if they are not imported in a Python module. `targets` is a comma-separated list of target names to include as dependencies. | |
553+
| [`# gazelle:include_pytest_conftest bool`](#annotation-include_pytest_conftest) | N/A |
554+
| Whether or not to include a sibling `:conftest` target in the deps of a `py_test` target. Default behaviour is to include `:conftest`. | |
516555

517556

518557
#### Annotation: `ignore`
@@ -585,6 +624,89 @@ deps = [
585624
]
586625
```
587626

627+
#### Annotation: `include_pytest_conftest`
628+
629+
Added in [#3080][gh3080].
630+
631+
[gh3080]: https://github.com/bazel-contrib/rules_python/pull/3080
632+
633+
This annotation accepts any string that can be parsed by go's
634+
[`strconv.ParseBool`][ParseBool]. If an unparsable string is passed, the
635+
annotation is ignored.
636+
637+
[ParseBool]: https://pkg.go.dev/strconv#ParseBool
638+
639+
Starting with [`rules_python` 0.14.0][rules-python-0.14.0] (specifically [PR #879][gh879]),
640+
Gazelle will include a `:conftest` dependency to an `py_test` target that is in
641+
the same directory as `conftest.py`.
642+
643+
[rules-python-0.14.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.14.0
644+
[gh879]: https://github.com/bazel-contrib/rules_python/pull/879
645+
646+
This annotation allows users to adjust that behavior. To disable the behavior, set
647+
the annotation value to "false":
648+
649+
```
650+
# some_file_test.py
651+
# gazelle:include_pytest_conftest false
652+
```
653+
654+
Example:
655+
656+
Given a directory tree like:
657+
658+
```
659+
.
660+
├── BUILD.bazel
661+
├── conftest.py
662+
└── some_file_test.py
663+
```
664+
665+
The default Gazelle behavior would create:
666+
667+
```starlark
668+
py_library(
669+
name = "conftest",
670+
testonly = True,
671+
srcs = ["conftest.py"],
672+
visibility = ["//:__subpackages__"],
673+
)
674+
675+
py_test(
676+
name = "some_file_test",
677+
srcs = ["some_file_test.py"],
678+
deps = [":conftest"],
679+
)
680+
```
681+
682+
When `# gazelle:include_pytest_conftest false` is found in `some_file_test.py`
683+
684+
```python
685+
# some_file_test.py
686+
# gazelle:include_pytest_conftest false
687+
```
688+
689+
Gazelle will generate:
690+
691+
```starlark
692+
py_library(
693+
name = "conftest",
694+
testonly = True,
695+
srcs = ["conftest.py"],
696+
visibility = ["//:__subpackages__"],
697+
)
698+
699+
py_test(
700+
name = "some_file_test",
701+
srcs = ["some_file_test.py"],
702+
)
703+
```
704+
705+
See [Issue #3076][gh3076] for more information.
706+
707+
[gh3076]: https://github.com/bazel-contrib/rules_python/issues/3076
708+
709+
588710
#### Directive: `experimental_allow_relative_imports`
589711
Enables experimental support for resolving relative imports in
590712
`python_generation_mode package`.

gazelle/python/BUILD.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ go_library(
3434
"@bazel_gazelle//config:go_default_library",
3535
"@bazel_gazelle//label:go_default_library",
3636
"@bazel_gazelle//language:go_default_library",
37+
"@bazel_gazelle//language/proto:go_default_library",
3738
"@bazel_gazelle//repo:go_default_library",
3839
"@bazel_gazelle//resolve:go_default_library",
3940
"@bazel_gazelle//rule:go_default_library",
@@ -91,7 +92,10 @@ gazelle_test(
9192

9293
gazelle_binary(
9394
name = "gazelle_binary",
94-
languages = [":python"],
95+
languages = [
96+
"@bazel_gazelle//language/proto",
97+
":python",
98+
],
9599
visibility = ["//visibility:public"],
96100
)
97101

0 commit comments

Comments
 (0)