Cython Lint
cython-lint is a powerful static analysis tool and pre-commit hook designed to lint Cython files. It extends the capabilities typically found in Python linters like flake8 to the Cython language, helping developers maintain code quality and catch common pitfalls. The current version is 0.19.0, with a release cadence of several minor versions per year.
Warnings
- gotcha cython-lint flags unused variables and imports. Messages like ''variable defined but unused'' or ''imported but unused'' indicate code that can be cleaned up, similar to Python linters.
- gotcha Dangerous mutable default values in function arguments are flagged (e.g., `def func(arg=[]):`). This common Python footgun extends to Cython functions.
- gotcha By default, cython-lint includes `pycodestyle` checks, which may report Python-specific style issues. If these are not desired, they can be disabled.
- breaking cython-lint requires Python 3.10 or newer. Attempting to install or run on older Python versions will result in an installation or runtime error.
- gotcha Relative imports can lead to complex module resolution issues in Python and Cython. `cython-lint` introduced a `--ban-relative-imports` flag in v0.17.0 to help enforce absolute imports.
Install
-
pip install cython-lint
Imports
- cython_lint
Usage is primarily via the command-line interface or as a pre-commit hook.
Quickstart
echo "def foo():\n x = 1 # unused variable\n if (1,): pass # always true tuple condition\n l = []\n l.append(1)" > my_module.pyx cython-lint my_module.pyx