{"id":7104,"library":"complexipy","title":"Complexipy","description":"Complexipy is an extremely fast Python library, written in Rust, designed to calculate the cognitive complexity of Python files. Unlike cyclomatic complexity, it focuses on measuring how difficult code is for humans to read and understand, identifying hard-to-maintain sections. It operates as both a command-line interface (CLI) tool and a Python API, facilitating integration into development workflows, CI/CD pipelines, and pre-commit hooks. The current version is 5.2.0, with an active release cadence, frequently adding new features and improvements.","status":"active","version":"5.2.0","language":"en","source_language":"en","source_url":"https://github.com/rohaquinlop/complexipy","tags":["complexity","cognitive complexity","code analysis","linter","rust","cli","static analysis","maintainability"],"install":[{"cmd":"pip install complexipy","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for analyzing remote Git repositories or tracking complexity diffs against Git references.","package":"git","optional":true}],"imports":[{"symbol":"file_complexity","correct":"from complexipy import file_complexity"},{"symbol":"code_complexity","correct":"from complexipy import code_complexity"}],"quickstart":{"code":"import os\nfrom complexipy import file_complexity, code_complexity\n\n# Example 1: Analyze a Python string\nsnippet = \"\"\"\ndef calculate_discount(price, quantity, is_member):\n    if is_member:\n        if quantity > 10:\n            return price * quantity * 0.8 # 20% discount\n        else:\n            return price * quantity * 0.9 # 10% discount\n    else:\n        return price * quantity\n\"\"\"\n\nprint(\"\\n--- Analyzing code snippet ---\")\nresult_code = code_complexity(snippet)\nprint(f\"Overall snippet complexity: {result_code.complexity}\")\nfor func in result_code.functions:\n    print(f\"  Function '{func.name}': Complexity {func.complexity}, Line {func.line_start}-{func.line_end}\")\n\n# Example 2: Analyze a temporary Python file\nfile_content = \"\"\"\ndef process_data(data):\n    if data.get('status') == 'active':\n        for item in data.get('items', []):\n            if item.get('value', 0) > 100:\n                print(f\"Processing high-value item: {item['id']}\")\n            else:\n                print(f\"Processing regular item: {item['id']}\")\n    elif data.get('status') == 'pending':\n        print(\"Data pending, skipping processing.\")\n    else:\n        print(\"Unknown data status.\")\n\"\"\"\n\nfile_path = \"example_module.py\"\nwith open(file_path, \"w\") as f:\n    f.write(file_content)\n\nprint(f\"\\n--- Analyzing file: {file_path} ---\")\nresult_file = file_complexity(file_path)\nprint(f\"File complexity: {result_file.complexity}\")\nfor func in result_file.functions:\n    print(f\"  Function '{func.name}': Complexity {func.complexity}, Line {func.line_start}-{func.line_end}\")\n\n# Clean up temporary file\nos.remove(file_path)\n","lang":"python","description":"This quickstart demonstrates how to use `complexipy` both by analyzing a Python code string directly and by analyzing a Python file on disk. It imports `file_complexity` and `code_complexity` to get detailed cognitive complexity metrics for functions within the given code."},"warnings":[{"fix":"Review existing codebases and adjust complexity thresholds (`--max-complexity-allowed`) or refactor high-complexity functions. Consider using `--snapshot-create` (v5.0.0+) to establish a baseline for existing complexity.","message":"In version 4.0.0, the calculation method for boolean operators in conditions was updated to align with the original Cognitive Complexity paper. This change may result in higher reported complexity scores for existing functions, potentially causing previously passing checks to fail.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Be aware that complexity scores for functions with extensive conditional logic might increase. Update your `pyproject.toml` configuration or CLI flags to reflect new acceptable thresholds if necessary. Refactor deeply nested or highly branched logic.","message":"Version 5.0.0 introduced a significant change where conditional scoring now counts each `elif` and `else` branch as an additional +1 complexity (plus its boolean test), aligning with Sonar's cognitive complexity rules. This will likely lead to higher scores for functions with multiple branching statements.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Understand the snapshot behavior for CI/CD. Use `--snapshot-ignore` to bypass snapshot comparisons temporarily or review and commit updated snapshots (`complexipy-snapshot.json`) when improvements are made. Manage `.complexipy_cache` if you need to clear old change tracking data.","message":"Starting from version 5.0.0, `complexipy` introduced snapshot baselines (`--snapshot-create`) and per-target change tracking with a `.complexipy_cache` directory. If a `complexipy-snapshot.json` file exists, the tool will automatically fail CI if new functions exceed the threshold or tracked functions become more complex. It will also update the snapshot on improvements.","severity":"gotcha","affected_versions":">=5.0.0"},{"fix":"Update your `pyproject.toml` or `complexipy.toml` configuration and CLI commands to use `output-format = \"json\"` or `--output-format json` respectively, for generating reports in specific formats.","message":"With the release of version 5.0.0, legacy TOML configuration keys (e.g., `output-json = true`) and CLI flags (e.g., `--output-json`) for output format are deprecated. They are replaced by the unified `output-format` key/flag which accepts values like `json` or `csv`.","severity":"deprecated","affected_versions":">=5.0.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `complexipy` is installed with `pip install complexipy`. If using a virtual environment, activate it before running the command. Verify the Python scripts directory is in your system's PATH.","cause":"The `complexipy` command-line tool is not installed or is not accessible in your system's PATH. This usually happens if the package wasn't installed correctly or the Python environment is not activated.","error":"Command 'complexipy' not found"},{"fix":"To prevent failure on high complexity, use `complexipy . --ignore-complexity` to report all functions without failing, or set a higher threshold with `complexipy . --max-complexity-allowed <value>`. To disable the error exit entirely, use `complexipy . --max-complexity-allowed 0`.","cause":"By default, `complexipy` is configured to exit with a non-zero status code (indicating failure) if any analyzed function's cognitive complexity exceeds the `--max-complexity-allowed` threshold (defaulting to 15). This behavior is intended for enforcing code quality.","error":"CLI exits with code 1 (or fails CI pipeline) when complexity exceeds threshold"},{"fix":"Double-check the path provided to `complexipy`. Ensure it is correct and accessible from your current working directory. Use absolute paths for clarity or navigate to the correct directory before execution.","cause":"The file or directory path provided to the `complexipy` command does not exist at the specified location, or there's a typo in the path.","error":"error: path 'non_existent_file.py' does not exist (or similar file/directory not found error)"}]}