From f37bfbb293fb1aa0eb84fe807699ffb32a721ece Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 10:35:07 -0400 Subject: [PATCH 01/16] draft implementation --- complexipy/main.py | 10 ++++++- src/classes/mod.rs | 7 +++++ src/cognitive_complexity/mod.rs | 50 +++++++++++++++++++++++++-------- src/lib.rs | 2 ++ 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/complexipy/main.py b/complexipy/main.py index 78c99f9..39bf060 100644 --- a/complexipy/main.py +++ b/complexipy/main.py @@ -25,10 +25,12 @@ import time import typer +from typing import Any + root_dir = Path(__file__).resolve().parent.parent app = typer.Typer(name="complexipy") console = Console() -version = "0.4.0" +version = "0.4.0dev" @app.command() @@ -109,5 +111,11 @@ def main( raise typer.Exit(code=1) +def compute( + code: str, max_complexity: int = 15, file_level: bool = True +) -> Any: + return rust.compute(code, max_complexity, file_level) + + if __name__ == "__main__": app() diff --git a/src/classes/mod.rs b/src/classes/mod.rs index fe98a5d..b145b80 100644 --- a/src/classes/mod.rs +++ b/src/classes/mod.rs @@ -15,3 +15,10 @@ pub struct FileComplexity { pub functions: Vec, pub complexity: u64, } + +#[derive(Clone)] +#[pyclass(module = "complexipy", get_all)] +pub struct CodeComplexity { + pub functions: Vec, + pub complexity: u64, +} diff --git a/src/cognitive_complexity/mod.rs b/src/cognitive_complexity/mod.rs index c595d21..4042d17 100644 --- a/src/cognitive_complexity/mod.rs +++ b/src/cognitive_complexity/mod.rs @@ -1,10 +1,11 @@ pub mod utils; -use crate::classes::{FileComplexity, FunctionComplexity}; +use crate::classes::{FileComplexity, FunctionComplexity, CodeComplexity}; use ignore::Walk; use indicatif::ProgressBar; use indicatif::ProgressStyle; use pyo3::prelude::*; +use pyo3::exceptions::PyValueError; use rayon::prelude::*; use rustpython_parser::{ ast::{self, Stmt}, @@ -145,16 +146,44 @@ pub fn cognitive_complexity( _max_complexity: usize, _file_level: bool, ) -> PyResult { + let path = path::Path::new(file_path); + let file_name = path.file_name().unwrap().to_str().unwrap(); + let relative_path = path.strip_prefix(base_path).unwrap().to_str().unwrap(); + let code = std::fs::read_to_string(file_path)?; - let ast = ast::Suite::parse(&code, "").unwrap(); + + // TODO: maybe this default propagation of errors could be a macro + let code_complexity = match compute(&code, _max_complexity, _file_level) { + Ok(v) => v, + Err(e) => return Err( + PyValueError:: + new_err(format!("Failed to compute code_complexity; error: {}", e))), + }; + + Ok(FileComplexity { + path: relative_path.to_string(), + file_name: file_name.to_string(), + complexity: code_complexity.complexity, + functions: code_complexity.functions, + }) +} + +#[pyfunction] +pub fn compute( + code: &str, + _max_complexity: usize, + _file_level: bool, +) -> PyResult { + let ast = match ast::Suite::parse(&code, "") { + Ok(v) => v, + Err(e) => return Err( + PyValueError:: + new_err(format!("Failed to parse this code; error: {}", e))), + }; let mut complexity: u64 = 0; - let path = path::Path::new(file_path); - let file_name = path.file_name().unwrap().to_str().unwrap(); let mut functions: Vec = Vec::new(); - let relative_path = path.strip_prefix(base_path).unwrap().to_str().unwrap(); - if _file_level { for node in ast.iter() { complexity += statement_cognitive_complexity(node.clone(), 0)?; @@ -165,14 +194,13 @@ pub fn cognitive_complexity( complexity = c; } - Ok(FileComplexity { - path: relative_path.to_string(), - file_name: file_name.to_string(), - complexity: complexity, - functions: functions, + Ok(CodeComplexity { + functions, + complexity, }) } + fn function_level_cognitive_complexity( ast: &Vec, ) -> PyResult<(Vec, u64)> { diff --git a/src/lib.rs b/src/lib.rs index fc62d74..be84b55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ mod cognitive_complexity; use classes::{FileComplexity, FunctionComplexity}; use cognitive_complexity::main; +use cognitive_complexity::compute; use cognitive_complexity::utils::{output_csv_file_level, output_csv_function_level}; use pyo3::prelude::*; @@ -10,6 +11,7 @@ use pyo3::prelude::*; #[pymodule] fn rust(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(main, m)?)?; + m.add_function(wrap_pyfunction!(compute, m)?)?; m.add_function(wrap_pyfunction!(output_csv_file_level, m)?)?; m.add_function(wrap_pyfunction!(output_csv_function_level, m)?)?; m.add_class::()?; From 84c1e39d7e3e46c8b2cd49c109f38ee4cd30c382 Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 10:49:35 -0400 Subject: [PATCH 02/16] fix warn: package `typer==0.12.5` does not have an extra named `all` --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f961f6d..6b8416d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ classifiers = [ ] dynamic = ["version"] -dependencies = ["typer[all]"] +dependencies = ["typer"] [project.scripts] complexipy = "complexipy.main:app" From 8f839601014e8c2ab83ed0a857d28d32dc51dcd7 Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 10:53:55 -0400 Subject: [PATCH 03/16] git ignore emacs- and pyright-specific files --- .gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.gitignore b/.gitignore index c8f0442..daad4d5 100644 --- a/.gitignore +++ b/.gitignore @@ -68,5 +68,13 @@ docs/_build/ # VSCode .vscode/ +# Emacs +*~ +\#*\# +*.dir-locals.el + # Pyenv .python-version + +# Pyright LSP +pyrightconfig.json \ No newline at end of file From 6ef9f552393fbdbb8180c8cb1595eaaeefeaba6a Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 12:00:25 -0400 Subject: [PATCH 04/16] rename fn `compute` -> `cognitive_complexity_on_str` --- src/cognitive_complexity/mod.rs | 6 +++--- src/lib.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cognitive_complexity/mod.rs b/src/cognitive_complexity/mod.rs index 4042d17..b068cf9 100644 --- a/src/cognitive_complexity/mod.rs +++ b/src/cognitive_complexity/mod.rs @@ -152,8 +152,7 @@ pub fn cognitive_complexity( let code = std::fs::read_to_string(file_path)?; - // TODO: maybe this default propagation of errors could be a macro - let code_complexity = match compute(&code, _max_complexity, _file_level) { + let code_complexity = match cognitive_complexity_on_str(&code, _max_complexity, _file_level) { Ok(v) => v, Err(e) => return Err( PyValueError:: @@ -168,8 +167,9 @@ pub fn cognitive_complexity( }) } +/// Calculate the cognitive complexity of a string of python code. #[pyfunction] -pub fn compute( +pub fn cognitive_complexity_on_str( code: &str, _max_complexity: usize, _file_level: bool, diff --git a/src/lib.rs b/src/lib.rs index be84b55..a0d0d81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ mod cognitive_complexity; use classes::{FileComplexity, FunctionComplexity}; use cognitive_complexity::main; -use cognitive_complexity::compute; +use cognitive_complexity::cognitive_complexity_on_str; use cognitive_complexity::utils::{output_csv_file_level, output_csv_function_level}; use pyo3::prelude::*; @@ -11,7 +11,7 @@ use pyo3::prelude::*; #[pymodule] fn rust(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(main, m)?)?; - m.add_function(wrap_pyfunction!(compute, m)?)?; + m.add_function(wrap_pyfunction!(cognitive_complexity_on_str, m)?)?; m.add_function(wrap_pyfunction!(output_csv_file_level, m)?)?; m.add_function(wrap_pyfunction!(output_csv_function_level, m)?)?; m.add_class::()?; From 17cc8e61f862f8c4a7d298a918e38ac606cfdfeb Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 12:00:44 -0400 Subject: [PATCH 05/16] expose the `cognitive_complexity` function as well --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index a0d0d81..acdf0b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ use pyo3::prelude::*; #[pymodule] fn rust(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(main, m)?)?; + m.add_function(wrap_pyfunction!(cognitive_complexity)?)?; m.add_function(wrap_pyfunction!(cognitive_complexity_on_str, m)?)?; m.add_function(wrap_pyfunction!(output_csv_file_level, m)?)?; m.add_function(wrap_pyfunction!(output_csv_function_level, m)?)?; From b6dd250351d34f8f642cdc869df3280f0540fcb5 Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 12:05:36 -0400 Subject: [PATCH 06/16] rename functions to avoid name-overlap --- src/cognitive_complexity/mod.rs | 10 +++++----- src/lib.rs | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/cognitive_complexity/mod.rs b/src/cognitive_complexity/mod.rs index b068cf9..081b784 100644 --- a/src/cognitive_complexity/mod.rs +++ b/src/cognitive_complexity/mod.rs @@ -77,7 +77,7 @@ pub fn main( } else { let parent_dir = path::Path::new(path).parent().unwrap().to_str().unwrap(); - match cognitive_complexity(path, parent_dir, max_complexity, file_level) { + match file_complexity(path, parent_dir, max_complexity, file_level) { Ok(file_complexity) => ans.push(file_complexity), Err(e) => return Err(e), } @@ -123,7 +123,7 @@ fn evaluate_dir( .par_iter() .map(|file_path| { pb.inc(1); - match cognitive_complexity(file_path, parent_dir, max_complexity, file_level) { + match file_complexity(file_path, parent_dir, max_complexity, file_level) { Ok(file_complexity) => Ok(file_complexity), Err(e) => Err(e), } @@ -140,7 +140,7 @@ fn evaluate_dir( /// Calculate the cognitive complexity of a python file. #[pyfunction] -pub fn cognitive_complexity( +pub fn file_complexity( file_path: &str, base_path: &str, _max_complexity: usize, @@ -152,7 +152,7 @@ pub fn cognitive_complexity( let code = std::fs::read_to_string(file_path)?; - let code_complexity = match cognitive_complexity_on_str(&code, _max_complexity, _file_level) { + let code_complexity = match code_complexity(&code, _max_complexity, _file_level) { Ok(v) => v, Err(e) => return Err( PyValueError:: @@ -169,7 +169,7 @@ pub fn cognitive_complexity( /// Calculate the cognitive complexity of a string of python code. #[pyfunction] -pub fn cognitive_complexity_on_str( +pub fn code_complexity( code: &str, _max_complexity: usize, _file_level: bool, diff --git a/src/lib.rs b/src/lib.rs index acdf0b7..2b2feac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,8 @@ mod cognitive_complexity; use classes::{FileComplexity, FunctionComplexity}; use cognitive_complexity::main; -use cognitive_complexity::cognitive_complexity_on_str; +use cognitive_complexity::file_complexity; +use cognitive_complexity::code_complexity; use cognitive_complexity::utils::{output_csv_file_level, output_csv_function_level}; use pyo3::prelude::*; @@ -11,8 +12,8 @@ use pyo3::prelude::*; #[pymodule] fn rust(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(main, m)?)?; - m.add_function(wrap_pyfunction!(cognitive_complexity)?)?; - m.add_function(wrap_pyfunction!(cognitive_complexity_on_str, m)?)?; + m.add_function(wrap_pyfunction!(file_complexity, m)?)?; + m.add_function(wrap_pyfunction!(code_complexity, m)?)?; m.add_function(wrap_pyfunction!(output_csv_file_level, m)?)?; m.add_function(wrap_pyfunction!(output_csv_function_level, m)?)?; m.add_class::()?; From 3f6834650016f1022661e1245bdc42bc62a2e42d Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 14:59:38 -0400 Subject: [PATCH 07/16] exposing file_complexity and code_complexity commands; formatting --- complexipy/main.py | 30 +++++++++++++++++++++--------- src/lib.rs | 3 ++- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/complexipy/main.py b/complexipy/main.py index 39bf060..c602a2a 100644 --- a/complexipy/main.py +++ b/complexipy/main.py @@ -11,9 +11,7 @@ from complexipy import ( rust, ) -from complexipy.rust import ( - FileComplexity, -) +from complexipy.rust import FileComplexity, CodeComplexity import os from pathlib import ( Path, @@ -25,8 +23,6 @@ import time import typer -from typing import Any - root_dir = Path(__file__).resolve().parent.parent app = typer.Typer(name="complexipy") console = Console() @@ -111,10 +107,26 @@ def main( raise typer.Exit(code=1) -def compute( - code: str, max_complexity: int = 15, file_level: bool = True -) -> Any: - return rust.compute(code, max_complexity, file_level) +def code_complexity( + # TODO: do we need to include the max_complexity?? + code: str, + max_complexity: int = 15, + file_level: bool = True, +) -> CodeComplexity: + return rust.code_complexity(code, max_complexity, file_level) + + +def file_complexity( + file_path: str, max_complexity: int = 15, file_level: bool = True +) -> FileComplexity: + path = Path(file_path).resolve() + base_path = path.parent + return rust.file_complexity( + file_path=str(path), + base_path=str(base_path), + _max_complexity=max_complexity, + _file_level=file_level, + ) if __name__ == "__main__": diff --git a/src/lib.rs b/src/lib.rs index 2b2feac..b1f1972 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ mod classes; mod cognitive_complexity; -use classes::{FileComplexity, FunctionComplexity}; +use classes::{FileComplexity, FunctionComplexity, CodeComplexity}; use cognitive_complexity::main; use cognitive_complexity::file_complexity; use cognitive_complexity::code_complexity; @@ -18,5 +18,6 @@ fn rust(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(output_csv_function_level, m)?)?; m.add_class::()?; m.add_class::()?; + m.add_class::()?; Ok(()) } From b30daf8e9b3c89d563e841102f7ccd6420471e91 Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 15:02:21 -0400 Subject: [PATCH 08/16] TODO item for the max_complexity parameter --- complexipy/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/complexipy/main.py b/complexipy/main.py index c602a2a..4dc93bd 100644 --- a/complexipy/main.py +++ b/complexipy/main.py @@ -75,6 +75,9 @@ def main( console.rule(f":octopus: complexipy {version}") start_time = time.time() + # TODO: it seems that the max_complexity parameter is actually NOT used throught the + # rust part of the code: it's only used when writing to CSV. + # should we remove it from the main, file_complexity, and code_complexity functions? files: list[FileComplexity] = rust.main( path, is_dir, is_url, max_complexity, file_level ) @@ -108,7 +111,6 @@ def main( def code_complexity( - # TODO: do we need to include the max_complexity?? code: str, max_complexity: int = 15, file_level: bool = True, From 6867bab69bdeebb61bea9155d725b560d3e0f8cd Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 15:08:58 -0400 Subject: [PATCH 09/16] consistent approach to resolving file-paths --- complexipy/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/complexipy/main.py b/complexipy/main.py index 4dc93bd..bb57e7e 100644 --- a/complexipy/main.py +++ b/complexipy/main.py @@ -121,11 +121,11 @@ def code_complexity( def file_complexity( file_path: str, max_complexity: int = 15, file_level: bool = True ) -> FileComplexity: - path = Path(file_path).resolve() + path = Path(file_path) base_path = path.parent return rust.file_complexity( - file_path=str(path), - base_path=str(base_path), + file_path=path.resolve().as_posix(), + base_path=base_path.resolve().as_posix(), _max_complexity=max_complexity, _file_level=file_level, ) From 484de9a7db0aee493b09d7d1e1fdf0268ea4c15f Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Fri, 11 Oct 2024 15:09:10 -0400 Subject: [PATCH 10/16] add tests for file_complexity and code_complexity --- tests/main.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/main.py b/tests/main.py index 8dadc2e..a53ab09 100644 --- a/tests/main.py +++ b/tests/main.py @@ -2,6 +2,8 @@ import unittest from pathlib import Path +from complexipy.main import file_complexity, code_complexity + class TestFiles(unittest.TestCase): local_path = Path(__file__).resolve().parent @@ -102,6 +104,28 @@ def test_try_nested(self): total_complexity = sum([file.complexity for file in files]) self.assertEqual(12, total_complexity) + def test_file_complexity(self): + path = "src/test_try_nested.py" + result = file_complexity(path) + self.assertEqual(12, result.complexity) + + def test_code_complexity(self): + snippet = """\ +def hello_world(s: str) -> str: + ans = "" + + def nested_func(s: str) -> str: + if s == "complexipy": + return "complexipy is awesome!" + return f"I don't know what to say, hello {s}(?)" + + ans = nested_func(s) + + return ans +""" + result = code_complexity(snippet) + self.assertEqual(2, result.complexity) + if __name__ == "__main__": unittest.main() From 67b079b43cb05940cbf629a0ec2ca0155a4d61c3 Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Mon, 14 Oct 2024 21:07:13 -0400 Subject: [PATCH 11/16] group import-lines together --- src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b1f1972..cad406e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,9 +2,7 @@ mod classes; mod cognitive_complexity; use classes::{FileComplexity, FunctionComplexity, CodeComplexity}; -use cognitive_complexity::main; -use cognitive_complexity::file_complexity; -use cognitive_complexity::code_complexity; +use cognitive_complexity::{code_complexity, file_complexity, main}; use cognitive_complexity::utils::{output_csv_file_level, output_csv_function_level}; use pyo3::prelude::*; From fe55b8eaf3880c016b83776c20401dee2782f2ee Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Mon, 14 Oct 2024 21:27:03 -0400 Subject: [PATCH 12/16] bump version to 0.5.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- complexipy/main.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e777c3..19e2a99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,7 +65,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "complexipy" -version = "0.4.0" +version = "0.5.0" dependencies = [ "csv", "ignore", diff --git a/Cargo.toml b/Cargo.toml index 1969943..cad76cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "complexipy" -version = "0.4.0" +version = "0.5.0" edition = "2021" authors = ["Robin Quintero "] license = "MIT" diff --git a/complexipy/main.py b/complexipy/main.py index bb57e7e..8c700e7 100644 --- a/complexipy/main.py +++ b/complexipy/main.py @@ -26,7 +26,7 @@ root_dir = Path(__file__).resolve().parent.parent app = typer.Typer(name="complexipy") console = Console() -version = "0.4.0dev" +version = "0.5.0" @app.command() From 748da59863ddbbfa5398a7ae52cf5274739d68ac Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Mon, 14 Oct 2024 21:27:44 -0400 Subject: [PATCH 13/16] pin typer dependencies --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 6b8416d..1eaa9bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ classifiers = [ ] dynamic = ["version"] -dependencies = ["typer"] +dependencies = ["typer==0.12.5"] [project.scripts] complexipy = "complexipy.main:app" From 5455767542b1d62d8df388adc6da4c1a26618181 Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Mon, 14 Oct 2024 21:28:03 -0400 Subject: [PATCH 14/16] remove max_complexity from parts of code where it's not used --- complexipy/main.py | 15 +++------------ src/cognitive_complexity/mod.rs | 14 +++++--------- tests/main.py | 32 ++++++++++++++++---------------- 3 files changed, 24 insertions(+), 37 deletions(-) diff --git a/complexipy/main.py b/complexipy/main.py index 8c700e7..d04a6fe 100644 --- a/complexipy/main.py +++ b/complexipy/main.py @@ -75,12 +75,7 @@ def main( console.rule(f":octopus: complexipy {version}") start_time = time.time() - # TODO: it seems that the max_complexity parameter is actually NOT used throught the - # rust part of the code: it's only used when writing to CSV. - # should we remove it from the main, file_complexity, and code_complexity functions? - files: list[FileComplexity] = rust.main( - path, is_dir, is_url, max_complexity, file_level - ) + files: list[FileComplexity] = rust.main(path, is_dir, is_url, file_level) execution_time = time.time() - start_time output_csv_path = f"{invocation_path}/complexipy.csv" @@ -112,21 +107,17 @@ def main( def code_complexity( code: str, - max_complexity: int = 15, file_level: bool = True, ) -> CodeComplexity: - return rust.code_complexity(code, max_complexity, file_level) + return rust.code_complexity(code, file_level) -def file_complexity( - file_path: str, max_complexity: int = 15, file_level: bool = True -) -> FileComplexity: +def file_complexity(file_path: str, file_level: bool = True) -> FileComplexity: path = Path(file_path) base_path = path.parent return rust.file_complexity( file_path=path.resolve().as_posix(), base_path=base_path.resolve().as_posix(), - _max_complexity=max_complexity, _file_level=file_level, ) diff --git a/src/cognitive_complexity/mod.rs b/src/cognitive_complexity/mod.rs index 081b784..e3fc587 100644 --- a/src/cognitive_complexity/mod.rs +++ b/src/cognitive_complexity/mod.rs @@ -25,7 +25,6 @@ pub fn main( path: &str, is_dir: bool, is_url: bool, - max_complexity: usize, file_level: bool, ) -> PyResult> { let mut ans: Vec = Vec::new(); @@ -63,21 +62,21 @@ pub fn main( let repo_path = dir.path().join(&repo_name).to_str().unwrap().to_string(); - match evaluate_dir(&repo_path, max_complexity, file_level) { + match evaluate_dir(&repo_path, file_level) { Ok(files_complexity) => ans = files_complexity, Err(e) => return Err(e), } dir.close()?; } else if is_dir { - match evaluate_dir(path, max_complexity, file_level) { + match evaluate_dir(path, file_level) { Ok(files_complexity) => ans = files_complexity, Err(e) => return Err(e), } } else { let parent_dir = path::Path::new(path).parent().unwrap().to_str().unwrap(); - match file_complexity(path, parent_dir, max_complexity, file_level) { + match file_complexity(path, parent_dir, file_level) { Ok(file_complexity) => ans.push(file_complexity), Err(e) => return Err(e), } @@ -92,7 +91,6 @@ pub fn main( fn evaluate_dir( path: &str, - max_complexity: usize, file_level: bool, ) -> PyResult> { let mut files_paths: Vec = Vec::new(); @@ -123,7 +121,7 @@ fn evaluate_dir( .par_iter() .map(|file_path| { pb.inc(1); - match file_complexity(file_path, parent_dir, max_complexity, file_level) { + match file_complexity(file_path, parent_dir, file_level) { Ok(file_complexity) => Ok(file_complexity), Err(e) => Err(e), } @@ -143,7 +141,6 @@ fn evaluate_dir( pub fn file_complexity( file_path: &str, base_path: &str, - _max_complexity: usize, _file_level: bool, ) -> PyResult { let path = path::Path::new(file_path); @@ -152,7 +149,7 @@ pub fn file_complexity( let code = std::fs::read_to_string(file_path)?; - let code_complexity = match code_complexity(&code, _max_complexity, _file_level) { + let code_complexity = match code_complexity(&code, _file_level) { Ok(v) => v, Err(e) => return Err( PyValueError:: @@ -171,7 +168,6 @@ pub fn file_complexity( #[pyfunction] pub fn code_complexity( code: &str, - _max_complexity: usize, _file_level: bool, ) -> PyResult { let ast = match ast::Suite::parse(&code, "") { diff --git a/tests/main.py b/tests/main.py index a53ab09..cc08576 100644 --- a/tests/main.py +++ b/tests/main.py @@ -10,97 +10,97 @@ class TestFiles(unittest.TestCase): def test_path(self): path = self.local_path / "src" - files = rust.main(path.resolve().as_posix(), True, False, 0, True) + files = rust.main(path.resolve().as_posix(), True, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(41, total_complexity) def test(self): path = self.local_path / "src/test.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(9, total_complexity) def test_break_continue(self): path = self.local_path / "src/test_break_continue.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(3, total_complexity) def test_class(self): path = self.local_path / "src/test_class.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(1, total_complexity) def test_decorator(self): path = self.local_path / "src/test_decorator.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(1, total_complexity) def test_for(self): path = self.local_path / "src/test_for.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(5, total_complexity) def test_for_assign(self): path = self.local_path / "src/test_for_assign.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(1, total_complexity) def test_if(self): path = self.local_path / "src/test_if.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(3, total_complexity) def test_main(self): path = self.local_path / "src/test_main.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(0, total_complexity) def test_match(self): path = self.local_path / "src/test_match.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(0, total_complexity) def test_multiple_func(self): path = self.local_path / "src/test_multiple_func.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(0, total_complexity) def test_nested_func(self): path = self.local_path / "src/test_nested_func.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(2, total_complexity) def test_recursive(self): path = self.local_path / "src/test_recursive.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(0, total_complexity) def test_ternary_op(self): path = self.local_path / "src/test_ternary_op.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(1, total_complexity) def test_try(self): path = self.local_path / "src/test_try.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(3, total_complexity) def test_try_nested(self): path = self.local_path / "src/test_try_nested.py" - files = rust.main(path.resolve().as_posix(), False, False, 15, True) + files = rust.main(path.resolve().as_posix(), False, False, True) total_complexity = sum([file.complexity for file in files]) self.assertEqual(12, total_complexity) From c6bf9493c8e0da2e665fa6c44838b7628fdd5c2c Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Tue, 15 Oct 2024 13:00:31 -0400 Subject: [PATCH 15/16] trying to fix FileNotFoundError --- tests/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/main.py b/tests/main.py index cc08576..3002204 100644 --- a/tests/main.py +++ b/tests/main.py @@ -105,8 +105,8 @@ def test_try_nested(self): self.assertEqual(12, total_complexity) def test_file_complexity(self): - path = "src/test_try_nested.py" - result = file_complexity(path) + path = self.local_path / "src/test_try_nested.py" + result = file_complexity(str(path)) self.assertEqual(12, result.complexity) def test_code_complexity(self): From 460dcb3c2298c947062d7e8449dab4a91e3068eb Mon Sep 17 00:00:00 2001 From: Andrew De Angelis Date: Tue, 15 Oct 2024 14:52:24 -0400 Subject: [PATCH 16/16] try to run CI/CD: ubuntu-latest -> ubuntu-22.04 --- .github/workflows/CI.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 06d0166..4bbe242 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -151,7 +151,7 @@ jobs: pytest tests/main.py sdist: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - name: Build sdist