pytest_language_server/fixtures/types.rs
1//! Data structures for fixture definitions, usages, and related types.
2
3use std::path::PathBuf;
4
5/// Pytest fixture scope, ordered from narrowest to broadest.
6/// A fixture with a broader scope cannot depend on a fixture with a narrower scope.
7#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub enum FixtureScope {
9 /// Function scope (default) - created once per test function
10 #[default]
11 Function = 0,
12 /// Class scope - created once per test class
13 Class = 1,
14 /// Module scope - created once per test module
15 Module = 2,
16 /// Package scope - created once per test package
17 Package = 3,
18 /// Session scope - created once per test session
19 Session = 4,
20}
21
22impl FixtureScope {
23 /// Parse scope from a string (as used in @pytest.fixture(scope="..."))
24 pub fn parse(s: &str) -> Option<Self> {
25 match s.to_lowercase().as_str() {
26 "function" => Some(Self::Function),
27 "class" => Some(Self::Class),
28 "module" => Some(Self::Module),
29 "package" => Some(Self::Package),
30 "session" => Some(Self::Session),
31 _ => None,
32 }
33 }
34
35 /// Get display name for the scope
36 pub fn as_str(&self) -> &'static str {
37 match self {
38 Self::Function => "function",
39 Self::Class => "class",
40 Self::Module => "module",
41 Self::Package => "package",
42 Self::Session => "session",
43 }
44 }
45}
46
47/// A fixture definition extracted from a Python file.
48#[derive(Debug, Clone, PartialEq)]
49pub struct FixtureDefinition {
50 pub name: String,
51 pub file_path: PathBuf,
52 pub line: usize,
53 pub start_char: usize, // Character position where the fixture name starts (on the line)
54 pub end_char: usize, // Character position where the fixture name ends (on the line)
55 pub docstring: Option<String>,
56 pub return_type: Option<String>, // The return type annotation (for generators, the yielded type)
57 pub is_third_party: bool, // Whether this fixture is from a third-party package (site-packages)
58 pub dependencies: Vec<String>, // Names of fixtures this fixture depends on (via parameters)
59 pub scope: FixtureScope, // The fixture's scope (function, class, module, package, session)
60 pub yield_line: Option<usize>, // Line number of the yield statement (for generator fixtures)
61}
62
63/// A fixture usage (reference) in a Python file.
64#[derive(Debug, Clone)]
65pub struct FixtureUsage {
66 pub name: String,
67 pub file_path: PathBuf,
68 pub line: usize,
69 pub start_char: usize, // Character position where this usage starts (on the line)
70 pub end_char: usize, // Character position where this usage ends (on the line)
71}
72
73/// An undeclared fixture used in a function body without being declared as a parameter.
74#[derive(Debug, Clone)]
75#[allow(dead_code)] // Fields used for debugging and future features
76pub struct UndeclaredFixture {
77 pub name: String,
78 pub file_path: PathBuf,
79 pub line: usize,
80 pub start_char: usize,
81 pub end_char: usize,
82 pub function_name: String, // Name of the test/fixture function where this is used
83 pub function_line: usize, // Line where the function is defined
84}
85
86/// A circular dependency between fixtures.
87#[derive(Debug, Clone)]
88pub struct FixtureCycle {
89 /// The chain of fixtures forming the cycle (e.g., ["A", "B", "C", "A"]).
90 pub cycle_path: Vec<String>,
91 /// The fixture where the cycle was detected (first fixture in the cycle).
92 pub fixture: FixtureDefinition,
93}
94
95/// A scope mismatch where a broader-scoped fixture depends on a narrower-scoped fixture.
96#[derive(Debug, Clone)]
97pub struct ScopeMismatch {
98 /// The fixture with broader scope that has the invalid dependency.
99 pub fixture: FixtureDefinition,
100 /// The dependency fixture with narrower scope.
101 pub dependency: FixtureDefinition,
102}
103
104/// Context for code completion.
105#[derive(Debug, Clone, PartialEq)]
106pub enum CompletionContext {
107 /// Inside a function signature (parameter list) - suggest fixtures as parameters.
108 FunctionSignature {
109 function_name: String,
110 function_line: usize,
111 is_fixture: bool,
112 declared_params: Vec<String>,
113 },
114 /// Inside a function body - suggest fixtures with auto-add to parameters.
115 FunctionBody {
116 function_name: String,
117 function_line: usize,
118 is_fixture: bool,
119 declared_params: Vec<String>,
120 },
121 /// Inside @pytest.mark.usefixtures("...") decorator - suggest fixture names as strings.
122 UsefixuturesDecorator,
123 /// Inside @pytest.mark.parametrize(..., indirect=...) - suggest fixture names as strings.
124 ParametrizeIndirect,
125}
126
127/// Information about where to insert a new parameter in a function signature.
128#[derive(Debug, Clone, PartialEq)]
129pub struct ParamInsertionInfo {
130 /// Line number (1-indexed) where the function signature is.
131 pub line: usize,
132 /// Character position where the new parameter should be inserted.
133 pub char_pos: usize,
134 /// Whether a comma needs to be added before the new parameter.
135 pub needs_comma: bool,
136}