Skip to content

feat(gazelle): resolve pytest_plugins declarations as dependencies - #4207

Open
TrapsterDK wants to merge 5 commits into
bazel-contrib:mainfrom
TrapsterDK:feat/gazelle-pytest-plugins
Open

TrapsterDK wants to merge 5 commits into
bazel-contrib:mainfrom
TrapsterDK:feat/gazelle-pytest-plugins

Conversation

@TrapsterDK

@TrapsterDK TrapsterDK commented Oct 1, 2026 •

Copy link
Copy Markdown

AI written, human reviewed.

Summary

Pytest loads reusable fixture modules through pytest_plugins, but Gazelle
currently discovers only ordinary imports. Users must therefore duplicate plugin
dependencies in BUILD files or gazelle:include_dep annotations.

Treat static module-level pytest_plugins assignments as imports and reuse the
existing dependency resolver. Support literal strings, lists, tuples, annotated
and chained assignments, implicit string concatenation, and common Python string
escapes. Reuse buildtools' existing unquoting with narrow Python 3 normalization.
Match pytest's comma-separated single-string form and retain literal dependencies
when lists or tuples also contain dynamic entries.

Preserve dependency-ignore annotations, resolution directives, source locations,
and type-checking metadata. Exclude function/class-local declarations and document
unsupported dynamic expressions. Conditional declarations are conservatively
included, as with ordinary conditional imports.

Add parser edge-case tests and BUILD-generation regressions covering reusable
plugin prerequisites, a workspace-root conftest, external modules, duplicate dependencies,
resolution overrides, ignore behavior, and unrelated sibling test isolation.

Fixes #4196.

Verification

From gazelle/:

  • bazel test --config=fast-tests //... — all 87 test targets pass.
  • gofmt check on the modified Go files — clean.
  • git diff --check — clean.

This changes dependency generation only; pytest's invocation-wide plugin
registration behavior is unchanged. Dynamic declarations and named Unicode
escapes remain unsupported and can use the existing explicit dependency
annotation.

@TrapsterDK
TrapsterDK marked this pull request as ready for review October 5, 2026 11:37
Copilot AI balanced review requested due to automatic review settings October 5, 2026 11:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Built-in plugin aliases such as pytester cause dependency-validation failures under default settings.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Adds Gazelle dependency discovery for static pytest_plugins declarations, reducing manual dependency annotations and addressing #4196.

Changes:

  • Parses supported literal declarations using the existing dependency resolver.
  • Adds parser and BUILD-generation regressions.
  • Documents supported syntax and limitations.
File Description
news/​4196.added.md Announces plugin dependency support.
gazelle/​python/​testdata/​pytest_plugins/​WORKSPACE Defines fixture workspace.
gazelle/​python/​testdata/​pytest_plugins/​test.yaml Specifies successful generation.
gazelle/​python/​testdata/​pytest_plugins/​support/​database.py Exercises plugin prerequisites.
gazelle/​python/​testdata/​pytest_plugins/​support/​BUILD.out Records prerequisite dependencies.
gazelle/​python/​testdata/​pytest_plugins/​support/​BUILD.in Initializes support package.
gazelle/​python/​testdata/​pytest_plugins/​support/​admin.py Supplies prerequisite fixture.
gazelle/​python/​testdata/​pytest_plugins/​README.md Explains regression scope.
gazelle/​python/​testdata/​pytest_plugins/​plugin_test.py Exercises declarations and resolution controls.
gazelle/​python/​testdata/​pytest_plugins/​isolated_test.py Checks sibling isolation.
gazelle/​python/​testdata/​pytest_plugins/​gazelle_python.yaml Maps external dependencies.
gazelle/​python/​testdata/​pytest_plugins/​BUILD.out Records expected test dependencies.
gazelle/​python/​testdata/​pytest_plugins/​BUILD.in Configures resolution directives.
gazelle/​python/​testdata/​pytest_plugins_conftest/​WORKSPACE Defines conftest fixture workspace.
gazelle/​python/​testdata/​pytest_plugins_conftest/​test.yaml Specifies successful generation.
gazelle/​python/​testdata/​pytest_plugins_conftest/​README.md Explains root-conftest coverage.
gazelle/​python/​testdata/​pytest_plugins_conftest/​gazelle_python.yaml Maps pytest dependency.
gazelle/​python/​testdata/​pytest_plugins_conftest/​fixtures.py Supplies reusable fixture.
gazelle/​python/​testdata/​pytest_plugins_conftest/​consumer_test.py Consumes conftest-provided fixture.
gazelle/​python/​testdata/​pytest_plugins_conftest/​conftest.py Declares root plugin.
gazelle/​python/​testdata/​pytest_plugins_conftest/​BUILD.out Records transitive dependencies.
gazelle/​python/​testdata/​pytest_plugins_conftest/​BUILD.in Enables per-file generation.
gazelle/​python/​file_parser.go Extracts static plugin declarations.
gazelle/​python/​file_parser_test.go Covers syntax, scope, and metadata.
gazelle/​docs/​installation_and_usage.md Documents support and limitations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread gazelle/python/file_parser.go

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Support parsing pytest_plugins as a form of import.

2 participants