Skip to content

Commit 6a58e1e

Browse files
authored
feat(gazelle): enable pyi attrs by default (bazel-contrib#3753)
Default python_generate_pyi_deps and python_generate_pyi_srcs to true so generated targets preserve type-only dependencies and sibling stub files without extra directives. These directives have been available for many versions now, and defaulting to true better matches the intended semantics of current rules_python versions. Write detailed docs for `python_generate_pyi_deps` since that was missing.
1 parent 83f714d commit 6a58e1e

3 files changed

Lines changed: 50 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ END_UNRELEASED_TEMPLATE
6060
### Changed
6161
* (gazelle) WORKSPACE's bazel-gazelle dependency bumped from 0.36.0 to 0.47.0.
6262
The go version was also bumped from 1.21.13 to 1.22.9.
63+
* (gazelle) `python_generate_pyi_deps` and `python_generate_pyi_srcs` now
64+
default to `true`.
6365
* (pypi) The data files of a wheel (bin, includes, etc) are now always included
6466
as a library's data dependencies.
6567

gazelle/docs/directives.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,18 @@ The Python-specific directives are:
147147
[`# gazelle:python_generate_pyi_deps bool`](#directive-python-generate-pyi-deps)
148148
: Controls whether to generate a separate `pyi_deps` attribute for
149149
type-checking dependencies or merge them into the regular `deps`
150-
attribute. When `false` (default), type-checking dependencies are
151-
merged into `deps` for backward compatibility. When `true`, generates
152-
separate `pyi_deps`. Imports in blocks with the format
150+
attribute. When `true` (default), generates separate `pyi_deps`. When
151+
`false`, type-checking dependencies are merged into `deps`. Imports in
152+
blocks with the format
153153
`if typing.TYPE_CHECKING:` or `if TYPE_CHECKING:` and type-only stub
154154
packages (eg. boto3-stubs) are recognized as type-checking dependencies.
155-
* Default: `false`
155+
* Default: `true`
156156
* Allowed Values: `true`, `false`
157157

158158
[`# gazelle:python_generate_pyi_srcs bool`](#directive-python-generate-pyi-srcs)
159159
: Controls whether to generate a `pyi_srcs` attribute if a sibling `.pyi` file
160-
is found. When `false` (default), the `pyi_srcs` attribute is not added.
161-
* Default: `false`
160+
is found. When `false`, the `pyi_srcs` attribute is not added.
161+
* Default: `true`
162162
* Allowed Values: `true`, `false`
163163

164164
[`# gazelle:python_generate_proto bool`](#directive-python-generate-proto)
@@ -681,10 +681,42 @@ that are relative to the current package.
681681
{gh-pr}`3014`
682682
:::
683683

684-
:::{error}
685-
Detailed docs are not yet written.
684+
:::{versionchanged} VERSION_NEXT_FEATURE
685+
The default was changed from `false` to `true`. {gh-pr}`3753`
686686
:::
687687

688+
When `true`, Gazelle writes type-checking dependencies to the `pyi_deps`
689+
attribute instead of merging them into `deps`. This is the default behavior.
690+
691+
Gazelle treats imports inside `if TYPE_CHECKING:` and
692+
`if typing.TYPE_CHECKING:` blocks as type-checking dependencies. It also adds
693+
type stub packages, such as `boto3-stubs`, to `pyi_deps` when the corresponding
694+
runtime package is imported normally.
695+
696+
For example, assume you have the following file:
697+
698+
```python
699+
import boto3
700+
from typing import TYPE_CHECKING
701+
702+
if TYPE_CHECKING:
703+
import requests
704+
```
705+
706+
The generated target will be:
707+
708+
```starlark
709+
py_library(
710+
name = "foo",
711+
srcs = ["foo.py"],
712+
pyi_deps = ["@pip//requests"],
713+
deps = ["@pip//boto3"],
714+
)
715+
```
716+
717+
When `false`, Gazelle merges type-checking dependencies into `deps` and does
718+
not write `pyi_deps`.
719+
688720

689721
(directive-python-generate-pyi-srcs)=
690722
## `python_generate_pyi_srcs`
@@ -693,6 +725,10 @@ Detailed docs are not yet written.
693725
{gh-pr}`3356`
694726
:::
695727

728+
:::{versionchanged} VERSION_NEXT_FEATURE
729+
The default was changed from `false` to `true`. {gh-pr}`3753`
730+
:::
731+
696732
When `true`, include any sibling `.pyi` files in the `pyi_srcs` target attribute.
697733

698734
For example, assume you have the following files:

gazelle/pythonconfig/pythonconfig.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ const (
102102
ExperimentalAllowRelativeImports = "python_experimental_allow_relative_imports"
103103
// GeneratePyiDeps represents the directive that controls whether to generate
104104
// separate pyi_deps attribute or merge type-checking dependencies into deps.
105-
// Defaults to false for backward compatibility.
105+
// Defaults to true.
106106
GeneratePyiDeps = "python_generate_pyi_deps"
107107
// GeneratePyiSrcs represents the directive that controls whether to include
108108
// a pyi_srcs attribute if a sibling .pyi file is found.
109-
// Defaults to false for backward compatibility.
109+
// Defaults to true.
110110
GeneratePyiSrcs = "python_generate_pyi_srcs"
111111
// GenerateProto represents the directive that controls whether to generate
112112
// python_generate_proto targets.
@@ -256,8 +256,8 @@ func New(
256256
labelConvention: DefaultLabelConvention,
257257
labelNormalization: DefaultLabelNormalizationType,
258258
experimentalAllowRelativeImports: false,
259-
generatePyiDeps: false,
260-
generatePyiSrcs: false,
259+
generatePyiDeps: true,
260+
generatePyiSrcs: true,
261261
generateProto: false,
262262
resolveSiblingImports: false,
263263
includeAncestorConftest: true,

0 commit comments

Comments
 (0)