Pre-commit Hooks¶
CytoScnPy provides several pre-commit hooks to automate code analysis in your local development workflow. This ensures that unused code, security vulnerabilities, and quality issues are caught before they reach your repository.
Installation¶
-
Install pre-commit:
-
Add Configuration: Create or update
.pre-commit-config.yamlin your project root: -
Install Hooks:
Available Hooks¶
| Hook ID | Description | Recommended For |
|---|---|---|
cytoscnpy-check | Full analysis (security + quality + dead code) | General protection |
cytoscnpy-danger | Scans for dangerous patterns (SQLi, XSS, etc.) | Security-focused projects |
cytoscnpy-secrets | Scans for hardcoded credentials/API keys | All projects |
cytoscnpy-quality | Checks CC, MI, and unused code | Maintaining code health |
cytoscnpy-security | cytoscnpy-danger + cytoscnpy-secrets | Security hardening |
Usage & Best Practices¶
Selective Analysis¶
If you only want to fail on security issues but want to see quality warnings, use separate hooks:
- id: cytoscnpy-security
args: ["--fail-threshold", "0"]
- id: cytoscnpy-quality
args: ["--fail-on-quality", "false"]
Strictness Levels¶
You can enforce strict quality gates using these flags in args:
--fail-on-quality: Exit with code 1 if any quality issues are found.--fail-threshold <N>: Fail if unused code percentage exceeds N.--max-complexity <N>: Fail if any function exceeds complexity N.
Performance¶
CytoScnPy is built in Rust and is designed to be extremely fast. However, for very large monorepos, you may want to limit the frequency:
Troubleshooting¶
"Too many open files"¶
If running on thousands of files at once, you might hit OS limits. You can limit the hook to specific directories:
Suppression¶
To ignore a specific finding on a line, use any of these formats:
def legacy_function(): # noqa
pass # bare noqa suppresses all
unused_var = 1 # ignore
# bare ignore also suppresses all
secret_key = "..." # pragma: no cytoscnpy
# legacy format, still supported
other_var = value # noqa: E501, CSP
# mixed codes: suppresses CytoScnPy because CSP is in the list
[!NOTE] Inline suppression comments (
# noqa,# ignore,# pragma: no cytoscnpy) apply to dead code, security, quality, and clone findings on a specific line. For ignoring rules across the entire project, use theignorelist in your.cytoscnpy.tomlconfiguration file.