Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ END_UNRELEASED_TEMPLATE
### Changed
* (gazelle) WORKSPACE's bazel-gazelle dependency bumped from 0.36.0 to 0.47.0.
The go version was also bumped from 1.21.13 to 1.22.9.
* (gazelle) `python_generate_pyi_deps` and `python_generate_pyi_srcs` now
default to `true`.
* (pypi) The data files of a wheel (bin, includes, etc) are now always included
as a library's data dependencies.

Expand Down
46 changes: 37 additions & 9 deletions gazelle/docs/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,18 @@ The Python-specific directives are:
[`# gazelle:python_generate_pyi_deps bool`](#directive-python-generate-pyi-deps)
: 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
attribute. When `true` (default), generates separate `pyi_deps`. When
`false`, type-checking dependencies are merged into `deps`. Imports in
blocks with the format
`if typing.TYPE_CHECKING:` or `if TYPE_CHECKING:` and type-only stub
packages (eg. boto3-stubs) are recognized as type-checking dependencies.
* Default: `false`
* Default: `true`
* Allowed Values: `true`, `false`

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

[`# gazelle:python_generate_proto bool`](#directive-python-generate-proto)
Expand Down Expand Up @@ -681,9 +681,37 @@ that are relative to the current package.
{gh-pr}`3014`
:::

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the docs!

Please add a versionchanged attribute:

:::{versionadded} 1.6.0
{gh-pr}`3014`
:::

:::{versionchanged} VERSION_NEXT_FEATURE
The default was changed from `false` to `true`. {gh-pr}`3753`
:::

When `true`, Gazelle ...

:::{error}
Detailed docs are not yet written.
:::
When `true`, Gazelle writes type-checking dependencies to the `pyi_deps`
attribute instead of merging them into `deps`. This is the default behavior.

Gazelle treats imports inside `if TYPE_CHECKING:` and
`if typing.TYPE_CHECKING:` blocks as type-checking dependencies. It also adds
type stub packages, such as `boto3-stubs`, to `pyi_deps` when the corresponding
runtime package is imported normally.

For example, assume you have the following file:

```python
import boto3
from typing import TYPE_CHECKING

if TYPE_CHECKING:
import requests
```

The generated target will be:

```starlark
py_library(
name = "foo",
srcs = ["foo.py"],
pyi_deps = ["@pip//requests"],
deps = ["@pip//boto3"],
)
```

When `false`, Gazelle merges type-checking dependencies into `deps` and does
not write `pyi_deps`.


(directive-python-generate-pyi-srcs)=
Expand Down
8 changes: 4 additions & 4 deletions gazelle/pythonconfig/pythonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ const (
ExperimentalAllowRelativeImports = "python_experimental_allow_relative_imports"
// GeneratePyiDeps represents the directive that controls whether to generate
// separate pyi_deps attribute or merge type-checking dependencies into deps.
// Defaults to false for backward compatibility.
// Defaults to true.
GeneratePyiDeps = "python_generate_pyi_deps"
// GeneratePyiSrcs represents the directive that controls whether to include
// a pyi_srcs attribute if a sibling .pyi file is found.
// Defaults to false for backward compatibility.
// Defaults to true.
GeneratePyiSrcs = "python_generate_pyi_srcs"
// GenerateProto represents the directive that controls whether to generate
// python_generate_proto targets.
Expand Down Expand Up @@ -256,8 +256,8 @@ func New(
labelConvention: DefaultLabelConvention,
labelNormalization: DefaultLabelNormalizationType,
experimentalAllowRelativeImports: false,
generatePyiDeps: false,
generatePyiSrcs: false,
generatePyiDeps: true,
generatePyiSrcs: true,
generateProto: false,
resolveSiblingImports: false,
includeAncestorConftest: true,
Expand Down