Skip to main content

Style checker and fixer

Style checker and safe auto-fixer for fast.ai Python and nbdev notebook conventions.

Run chkstyle from the command line or call check_source, fix_source, check_path, and fix_path from Python. Configure rules under [tool.chkstyle] in pyproject.toml (merged onto $XDG_CONFIG_HOME/chkstyle/config.toml), or suppress inline with # chkstyle: ignore, # chkstyle: ignore-node, # chkstyle: off/on, and # chkstyle: skip.

assert check_source("x = 1; y = 2\n", "demo.py")[0][2] == "semicolon"
fixed, n = fix_source("x = 1; y = 2\n", "demo.py", {"semicolon"})
assert fixed == "x = 1\ny = 2\n" and n == 1

Issue

def Issue(
    rule:str, # Rule identifier (e.g. `"semicolon"`, `"hard-wrapped-docstring"`)
    path:str, # File or notebook cell path label
    lineno:int, # 1-based line number of the violation
    msg:str, # Human-readable message with optional hint
    lines:list[str], # Offending source lines shown in diagnostics
    edit:tuple[int, int, str] | None=None, # Optional `(start_offset, end_offset, replacement)` safe fix
)->None:

A single style violation with an optional safe source edit.

Issue.violation

def violation()->tuple:

Return the (path, lineno, rule, msg, lines) tuple used by check_* functions.

Configuration and discovery

user_config

def user_config()->dict:

Load global settings from $XDG_CONFIG_HOME/chkstyle/config.toml (~/.config by default).

load_config

def load_config(
    root:str='.', # Project directory containing `pyproject.toml`
)->dict: # Merged user and `[tool.chkstyle]` configuration dict

Load [tool.chkstyle] from root/pyproject.toml merged onto user_config() (lists concatenate, skip regexes join, scalars override).

iter_py_files

def iter_py_files(
    root:str, # Root directory to scan
    skip_path_re=None, # Optional regex matched against normalized relative paths to skip
    skip_paths=None, # Optional list of path prefixes to skip
):

Yield .py and .ipynb file paths under root, respecting .gitignore and configured skips.

Checking and fixing

Check or auto-fix Python source strings, .py files, and Jupyter notebooks.

source_issues

def source_issues(
    source:str, # Python source string
    path:str, # Filename label used in diagnostics
    check_unused:bool=True, # Check for unused imports via pyflakes?
    skip_generated:bool=True, # Return `[]` for `# AUTOGENERATED!` nbdev modules?
)->list[chkstyle.core.Issue]: # `Issue` objects carrying optional safe edits

Collect style Issue objects (with optional safe edits) from Python source.

fix_source

def fix_source(
    source:str, # Python source string
    path:str, # Filename label used in diagnostics
    selected:set[str], # Rule IDs to fix (or `{"all"}`)
    notebook:bool=False, # Mask IPython cell magics before parsing?
)->tuple[str, int]: # `(fixed_source, pass_count)`

Iteratively apply safe fixes for selected rules up to 8 passes, returning the updated source and pass count.

check_source

def check_source(
    source:str, # Python source string
    path:str, # Filename label used in diagnostics
    check_unused:bool=True, # Check for unused imports via pyflakes?
)->list[tuple]: # `(path, lineno, rule, msg, lines)` violation tuples

Check Python source and return violation tuples.

check_file

def check_file(
    path:str, # Path to a `.py` file
)->list[tuple]: # `(path, lineno, rule, msg, lines)` violation tuples

Check a Python file for style violations, skipping # AUTOGENERATED! nbdev modules.

fix_file

def fix_file(
    path:str, # Path to a `.py` file
    selected:set[str], # Rule IDs to fix (or `{"all"}`)
)->bool: # `True` when the file was modified on disk

Fix a Python file in place, skipping # AUTOGENERATED! nbdev modules.

check_nb

def check_nb(
    nb, # In-memory `fastcore.nbio.Notebook` object
    path:str='notebook.ipynb', # Label used in diagnostics
)->list[tuple]: # `(cell_path, lineno, rule, msg, lines)` violation tuples

Check an in-memory Jupyter notebook for code style, import, and nbdev narrative violations.

check_notebook

def check_notebook(
    path:str, # Path to a `.ipynb` notebook
)->list[tuple]: # `(cell_path, lineno, rule, msg, lines)` violation tuples

Check a Jupyter notebook file for code style, import, and nbdev narrative violations.

fix_nb

def fix_nb(
    nb, # In-memory `fastcore.nbio.Notebook` object
    selected:set[str], # Rule IDs to fix (or `{"all"}`)
    path:str='notebook.ipynb', # Label used in diagnostics
)->bool: # `True` when any cell changed

Fix an in-memory Jupyter notebook’s code cells in place.

fix_notebook

def fix_notebook(
    path:str, # Path to a `.ipynb` notebook
    selected:set[str], # Rule IDs to fix (or `{"all"}`)
)->bool: # `True` when any cell changed and the notebook was written

Fix Jupyter notebook code cells in place on disk.

check_path

def check_path(
    path:str, # Path to a `.py` or `.ipynb` file
)->list[tuple]: # `(path, lineno, rule, msg, lines)` violation tuples

Check a single .py or .ipynb file for style violations.

fix_path

def fix_path(
    path:str, # Path to a `.py` or `.ipynb` file
    selected:set[str], # Rule IDs to fix (or `{"all"}`)
)->bool: # `True` when the file was modified on disk

Fix a single .py or .ipynb file in place.

CLI

main

def main(
    argv:list[str], # Argument vector including program name (e.g. `sys.argv`)
)->int: # `0` when clean or disabled, `1` when violations remain

Parse CLI arguments, apply --fix if requested, print violations, and return exit status.

cli

def cli()->None:

Console entrypoint (chkstyle) running main(sys.argv) and exiting with its status code.