From e54d4842395d6df00b864d0aa0c1dace57836d3e Mon Sep 17 00:00:00 2001 From: Martin Kuntz Jacobsen Date: Wed, 28 Oct 2026 13:35:06 +0100 Subject: [PATCH] gazelle/python: resolve pytest_plugins declarations as deps Module-level static pytest_plugins assignments in tests and conftest.py files at any level now add their modules as imports, mirroring bazel-contrib/rules_python#4207. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011u9TXHDZrfmzFQ2WT8QAKK --- gazelle/python/BUILD.bazel | 1 + gazelle/python/parser.go | 131 +++++++++++++++++- gazelle/python/parser_test.go | 29 ++++ .../python/testdata/pytest_plugins/README.md | 5 + .../python/testdata/pytest_plugins/WORKSPACE | 1 + .../testdata/pytest_plugins/pkg/demo/BUILD.in | 2 + .../pytest_plugins/pkg/demo/BUILD.out | 51 +++++++ .../pytest_plugins/pkg/demo/builtin_test.py | 7 + .../pytest_plugins/pkg/demo/capture.py | 1 + .../pytest_plugins/pkg/demo/conftest.py | 3 + .../pkg/demo/database_fixtures.py | 1 + .../pytest_plugins/pkg/demo/http_fixtures.py | 1 + .../pytest_plugins/pkg/demo/local_test.py | 10 ++ .../pytest_plugins/pkg/demo/plugin_test.py | 14 ++ .../pytest_plugins/pkg/demo/sub/BUILD.out | 20 +++ .../pytest_plugins/pkg/demo/sub/conftest.py | 3 + .../pkg/demo/sub/deep_fixtures.py | 1 + .../pytest_plugins/pkg/demo/sub/sub_test.py | 5 + .../pytest_plugins/pkg/vendor/BUILD.in | 4 + .../pytest_plugins/pkg/vendor/BUILD.out | 4 + 20 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 gazelle/python/testdata/pytest_plugins/README.md create mode 100644 gazelle/python/testdata/pytest_plugins/WORKSPACE create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.in create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.out create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/builtin_test.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/capture.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/conftest.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/database_fixtures.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/http_fixtures.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/local_test.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/plugin_test.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/sub/BUILD.out create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/sub/conftest.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/sub/deep_fixtures.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/demo/sub/sub_test.py create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.in create mode 100644 gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.out diff --git a/gazelle/python/BUILD.bazel b/gazelle/python/BUILD.bazel index aad70fe3b2..19c46997d7 100644 --- a/gazelle/python/BUILD.bazel +++ b/gazelle/python/BUILD.bazel @@ -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"], diff --git a/gazelle/python/parser.go b/gazelle/python/parser.go index 20f6eeb7cc..aac03f82eb 100644 --- a/gazelle/python/parser.go +++ b/gazelle/python/parser.go @@ -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) @@ -54,6 +55,7 @@ func ParseImports(content []byte) []string { } return true }) + addPytestPlugins(tree, add) } out := make([]string, 0, len(set)) for name := range set { @@ -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 +} diff --git a/gazelle/python/parser_test.go b/gazelle/python/parser_test.go index cdd9d68fe5..06d5b582de 100644 --- a/gazelle/python/parser_test.go +++ b/gazelle/python/parser_test.go @@ -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) { diff --git a/gazelle/python/testdata/pytest_plugins/README.md b/gazelle/python/testdata/pytest_plugins/README.md new file mode 100644 index 0000000000..17d5fffa16 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/README.md @@ -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. diff --git a/gazelle/python/testdata/pytest_plugins/WORKSPACE b/gazelle/python/testdata/pytest_plugins/WORKSPACE new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/WORKSPACE @@ -0,0 +1 @@ + diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.in b/gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.in new file mode 100644 index 0000000000..c4f6645f8e --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.in @@ -0,0 +1,2 @@ +# gazelle:resolve python mapped_plugin //pkg/vendor:mapped +# gazelle:dx_ignore_import python ignored_plugin diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.out b/gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.out new file mode 100644 index 0000000000..65311c784d --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/BUILD.out @@ -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", + ], +) diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/builtin_test.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/builtin_test.py new file mode 100644 index 0000000000..87600c798c --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/builtin_test.py @@ -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 diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/capture.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/capture.py new file mode 100644 index 0000000000..8a46e99617 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/capture.py @@ -0,0 +1 @@ +"""Local module that shares a name with a built-in pytest plugin.""" diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/conftest.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/conftest.py new file mode 100644 index 0000000000..081b938f9e --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/conftest.py @@ -0,0 +1,3 @@ +"""Root conftest that loads shared fixtures and a built-in plugin.""" + +pytest_plugins = ["database_fixtures", "pytester"] diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/database_fixtures.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/database_fixtures.py new file mode 100644 index 0000000000..e117c0a66e --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/database_fixtures.py @@ -0,0 +1 @@ +"""Database fixtures plugin.""" diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/http_fixtures.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/http_fixtures.py new file mode 100644 index 0000000000..330c560378 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/http_fixtures.py @@ -0,0 +1 @@ +"""HTTP fixtures plugin.""" diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/local_test.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/local_test.py new file mode 100644 index 0000000000..d3db7aee2c --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/local_test.py @@ -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() diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/plugin_test.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/plugin_test.py new file mode 100644 index 0000000000..0125b5b857 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/plugin_test.py @@ -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 diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/BUILD.out b/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/BUILD.out new file mode 100644 index 0000000000..58b7ceb1d4 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/BUILD.out @@ -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 = ["."], +) diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/conftest.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/conftest.py new file mode 100644 index 0000000000..550a1e85d8 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/conftest.py @@ -0,0 +1,3 @@ +"""Nested conftest with a comma-separated, implicitly concatenated string.""" + +pytest_plugins = "deep_" "fixtures" diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/deep_fixtures.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/deep_fixtures.py new file mode 100644 index 0000000000..c751f18cab --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/deep_fixtures.py @@ -0,0 +1 @@ +"""Nested fixtures plugin.""" diff --git a/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/sub_test.py b/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/sub_test.py new file mode 100644 index 0000000000..e7a32320c9 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/demo/sub/sub_test.py @@ -0,0 +1,5 @@ +"""Test beside the nested conftest.""" + + +def test_sub(): + assert True diff --git a/gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.in b/gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.in new file mode 100644 index 0000000000..aabe6e9804 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.in @@ -0,0 +1,4 @@ +filegroup( + name = "mapped", + srcs = [], +) diff --git a/gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.out b/gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.out new file mode 100644 index 0000000000..aabe6e9804 --- /dev/null +++ b/gazelle/python/testdata/pytest_plugins/pkg/vendor/BUILD.out @@ -0,0 +1,4 @@ +filegroup( + name = "mapped", + srcs = [], +)