-
Notifications
You must be signed in to change notification settings - Fork 255
impl LSP: Navigation support for pytest fixtures #1901 #2075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
pyrefly/lib/state/lsp.rs
Outdated
| // 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()) | ||
| } | ||
| } |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
for sure. maybe a pyrefly/lib/third_party or something similar? |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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