-
-
Notifications
You must be signed in to change notification settings - Fork 639
docs(gazelle): Migrate Gazelle docs to ReadTheDocs, part 3/5: annotations #3137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dougthor42
merged 2 commits into
bazel-contrib:main
from
dougthor42:u/dthor/gazelle-refactor-docs-pt3-annotations
Aug 4, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| # Annotations | ||
|
|
||
| *Annotations* refer to comments found _within Python files_ that configure how | ||
| Gazelle acts for that particular file. | ||
|
|
||
| Annotations have the form: | ||
|
|
||
| ```python | ||
| # gazelle:annotation_name value | ||
| ``` | ||
|
|
||
| and can reside anywhere within a Python file where comments are valid. For example: | ||
|
|
||
| ```python | ||
| import foo | ||
| # gazelle:annotation_name value | ||
|
|
||
| def bar(): # gazelle:annotation_name value | ||
| pass | ||
| ``` | ||
|
|
||
| The annotations are: | ||
|
|
||
| * [`# gazelle:ignore imports`](#ignore) | ||
| * Default: n/a | ||
| * Allowed Values: A comma-separated string of python package names | ||
| * Tells Gazelle to ignore import statements. `imports` is a comma-separated | ||
| list of imports to ignore. | ||
| * [`# gazelle:include_dep targets`](#include-dep) | ||
| * Default: n/a | ||
| * Allowed Values: A string | ||
| * 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. | ||
| * [`# gazelle:include_pytest_conftest bool`](#include-pytest-conftest) | ||
| * Default: n/a | ||
| * Allowed Values: `true`, `false` | ||
| * Whether or not to include a sibling `:conftest` target in the `deps` | ||
| of a {bzl:obj}`py_test` target. The default behaviour is to include `:conftest` | ||
| (i.e.: `# gazelle:include_pytest_conftest true`). | ||
|
|
||
|
|
||
| ## `ignore` | ||
|
|
||
| This annotation accepts a comma-separated string of values. Values are names of | ||
| Python imports that Gazelle should _not_ include in target dependencies. | ||
|
|
||
| The annotation can be added multiple times, and all values are combined and | ||
| de-duplicated. | ||
|
|
||
| For `python_generation_mode = "package"`, the `ignore` annotations | ||
| found across all files included in the generated target are removed from | ||
| `deps`. | ||
|
|
||
| ### Example: | ||
|
|
||
| ```python | ||
| import numpy # a pypi package | ||
|
|
||
| # gazelle:ignore bar.baz.hello,foo | ||
| import bar.baz.hello | ||
| import foo | ||
|
|
||
| # Ignore this import because _reasons_ | ||
| import baz # gazelle:ignore baz | ||
| ``` | ||
|
|
||
| will cause Gazelle to generate: | ||
|
|
||
| ```starlark | ||
| deps = ["@pypi//numpy"], | ||
| ``` | ||
|
|
||
|
|
||
| ## `include_dep` | ||
|
|
||
| This annotation accepts a comma-separated string of values. Values _must_ | ||
| be Python targets, but _no validation is done_. If a value is not a Python | ||
| target, building will result in an error saying: | ||
|
|
||
| ``` | ||
| <target> does not have mandatory providers: 'PyInfo' or 'CcInfo' or 'PyInfo'. | ||
| ``` | ||
|
|
||
| Adding non-Python targets to the generated target is a feature request being | ||
| tracked in {gh-issue}`1865`. | ||
|
|
||
| The annotation can be added multiple times, and all values are combined | ||
| and de-duplicated. | ||
|
|
||
| For `python_generation_mode = "package"`, the `include_dep` annotations | ||
| found across all files included in the generated target are included in | ||
| `deps`. | ||
|
|
||
| ### Example: | ||
|
|
||
| ```python | ||
| # gazelle:include_dep //foo:bar,:hello_world,//:abc | ||
| # gazelle:include_dep //:def,//foo:bar | ||
| import numpy # a pypi package | ||
| ``` | ||
|
|
||
| will cause Gazelle to generate: | ||
|
|
||
| ```starlark | ||
| deps = [ | ||
| ":hello_world", | ||
| "//:abc", | ||
| "//:def", | ||
| "//foo:bar", | ||
| "@pypi//numpy", | ||
| ] | ||
| ``` | ||
|
|
||
|
|
||
| ## `include_pytest_conftest` | ||
|
|
||
| :::{versionadded} VERSION_NEXT_FEATURE | ||
| {gh-pr}`3080` | ||
| ::: | ||
|
|
||
| This annotation accepts any string that can be parsed by go's | ||
| [`strconv.ParseBool`][ParseBool]. If an unparsable string is passed, the | ||
| annotation is ignored. | ||
|
|
||
| [ParseBool]: https://pkg.go.dev/strconv#ParseBool | ||
|
|
||
| Starting with [`rules_python` 0.14.0][rules-python-0.14.0] (specifically | ||
| {gh-pr}`879`), Gazelle will include a `:conftest` dependency to a | ||
| {bzl:obj}`py_test` target that is in the same directory as `conftest.py`. | ||
|
|
||
| [rules-python-0.14.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.14.0 | ||
|
|
||
| This annotation allows users to adjust that behavior. To disable the behavior, | ||
| set the annotation value to `false`: | ||
|
|
||
| ``` | ||
| # some_file_test.py | ||
| # gazelle:include_pytest_conftest false | ||
| ``` | ||
|
|
||
| ### Example: | ||
|
|
||
| Given a directory tree like: | ||
|
|
||
| ``` | ||
| . | ||
| ├── BUILD.bazel | ||
| ├── conftest.py | ||
| └── some_file_test.py | ||
| ``` | ||
|
|
||
| The default Gazelle behavior would create: | ||
|
|
||
| ```starlark | ||
| py_library( | ||
| name = "conftest", | ||
| testonly = True, | ||
| srcs = ["conftest.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "some_file_test", | ||
| srcs = ["some_file_test.py"], | ||
| deps = [":conftest"], | ||
| ) | ||
| ``` | ||
|
|
||
| When `# gazelle:include_pytest_conftest false` is found in | ||
| `some_file_test.py` | ||
|
|
||
| ```python | ||
| # some_file_test.py | ||
| # gazelle:include_pytest_conftest false | ||
| ``` | ||
|
|
||
| Gazelle will generate: | ||
|
|
||
| ```starlark | ||
| py_library( | ||
| name = "conftest", | ||
| testonly = True, | ||
| srcs = ["conftest.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "some_file_test", | ||
| srcs = ["some_file_test.py"], | ||
| ) | ||
| ``` | ||
|
|
||
| See {gh-issue}`3076` for more information. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using
versionaddeddirective.See https://github.com/search?q=repo%3Abazel-contrib%2Frules_python+versionadded&type=code for examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I'd forgotten about that directive!
I updated
Added in PR 3080to use it but I opted to keep this one as-is becauseinclude_pytest_conftestsection.