Skip to content

Commit 49c4dc8

Browse files
authored
feat(gazelle): Add directives for label format & normalisation (#1976)
Adds new directives to alter default Gazelle label format to third-party dependencies useful for re-using Gazelle plugin with other rules, including `rules_pycross`. Fixes #1939
1 parent 8d40b19 commit 49c4dc8

File tree

36 files changed

+476
-21
lines changed

36 files changed

+476
-21
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ A brief description of the categories of changes:
6868
* (toolchains) {obj}`//python/runtime_env_toolchains:all`, which is a drop-in
6969
replacement for the "autodetecting" toolchain.
7070
71+
### Added
72+
* (gazelle) Added new `python_label_convention` and `python_label_normalization` directives. These directive
73+
allows altering default Gazelle label format to third-party dependencies useful for re-using Gazelle plugin
74+
with other rules, including `rules_pycross`. See [#1939](https://github.com/bazelbuild/rules_python/issues/1939).
75+
7176
### Removed
7277
* (pip): Removes the `entrypoint` macro that was replaced by `py_console_script_binary` in 0.26.0.
7378

gazelle/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ Python-specific directives are as follows:
204204
| Appends additional visibility labels to each generated target. This directive can be set multiple times. | |
205205
| [`# gazelle:python_test_file_pattern`](#directive-python_test_file_pattern) | `*_test.py,test_*.py` |
206206
| Filenames matching these comma-separated `glob`s will be mapped to `py_test` targets. |
207-
207+
| `# gazelle:python_label_convention` | `$distribution_name$` |
208+
| Defines the format of the distribution name in labels to third-party deps. Useful for using Gazelle plugin with other rules with different repository conventions (e.g. `rules_pycross`). Full label is always prepended with (pip) repository name, e.g. `@pip//numpy`. |
209+
| `# gazelle:python_label_normalization` | `snake_case` |
210+
| Controls how distribution names in labels to third-party deps are normalized. Useful for using Gazelle plugin with other rules with different label conventions (e.g. `rules_pycross` uses PEP-503). Can be "snake_case", "none", or "pep503". |
208211

209212
#### Directive: `python_root`:
210213

gazelle/python/configure.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func (py *Configurer) KnownDirectives() []string {
6767
pythonconfig.DefaultVisibilty,
6868
pythonconfig.Visibility,
6969
pythonconfig.TestFilePattern,
70+
pythonconfig.LabelConvention,
71+
pythonconfig.LabelNormalization,
7072
}
7173
}
7274

@@ -196,6 +198,23 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
196198
}
197199
}
198200
config.SetTestFilePattern(globStrings)
201+
case pythonconfig.LabelConvention:
202+
value := strings.TrimSpace(d.Value)
203+
if value == "" {
204+
log.Fatalf("directive '%s' requires a value", pythonconfig.LabelConvention)
205+
}
206+
config.SetLabelConvention(value)
207+
case pythonconfig.LabelNormalization:
208+
switch directiveArg := strings.ToLower(strings.TrimSpace(d.Value)); directiveArg {
209+
case "pep503":
210+
config.SetLabelNormalization(pythonconfig.Pep503LabelNormalizationType)
211+
case "none":
212+
config.SetLabelNormalization(pythonconfig.NoLabelNormalizationType)
213+
case "snake_case":
214+
config.SetLabelNormalization(pythonconfig.SnakeCaseLabelNormalizationType)
215+
default:
216+
config.SetLabelNormalization(pythonconfig.DefaultLabelNormalizationType)
217+
}
199218
}
200219
}
201220

gazelle/python/testdata/annotation_include_dep/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import module1
21
import foo # third party package
2+
import module1
33

44
# gazelle:include_dep //foo/bar:baz
55
# gazelle:include_dep //hello:world,@star_wars//rebel_alliance/luke:skywalker
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Directive: `python_label_convention`
2+
3+
This test case asserts that the `# gazelle:python_label_convention` directive
4+
works as intended when set.

gazelle/python/testdata/directive_python_label_convention/WORKSPACE

Whitespace-only changes.

gazelle/python/testdata/directive_python_label_convention/test.yaml

Whitespace-only changes.

gazelle/python/testdata/directive_python_label_convention/test1_unset/BUILD.in

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "test1_unset",
5+
srcs = ["bar.py"],
6+
visibility = ["//:__subpackages__"],
7+
deps = [
8+
"@gazelle_python_test//google_cloud_aiplatform",
9+
"@gazelle_python_test//google_cloud_storage",
10+
],
11+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from google.cloud import aiplatform, storage
2+
3+
4+
def main():
5+
a = dir(aiplatform)
6+
b = dir(storage)

0 commit comments

Comments
 (0)