Skip to content

Conversation

@asukaminato0721
Copy link
Contributor

@asukaminato0721 asukaminato0721 commented Jan 12, 2026

Summary

Fixes #1901

Implemented pytest import/alias detection plus fixture discovery and parameter scanning in pyrefly/lib/state/lsp.rs, and hooked it into go‑to‑definition and local references.

todo: should these pytest functions in their own mod?

Test Plan

cargo test pytest_fixture_parameter_goes_to_fixture_definition

cargo test pytest_fixture_references_include_injected_parameters

@meta-cla meta-cla bot added the cla signed label Jan 12, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review January 12, 2026 13:14
Copilot AI review requested due to automatic review settings January 12, 2026 13:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements LSP navigation support for pytest fixtures, enabling go-to-definition and find-references functionality for fixture parameters in test functions. When a developer invokes go-to-definition on a fixture parameter (e.g., my_fixture in def test_thing(my_fixture):), the LSP will navigate to the fixture definition. Similarly, finding references on a fixture definition will include all test/fixture functions that use it as a parameter.

Changes:

  • Added pytest import and fixture detection logic to identify pytest modules and fixture decorators
  • Implemented fixture discovery and parameter scanning to collect fixture definitions and their usage in test functions
  • Integrated pytest fixture resolution into LSP go-to-definition and find-references operations

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
pyrefly/lib/state/lsp.rs Core implementation: added PytestAliases tracking, fixture detection functions, and integrated pytest-aware parameter resolution into LSP operations
pyrefly/lib/test/lsp/definition.rs Added test verifying go-to-definition navigation from fixture parameters to fixture definitions
pyrefly/lib/test/lsp/local_find_refs.rs Added test verifying find-references includes injected fixture parameters in test functions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 111 to 172
// Tracks local names that refer to pytest so we can resolve @pytest.fixture and fixture aliases.
#[derive(Default)]
struct PytestAliases {
pytest_module_aliases: Vec<String>,
fixture_aliases: Vec<String>,
}

impl PytestAliases {
fn from_module(module: &ModModule) -> Self {
let mut aliases = Self::default();
for stmt in &module.body {
match stmt {
Stmt::Import(import_stmt) => {
for alias in &import_stmt.names {
if alias.name.id == "pytest" {
let local_name = alias
.asname
.as_ref()
.map(|asname| asname.id.as_str())
.unwrap_or(alias.name.id.as_str());
aliases.pytest_module_aliases.push(local_name.to_owned());
}
}
}
Stmt::ImportFrom(StmtImportFrom {
module: Some(module),
names,
..
}) if module.id == "pytest" || module.id.starts_with("pytest.") => {
for alias in names {
if alias.name.id == "fixture" {
let local_name = alias
.asname
.as_ref()
.map(|asname| asname.id.as_str())
.unwrap_or(alias.name.id.as_str());
aliases.fixture_aliases.push(local_name.to_owned());
}
}
}
_ => {}
}
}
aliases
}

fn is_empty(&self) -> bool {
self.pytest_module_aliases.is_empty() && self.fixture_aliases.is_empty()
}

fn is_pytest_module_alias(&self, name: &Name) -> bool {
self.pytest_module_aliases
.iter()
.any(|alias| alias == name.as_str())
}

fn is_fixture_alias(&self, name: &Name) -> bool {
self.fixture_aliases
.iter()
.any(|alias| alias == name.as_str())
}
}
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

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

The new helper functions and structs lack documentation comments. Consider adding doc comments to explain:

  • What PytestAliases tracks and why it's needed
  • How is_pytest_fixture_decorator identifies fixture decorators
  • The behavior of is_pytest_test_function with different class_context values
  • What PytestFixtureDefinition represents

Copilot uses AI. Check for mistakes.
@github-actions

This comment has been minimized.

@kinto0
Copy link
Contributor

kinto0 commented Jan 12, 2026

todo: should these pytest functions in their own mod?

for sure. maybe a pyrefly/lib/third_party or something similar?

@github-actions
Copy link

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LSP: Navigation support for pytest fixtures

3 participants