similarity-py 0.5.0

CLI tool for detecting code duplication in Python projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#![allow(clippy::io_other_error)]

use similarity_core::language_parser::{
    GenericFunctionDef, GenericTypeDef, Language, LanguageParser,
};
use similarity_core::tree::TreeNode;
use std::error::Error;
use std::rc::Rc;
use tree_sitter::{Node, Parser};

pub struct PythonParser {
    parser: Parser,
}

impl PythonParser {
    pub fn new() -> Result<Self, Box<dyn Error + Send + Sync>> {
        let mut parser = Parser::new();
        parser.set_language(&tree_sitter_python::LANGUAGE.into()).map_err(|e| {
            Box::new(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("Failed to set Python language: {e:?}"),
            )) as Box<dyn Error + Send + Sync>
        })?;

        Ok(Self { parser })
    }

    #[allow(clippy::only_used_in_recursion)]
    fn convert_node(&self, node: Node, source: &str, id_counter: &mut usize) -> TreeNode {
        let current_id = *id_counter;
        *id_counter += 1;

        let label = node.kind().to_string();
        let value = match node.kind() {
            "identifier" | "string" | "integer" | "float" | "true" | "false" | "none" => {
                node.utf8_text(source.as_bytes()).unwrap_or("").to_string()
            }
            _ => "".to_string(),
        };

        let mut tree_node = TreeNode::new(label, value, current_id);

        for child in node.children(&mut node.walk()) {
            let child_node = self.convert_node(child, source, id_counter);
            tree_node.add_child(Rc::new(child_node));
        }

        tree_node
    }

    fn extract_functions_from_node(
        &self,
        node: Node,
        source: &str,
        class_name: Option<&str>,
    ) -> Vec<GenericFunctionDef> {
        let mut functions = Vec::new();

        // Visit all nodes
        fn visit_node(
            node: Node,
            source: &str,
            functions: &mut Vec<GenericFunctionDef>,
            class_name: Option<&str>,
        ) {
            match node.kind() {
                "function_definition" => {
                    if let Some(name_node) = node.child_by_field_name("name") {
                        if let Ok(name) = name_node.utf8_text(source.as_bytes()) {
                            let params_node = node.child_by_field_name("parameters");
                            let body_node = node.child_by_field_name("body");

                            let params = extract_params(params_node, source);

                            functions.push(GenericFunctionDef {
                                name: name.to_string(),
                                start_line: node.start_position().row as u32 + 1,
                                end_line: node.end_position().row as u32 + 1,
                                body_start_line: body_node
                                    .map(|n| n.start_position().row as u32 + 1)
                                    .unwrap_or(0),
                                body_end_line: body_node
                                    .map(|n| n.end_position().row as u32 + 1)
                                    .unwrap_or(0),
                                parameters: params,
                                is_method: class_name.is_some(),
                                class_name: class_name.map(|s| s.to_string()),
                                is_async: is_async_def(node, source),
                                is_generator: is_generator_def(node, source),
                                decorators: extract_decorators(node, source),
                            });
                        }
                    }
                }
                "decorated_definition" => {
                    // Check if it decorates a function
                    if let Some(child) = node.child(node.child_count().saturating_sub(1)) {
                        if child.kind() == "function_definition" {
                            if let Some(name_node) = child.child_by_field_name("name") {
                                if let Ok(name) = name_node.utf8_text(source.as_bytes()) {
                                    let params_node = child.child_by_field_name("parameters");
                                    let body_node = child.child_by_field_name("body");

                                    let params = extract_params(params_node, source);

                                    functions.push(GenericFunctionDef {
                                        name: name.to_string(),
                                        start_line: node.start_position().row as u32 + 1,
                                        end_line: node.end_position().row as u32 + 1,
                                        body_start_line: body_node
                                            .map(|n| n.start_position().row as u32 + 1)
                                            .unwrap_or(0),
                                        body_end_line: body_node
                                            .map(|n| n.end_position().row as u32 + 1)
                                            .unwrap_or(0),
                                        parameters: params,
                                        is_method: class_name.is_some(),
                                        class_name: class_name.map(|s| s.to_string()),
                                        is_async: is_async_def(child, source),
                                        is_generator: is_generator_def(child, source),
                                        decorators: extract_decorators(child, source),
                                    });
                                }
                            }
                        }
                    }
                }
                "class_definition" => {
                    // Don't recurse into nested classes when we're already in a class
                    if class_name.is_none() {
                        if let Some(name_node) = node.child_by_field_name("name") {
                            if let Ok(name) = name_node.utf8_text(source.as_bytes()) {
                                // Recursively extract methods from this class
                                let mut subcursor = node.walk();
                                for child in node.children(&mut subcursor) {
                                    visit_node(child, source, functions, Some(name));
                                }
                            }
                        }
                    }
                }
                _ => {
                    // Continue traversing for other node types
                    let mut subcursor = node.walk();
                    for child in node.children(&mut subcursor) {
                        visit_node(child, source, functions, class_name);
                    }
                }
            }
        }

        fn is_async_def(node: Node, source: &str) -> bool {
            if let Ok(text) = node.utf8_text(source.as_bytes()) {
                text.starts_with("async ")
            } else {
                false
            }
        }

        fn is_generator_def(node: Node, source: &str) -> bool {
            // Python generators are functions that contain yield statements
            // For simplicity, we'll just check if the function body contains "yield"
            if let Some(body) = node.child_by_field_name("body") {
                if let Ok(body_text) = body.utf8_text(source.as_bytes()) {
                    return body_text.contains("yield");
                }
            }
            false
        }

        fn extract_decorators(node: Node, source: &str) -> Vec<String> {
            let mut decorators = Vec::new();
            let mut cursor = node.walk();

            // Look for decorator nodes before the function definition
            if let Some(parent) = node.parent() {
                for child in parent.children(&mut cursor) {
                    if child.kind() == "decorator"
                        && child.end_position().row < node.start_position().row
                    {
                        if let Ok(decorator_text) = child.utf8_text(source.as_bytes()) {
                            decorators.push(decorator_text.trim_start_matches('@').to_string());
                        }
                    }
                }
            }

            decorators
        }

        fn extract_params(params_node: Option<Node>, source: &str) -> Vec<String> {
            if let Some(node) = params_node {
                let mut params = Vec::new();
                let mut cursor = node.walk();

                for child in node.children(&mut cursor) {
                    match child.kind() {
                        "identifier" => {
                            if let Ok(param_text) = child.utf8_text(source.as_bytes()) {
                                params.push(param_text.to_string());
                            }
                        }
                        "typed_parameter" | "default_parameter" => {
                            if let Some(ident) = child.child_by_field_name("name") {
                                if let Ok(param_text) = ident.utf8_text(source.as_bytes()) {
                                    params.push(param_text.to_string());
                                }
                            }
                        }
                        _ => {}
                    }
                }

                params
            } else {
                Vec::new()
            }
        }

        visit_node(node, source, &mut functions, class_name);
        functions
    }
}

impl LanguageParser for PythonParser {
    fn parse(
        &mut self,
        source: &str,
        _filename: &str,
    ) -> Result<Rc<TreeNode>, Box<dyn Error + Send + Sync>> {
        let tree = self.parser.parse(source, None).ok_or_else(|| {
            Box::new(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Failed to parse Python source",
            )) as Box<dyn Error + Send + Sync>
        })?;

        let root_node = tree.root_node();
        let mut id_counter = 0;
        Ok(Rc::new(self.convert_node(root_node, source, &mut id_counter)))
    }

    fn extract_functions(
        &mut self,
        source: &str,
        _filename: &str,
    ) -> Result<Vec<GenericFunctionDef>, Box<dyn Error + Send + Sync>> {
        let tree = self.parser.parse(source, None).ok_or_else(|| {
            Box::new(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Failed to parse Python source",
            )) as Box<dyn Error + Send + Sync>
        })?;

        let root_node = tree.root_node();
        Ok(self.extract_functions_from_node(root_node, source, None))
    }

    fn extract_types(
        &mut self,
        source: &str,
        _filename: &str,
    ) -> Result<Vec<GenericTypeDef>, Box<dyn Error + Send + Sync>> {
        let tree = self.parser.parse(source, None).ok_or_else(|| {
            Box::new(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Failed to parse Python source",
            )) as Box<dyn Error + Send + Sync>
        })?;

        let root_node = tree.root_node();
        let mut types = Vec::new();

        fn visit_node_for_types(node: Node, source: &str, types: &mut Vec<GenericTypeDef>) {
            if node.kind() == "class_definition" {
                if let Some(name_node) = node.child_by_field_name("name") {
                    if let Ok(name) = name_node.utf8_text(source.as_bytes()) {
                        types.push(GenericTypeDef {
                            name: name.to_string(),
                            kind: "class".to_string(),
                            start_line: node.start_position().row as u32 + 1,
                            end_line: node.end_position().row as u32 + 1,
                            fields: extract_class_fields(node, source),
                        });
                    }
                }
            }

            // Continue traversing
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                visit_node_for_types(child, source, types);
            }
        }

        fn extract_class_fields(node: Node, source: &str) -> Vec<String> {
            let mut fields = Vec::new();

            if let Some(body) = node.child_by_field_name("body") {
                let mut cursor = body.walk();
                for child in body.children(&mut cursor) {
                    // Look for instance variable assignments in __init__ method
                    if child.kind() == "function_definition" {
                        if let Some(name_node) = child.child_by_field_name("name") {
                            if let Ok(name) = name_node.utf8_text(source.as_bytes()) {
                                if name == "__init__" {
                                    // Extract self.field assignments from __init__
                                    if let Some(func_body) = child.child_by_field_name("body") {
                                        extract_self_assignments(func_body, source, &mut fields);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            fields
        }

        fn extract_self_assignments(node: Node, source: &str, fields: &mut Vec<String>) {
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if child.kind() == "assignment" {
                    if let Some(left) = child.child(0) {
                        if left.kind() == "attribute" {
                            if let Ok(text) = left.utf8_text(source.as_bytes()) {
                                if text.starts_with("self.") {
                                    let field_name = text.trim_start_matches("self.");
                                    if !fields.contains(&field_name.to_string()) {
                                        fields.push(field_name.to_string());
                                    }
                                }
                            }
                        }
                    }
                }
                // Recursively check nested nodes
                extract_self_assignments(child, source, fields);
            }
        }

        visit_node_for_types(root_node, source, &mut types);
        Ok(types)
    }

    fn language(&self) -> Language {
        Language::Python
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_python_functions() {
        let mut parser = PythonParser::new().unwrap();
        let source = r#"
def hello(name):
    return f"Hello, {name}!"

def add(a, b=0):
    return a + b

class Calculator:
    def __init__(self):
        self.result = 0
    
    def add(self, x):
        self.result += x
        return self.result
"#;

        let functions = parser.extract_functions(source, "test.py").unwrap();
        assert_eq!(functions.len(), 4);
        assert_eq!(functions[0].name, "hello");
        assert_eq!(functions[1].name, "add");
        assert!(!functions[1].is_method);
        assert_eq!(functions[2].name, "__init__");
        assert!(functions[2].is_method);
        assert_eq!(functions[2].class_name, Some("Calculator".to_string()));
        assert_eq!(functions[3].name, "add");
        assert!(functions[3].is_method);
    }

    #[test]
    fn test_python_classes() {
        let mut parser = PythonParser::new().unwrap();
        let source = r#"
class User:
    def __init__(self, name):
        self.name = name

class Admin(User):
    def __init__(self, name, level):
        super().__init__(name)
        self.level = level
"#;

        let types = parser.extract_types(source, "test.py").unwrap();
        assert_eq!(types.len(), 2);
        assert_eq!(types[0].name, "User");
        assert_eq!(types[0].kind, "class");
        assert_eq!(types[1].name, "Admin");
    }
}