Skip to content
Merged
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
76 changes: 76 additions & 0 deletions gazelle/docs/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ The Python-specific directives are:
* Default: `true`
* Allowed Values: `true`, `false`

[`# gazelle:python_resolve_pytest_plugins bool`](#directive-python-resolve-pytest-plugins)
: Controls whether module-level `pytest_plugins` declarations are resolved into
target dependencies.
* Default: `true`
* Allowed Values: `true`, `false`

(directive-python-extension)=
## `python_extension`

Expand Down Expand Up @@ -899,3 +905,73 @@ py_test(
],
)
```


(directive-python-resolve-pytest-plugins)=
## `python_resolve_pytest_plugins`

:::{versionadded} VERSION_NEXT_FEATURE
{gh-pr}`4207`
:::

Gazelle resolves static, module-level `pytest_plugins` declarations into
dependencies (see {ref}`gazelle-pytest-plugins`). The
`python_resolve_pytest_plugins` directive controls this behavior. It defaults
to `true`.

Setting the directive to `false` makes Gazelle ignore `pytest_plugins`
declarations in the package and its subpackages: their entries are neither
added as dependencies nor validated. Ordinary imports are unaffected.

For example, with `# gazelle:python_generation_mode file` and these two
Python files in the repository root:

```python
# plugins.py
import pytest
```

```python
# foo_test.py
pytest_plugins = ["plugins"]
```

`# gazelle:python_resolve_pytest_plugins true` (the default) results in:

```starlark
# gazelle:python_generation_mode file
# gazelle:python_resolve_pytest_plugins true

py_library(
name = "plugins",
srcs = ["plugins.py"],
visibility = ["//:__subpackages__"],
deps = ["@pip//pytest"],
)

py_test(
name = "foo_test",
srcs = ["foo_test.py"],
deps = [":plugins"],
)
```

And `# gazelle:python_resolve_pytest_plugins false` results in no dependency
on `:plugins`:

```starlark
# gazelle:python_generation_mode file
# gazelle:python_resolve_pytest_plugins false

py_library(
name = "plugins",
srcs = ["plugins.py"],
visibility = ["//:__subpackages__"],
deps = ["@pip//pytest"],
)

py_test(
name = "foo_test",
srcs = ["foo_test.py"],
)
```
Comment thread
dougthor42 marked this conversation as resolved.
Resolved
29 changes: 29 additions & 0 deletions gazelle/docs/installation_and_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,35 @@ You can control the naming convention for test targets using the
{term}`# gazelle:python_test_naming_convention value` directive.


(gazelle-pytest-plugins)=
#### Pytest plugins

Gazelle resolves module-level `pytest_plugins` declarations into dependencies in
tests, root conftests, and reusable plugin modules:

```python
pytest_plugins = ["myapp.testing.database", "myapp.testing.http"]
```

Literal strings, lists, and tuples are supported, including annotated assignments
and pytest's comma-separated string form. Existing resolution directives and
dependency-ignore annotations apply as they do to normal imports.
Built-in plugin aliases such as `pytester`, `capture`, and `logging` resolve to
their `_pytest` modules from the pytest distribution. Ignores and resolution
overrides still match the declared alias. Ordinary imports are not remapped.

Conditional declarations are included conservatively. Function/class-local
declarations are ignored. Literal entries in mixed lists are resolved, but dynamic
values, bytes, f-strings, and named Unicode escapes are not evaluated; use
{ref}`annotation-include-dep` for their dependencies. This does not change pytest's
invocation-wide plugin registration or its restriction on non-root conftests.

Set the {term}`# gazelle:python_resolve_pytest_plugins bool` directive to `false`
to turn this off for a package and its subpackages.

:::{versionadded} VERSION_NEXT_FEATURE
:::

### Binaries

When a `__main__.py` file is encountered, this indicates the entry point
Expand Down
1 change: 1 addition & 0 deletions gazelle/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ go_test(
],
embed = [":python"],
deps = [
"@com_github_emirpasic_gods//sets/treeset:go_default_library",
"@com_github_stretchr_testify//assert",
],
)
7 changes: 7 additions & 0 deletions gazelle/python/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (py *Configurer) KnownDirectives() []string {
pythonconfig.GenerateProto,
pythonconfig.PythonResolveSiblingImports,
pythonconfig.PythonIncludeAncestorConftest,
pythonconfig.PythonResolvePytestPlugins,
}
}

Expand Down Expand Up @@ -268,6 +269,12 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
log.Fatal(err)
}
config.SetIncludeAncestorConftest(v)
case pythonconfig.PythonResolvePytestPlugins:
v, err := strconv.ParseBool(strings.TrimSpace(d.Value))
if err != nil {
log.Fatal(err)
}
config.SetResolvePytestPlugins(v)
}
}

Expand Down
174 changes: 172 additions & 2 deletions gazelle/python/file_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"log"
"os"
"path/filepath"
"strconv"
"strings"

bzl "github.com/bazelbuild/buildtools/build"
sitter "github.com/tree-sitter/go-tree-sitter"
tree_sitter_python "github.com/tree-sitter/tree-sitter-python/bindings/go"
)
Expand Down Expand Up @@ -51,6 +53,7 @@ type FileParser struct {
relFilepath string
output ParserOutput
inTypeCheckingBlock bool
inLocalScope bool
}

func NewFileParser() *FileParser {
Expand Down Expand Up @@ -256,13 +259,181 @@ func (p *FileParser) isTypeCheckingBlock(node *sitter.Node) bool {
return false
}

// pytestPluginExpression unwraps parentheses, ignoring comments before the value.
func pytestPluginExpression(node *sitter.Node) *sitter.Node {
for node != nil && node.Kind() == "parenthesized_expression" {
var expression *sitter.Node
for i := uint(0); i < node.NamedChildCount(); i++ {
child := node.NamedChild(i)
if child.Kind() != sitterNodeTypeComment {
expression = child
break
}
}
node = expression
}
return node
}

// pytestPluginString evaluates literal Python strings without executing Python.
// Dynamic expressions, bytes, f-strings, and named Unicode escapes are not evaluated.
func pytestPluginString(node *sitter.Node, code []byte) (string, bool) {
node = pytestPluginExpression(node)
if node == nil {
return "", false
}
if node.Kind() == "concatenated_string" {
var value strings.Builder
for i := uint(0); i < node.NamedChildCount(); i++ {
child := node.NamedChild(i)
if child.Kind() == sitterNodeTypeComment {
continue
}
part, ok := pytestPluginString(child, code)
if !ok {
return "", false
}
value.WriteString(part)
}
return value.String(), true
}
if node.Kind() != sitterNodeTypeString || node.HasError() {
return "", false
}
literal := node.Utf8Text(code)
quote := strings.IndexAny(literal, "\"'")
if quote < 0 {
return "", false
}
prefix := strings.ToLower(literal[:quote])
if prefix != "" && prefix != "r" && prefix != "u" {
return "", false
}
// Python normalizes source newlines even inside raw string literals.
content := strings.ReplaceAll(literal[quote:], "\r\n", "\n")
content = strings.ReplaceAll(content, "\r", "\n")
if prefix == "r" {
value, _, err := bzl.Unquote("r" + content)
return value, err == nil
}
if !strings.Contains(content, "\\") {
value, _, err := bzl.Unquote(content)
return value, err == nil
}
// Buildtools already handles Python-style quotes and escapes. Normalize
// newlines and byte escapes to preserve Python 3's Unicode string semantics:
// buildtools interprets hex and octal escapes as bytes instead of code points.
var normalized strings.Builder
for len(content) > 0 {
if content[0] != '\\' {
normalized.WriteByte(content[0])
content = content[1:]
continue
}
if len(content) < 2 {
return "", false
}
if content[1] == 'N' {
return "", false
}
if content[1] == 'x' || (content[1] >= '0' && content[1] <= '7') {
end := 2
base := 8
start := 1
if content[1] == 'x' {
end, start, base = 4, 2, 16
if len(content) < end {
return "", false
}
} else {
for end < len(content) && end < 4 && content[end] >= '0' && content[end] <= '7' {
end++
}
}
r, err := strconv.ParseUint(content[start:end], base, 16)
if err != nil {
return "", false
}
fmt.Fprintf(&normalized, "\\u%04x", r)
content = content[end:]
continue
}
normalized.WriteString(content[:2])
content = content[2:]
}
value, _, err := bzl.Unquote(normalized.String())
return value, err == nil
}

// parsePytestPlugins treats static module-level pytest_plugins declarations as
// imports. As with conditional imports, all statically declared alternatives are
// included; Python code is never executed to determine the active branch.
func (p *FileParser) parsePytestPlugins(node *sitter.Node) {
if p.inLocalScope || node.Kind() != "assignment" {
return
}
left := node.ChildByFieldName("left")
if left == nil || left.Kind() != sitterNodeTypeIdentifier || left.Utf8Text(p.code) != "pytest_plugins" {
return
}
right := node.ChildByFieldName("right")
for right != nil && right.Kind() == "assignment" {
right = right.ChildByFieldName("right")
}
right = pytestPluginExpression(right)
if right == nil || right.HasError() {
return
}
add := func(value string, node *sitter.Node) {
if value != "" {
p.output.Modules = append(p.output.Modules, Module{
Name: value,
Comment thread
TrapsterDK marked this conversation as resolved.
Resolved
LineNumber: uint32(node.StartPosition().Row) + 1,
Filepath: p.relFilepath,
TypeCheckingOnly: p.inTypeCheckingBlock,
PytestPlugin: true,
})
}
}
if right.Kind() == "list" || right.Kind() == "tuple" || right.Kind() == "expression_list" {
for i := uint(0); i < right.NamedChildCount(); i++ {
child := right.NamedChild(i)
if child.Kind() == sitterNodeTypeComment {
continue
}
value, ok := pytestPluginString(child, p.code)
if !ok {
continue
}
add(value, child)
}
} else {
value, ok := pytestPluginString(right, p.code)
if !ok {
return
}
// Pytest also accepts comma-separated module names in a single string.
for _, name := range strings.Split(value, ",") {
add(name, right)
}
}
}

func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
if node == nil {
return
}

// Check if this is a TYPE_CHECKING block
wasInTypeCheckingBlock := p.inTypeCheckingBlock
wasInLocalScope := p.inLocalScope
defer func() {
p.inTypeCheckingBlock = wasInTypeCheckingBlock
p.inLocalScope = wasInLocalScope
}()
if node.Kind() == "function_definition" || node.Kind() == "class_definition" || node.Kind() == "lambda" {
p.inLocalScope = true
}
if p.isTypeCheckingBlock(node) {
p.inTypeCheckingBlock = true
}
Expand All @@ -272,6 +443,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
return
}
child := node.Child(i)
p.parsePytestPlugins(child)
if p.parseImportStatements(child) {
continue
}
Expand All @@ -281,8 +453,6 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
p.parse(ctx, child)
}

// Restore the previous state
p.inTypeCheckingBlock = wasInTypeCheckingBlock
}

func (p *FileParser) Parse(ctx context.Context) (*ParserOutput, error) {
Expand Down
Loading
Loading