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
1 change: 1 addition & 0 deletions gazelle/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ real_source_target(
aspect_hints = ["//quality:corpus_vale_config"],
markdown_srcs = [
"testdata/entries/README.md",
"testdata/pytest_plugins/README.md",
"testdata/source_only/README.md",
],
tags = ["corpus"],
Expand Down
131 changes: 130 additions & 1 deletion gazelle/python/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
var grammar = sync.OnceValue(func() *ts.Language { return python.Language() })

// ParseImports returns the sorted, deduplicated top-level module names
// imported by content, including import_module("x") calls. Relative imports
// imported by content, including import_module("x") calls and module-level
// pytest_plugins entries. Relative imports
// are skipped.
func ParseImports(content []byte) []string {
tree := treesitter.Parse(grammar(), content)
Expand Down Expand Up @@ -54,6 +55,7 @@ func ParseImports(content []byte) []string {
}
return true
})
addPytestPlugins(tree, add)
}
out := make([]string, 0, len(set))
for name := range set {
Expand Down Expand Up @@ -111,3 +113,130 @@ func addDynamic(tree *treesitter.Tree, call *ts.Node, add func(string)) {
return
}
}

// pytestBuiltinPlugins mirrors builtin_plugins in _pytest/config/__init__.py.
// python_test already provides pytest, so these aliases need no dep.
var pytestBuiltinPlugins = map[string]bool{
"mark": true, "main": true, "runner": true, "fixtures": true, "helpconfig": true,
"python": true, "terminal": true, "debugging": true, "unittest": true, "capture": true,
"skipping": true, "legacypath": true, "tmpdir": true, "monkeypatch": true, "recwarn": true,
"pastebin": true, "assertion": true, "junitxml": true, "doctest": true, "cacheprovider": true,
"setuponly": true, "setupplan": true, "stepwise": true, "unraisableexception": true,
"threadexception": true, "warnings": true, "logging": true, "reports": true,
"faulthandler": true, "subtests": true, "pytester": true, "pytester_assertions": true,
"terminalprogress": true, "nose": true, "freeze_support": true,
}

// addPytestPlugins adds the static entries of module-level pytest_plugins
// assignments. Function- and class-local assignments are skipped.
func addPytestPlugins(tree *treesitter.Tree, add func(string)) {
addName := func(name string) {
name = strings.TrimSpace(name)
if name == "" || strings.HasPrefix(name, ".") || pytestBuiltinPlugins[name] {
return
}
add(name)
}
tree.Walk(tree.Root, func(n *ts.Node) bool {
switch tree.Type(n) {
case "function_definition", "class_definition", "lambda":
return false
case "assignment":
default:
return true
}
left := n.ChildByFieldName("left", grammar())
if left == nil || tree.Type(left) != "identifier" || tree.Text(left) != "pytest_plugins" {
return true
}
right := n.ChildByFieldName("right", grammar())
for right != nil && tree.Type(right) == "assignment" {
right = right.ChildByFieldName("right", grammar())
}
right = unparen(tree, right)
if right == nil || right.HasError() {
return true
}
switch tree.Type(right) {
case "list", "tuple", "expression_list":
for _, c := range right.Children() {
if !c.IsNamed() {
continue
}
if v, ok := pythonString(tree, c); ok {
addName(v)
}
}
default:
if v, ok := pythonString(tree, right); ok {
for _, name := range strings.Split(v, ",") {
addName(name)
}
}
}
return true
})
}

func unparen(tree *treesitter.Tree, n *ts.Node) *ts.Node {
for n != nil && tree.Type(n) == "parenthesized_expression" {
var inner *ts.Node
for _, c := range n.Children() {
if c.IsNamed() && tree.Type(c) != "comment" {
inner = c
break
}
}
n = inner
}
return n
}

// pythonString returns the value of a plain, raw, or unicode string literal or
// an implicit concatenation of them. Bytes, f-strings, and escapes are rejected.
func pythonString(tree *treesitter.Tree, n *ts.Node) (string, bool) {
n = unparen(tree, n)
if n == nil || n.HasError() {
return "", false
}
switch tree.Type(n) {
case "concatenated_string":
var b strings.Builder
for _, c := range n.Children() {
if !c.IsNamed() || tree.Type(c) == "comment" {
continue
}
part, ok := pythonString(tree, c)
if !ok {
return "", false
}
b.WriteString(part)
}
return b.String(), true
case "string":
default:
return "", false
}
s := tree.Text(n)
quote := strings.IndexAny(s, `"'`)
if quote < 0 {
return "", false
}
prefix := strings.ToLower(s[:quote])
if prefix != "" && prefix != "r" && prefix != "u" {
return "", false
}
s = s[quote:]
switch {
case len(s) >= 6 && (strings.HasPrefix(s, `"""`) || strings.HasPrefix(s, `'''`)) && strings.HasSuffix(s, s[:3]):
s = s[3 : len(s)-3]
case len(s) >= 2 && s[len(s)-1] == s[0]:
s = s[1 : len(s)-1]
default:
return "", false
}
if prefix != "r" && strings.Contains(s, `\`) {
return "", false
}
return s, true
}
29 changes: 29 additions & 0 deletions gazelle/python/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ func TestParseImports(t *testing.T) {
{"dynamicTripleUnterminated", "import_module(\"\"\"abc)\n", nil},
{"dynamicEmpty", "import_module(\"\")\n", nil},
{"dynamicRelative", "import_module(\".hidden\")\n", nil},
{"pluginsList", "pytest_plugins = [\"a.fix\", 'b']\n", []string{"a", "b"}},
{"pluginsString", "pytest_plugins = \"a.fix\"\n", []string{"a"}},
{"pluginsCommaString", "pytest_plugins = \"a.fix, b.more,,\"\n", []string{"a", "b"}},
{"pluginsTuple", "pytest_plugins = (\"a\", \"b\")\n", []string{"a", "b"}},
{"pluginsBareTuple", "pytest_plugins = \"a\", \"b\"\n", []string{"a", "b"}},
{"pluginsSingleTuple", "pytest_plugins = (\"a\",)\n", []string{"a"}},
{"pluginsParen", "pytest_plugins = (\n # db\n \"a\"\n)\n", []string{"a"}},
{"pluginsAnnotated", "pytest_plugins: list[str] = [\"a\"]\n", []string{"a"}},
{"pluginsAnnotatedOnly", "pytest_plugins: list[str]\n", nil},
{"pluginsChained", "pytest_plugins = other = [\"a\"]\n", []string{"a"}},
{"pluginsChainedInner", "other = pytest_plugins = [\"a\"]\n", []string{"a"}},
{"pluginsConcat", "pytest_plugins = [\"pkg.\" \"fix\", (\"b\"\n \"c\")]\n", []string{"bc", "pkg"}},
{"pluginsConcatDynamic", "pytest_plugins = [\"a\" f\"{x}\"]\n", nil},
{"pluginsMixed", "pytest_plugins = [\"a\", name, \"b\" + c, *rest, \"d\"]\n", []string{"a", "d"}},
{"pluginsRawUnicode", "pytest_plugins = [r\"a\", u'b', R'''c''']\n", []string{"a", "b", "c"}},
{"pluginsRejected", "pytest_plugins = [b\"a\", f\"b\", \"c\\x64\"]\n", nil},
{"pluginsMultiline", "pytest_plugins = [\n \"a\", # first\n \"b\",\n]\n", []string{"a", "b"}},
{"pluginsConditional", "if x:\n pytest_plugins = [\"a\"]\nelse:\n pytest_plugins = [\"b\"]\n", []string{"a", "b"}},
{"pluginsTry", "try:\n pytest_plugins = [\"a\"]\nexcept E:\n pass\n", []string{"a"}},
{"pluginsFunction", "def f():\n pytest_plugins = [\"a\"]\n", nil},
{"pluginsDecorated", "@d\ndef f():\n pytest_plugins = [\"a\"]\n", nil},
{"pluginsClass", "class C:\n pytest_plugins = [\"a\"]\n", nil},
{"pluginsAttribute", "mod.pytest_plugins = [\"a\"]\n", nil},
{"pluginsOtherName", "plugins = [\"a\"]\n", nil},
{"pluginsDynamic", "pytest_plugins = make()\n", nil},
{"pluginsRelative", "pytest_plugins = [\".local\"]\n", nil},
{"pluginsBuiltin", "pytest_plugins = [\"pytester\", \"capture\", \"a\"]\n", []string{"a"}},
{"pluginsBuiltinDotted", "pytest_plugins = \"logging.sub\"\n", []string{"logging"}},
{"pluginsWithImports", "import os\nimport a\npytest_plugins = [\"a.x\", \"b\"]\n", []string{"a", "b", "os"}},
}
for _, tc := range cases {
if got := ParseImports([]byte(tc.source)); !reflect.DeepEqual(got, tc.want) {
Expand Down
5 changes: 5 additions & 0 deletions gazelle/python/testdata/pytest_plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Pytest plugin declarations

Verifies module-level `pytest_plugins` entries in tests and in `conftest.py`
files at any level become deps, with resolve and ignore directives applied.
Function-local declarations and built-in pytest plugins add no deps.
1 change: 1 addition & 0 deletions gazelle/python/testdata/pytest_plugins/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:resolve python mapped_plugin //pkg/vendor:mapped
# gazelle:dx_ignore_import python ignored_plugin
51 changes: 51 additions & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
load("@rules_dx//python/rules:defs.bzl", "python_library", "python_test")

# gazelle:resolve python mapped_plugin //pkg/vendor:mapped
# gazelle:dx_ignore_import python ignored_plugin

python_test(
name = "builtin_test",
srcs = ["builtin_test.py"],
imports = ["."],
)

python_library(
name = "capture",
srcs = ["capture.py"],
imports = ["."],
)

python_library(
name = "conftest",
srcs = ["conftest.py"],
imports = ["."],
deps = [":database_fixtures"],
)

python_library(
name = "database_fixtures",
srcs = ["database_fixtures.py"],
imports = ["."],
)

python_library(
name = "http_fixtures",
srcs = ["http_fixtures.py"],
imports = ["."],
)

python_test(
name = "local_test",
srcs = ["local_test.py"],
imports = ["."],
)

python_test(
name = "plugin_test",
srcs = ["plugin_test.py"],
imports = ["."],
deps = [
":http_fixtures",
"//pkg/vendor:mapped",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Built-in plugin aliases resolve to pytest, not to local modules."""

pytest_plugins = "capture, logging"


def test_builtin():
assert pytest_plugins
1 change: 1 addition & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/demo/capture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Local module that shares a name with a built-in pytest plugin."""
3 changes: 3 additions & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/demo/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Root conftest that loads shared fixtures and a built-in plugin."""

pytest_plugins = ["database_fixtures", "pytester"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Database fixtures plugin."""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""HTTP fixtures plugin."""
10 changes: 10 additions & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/demo/local_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Function-local declarations are not read by pytest."""


def helper():
pytest_plugins = ["http_fixtures"]
return pytest_plugins


def test_local():
assert helper()
14 changes: 14 additions & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/demo/plugin_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Annotated tuple with mapped, ignored, and dynamic entries."""

import os

pytest_plugins: tuple[str, ...] = (
"http_fixtures",
"mapped_plugin",
"ignored_plugin",
os.environ.get("EXTRA_PLUGIN", ""),
)


def test_plugins():
assert pytest_plugins
20 changes: 20 additions & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/demo/sub/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@rules_dx//python/rules:defs.bzl", "python_library", "python_test")

python_library(
name = "conftest",
srcs = ["conftest.py"],
imports = ["."],
deps = [":deep_fixtures"],
)

python_library(
name = "deep_fixtures",
srcs = ["deep_fixtures.py"],
imports = ["."],
)

python_test(
name = "sub_test",
srcs = ["sub_test.py"],
imports = ["."],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Nested conftest with a comma-separated, implicitly concatenated string."""

pytest_plugins = "deep_" "fixtures"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Nested fixtures plugin."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Test beside the nested conftest."""


def test_sub():
assert True
4 changes: 4 additions & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.in

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
filegroup(
name = "mapped",
srcs = [],
)
Loading