### Install fastaistyle from source Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Clones the repository and installs it in editable mode for development. ```bash git clone https://github.com/AnswerDotAI/fastaistyle cd fastaistyle pip install -e . ``` -------------------------------- ### Install fastaistyle with pip Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Install the fastaistyle package using pip. Run this command in your terminal to install the style checker. ```bash pip install fastaistyle ``` -------------------------------- ### consecutive-short-imports rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md If you have a run of short 'import foo' lines, combine them. The example shows a bad run of separate imports and a good combined import. ```python # Bad import os import sys import pathlib # Good import os, sys, pathlib ``` -------------------------------- ### multi-line-from-import rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md If a from-import fits on one line, keep it on one line. When it can't fit but uses more lines than needed, it's reported as inefficient-multiline-from-import. The example shows a bad multi-line import and a good single-line one. ```python # Bad from os import ( path, environ, ) # Good from os import path, environ ``` -------------------------------- ### continuation-indent rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Continuation lines should be indented exactly 4 spaces beyond the line that opened the block. The example shows a bad indentation and a good one. ```python # Bad result = call( first_arg, second_arg) # Good result = call( first_arg, second_arg) ``` -------------------------------- ### Short variable names example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Illustrates the 'Huffman coding' principle for variable names: frequently used things get short names. The 'Good' example uses short, recognizable names like 'img' and 'i', while the 'Bad' example uses verbose names like 'image_data' and 'loop_index'. Use short names for frequently used variables to keep code compact. ```python # Good: conventional, recognizable img, i, msg, ctx # Bad: verbose for no benefit image_data, loop_index, message_object, context_instance ``` -------------------------------- ### One-line vs multi-line code style comparison Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Shows the fast.ai style preference for keeping simple bodies on one line. The 'Good' example shows three one-line statements, while the 'Bad' example shows the same logic expanded to six lines. Use the one-line style to reduce vertical space and improve readability. ```python # Good: you see the whole pattern at once if not data: return None for item in items: process(item) def _is_ready(self): return self._ready.is_set() # Bad: same logic, but now it's 6 lines instead of 3 if not data: return None for item in items: process(item) def _is_ready(self): return self._ready.is_set() ``` -------------------------------- ### dict-literal rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Use dict() for keyword-like keys when all keys can be valid Python keyword arguments. The example shows a bad dict literal and a good dict() call. ```python # Bad payload = {"host": host, "port": port, "timeout": timeout} # Good payload = dict(host=host, port=port, timeout=timeout) ``` -------------------------------- ### mixed-imports rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Notebooks only. Mixing imports with other code in one cell breaks the build or runs code at doc time. Put imports in their own cell. The example shows a bad cell mixing imports and code, and a good split into two cells. ```python # Bad (one cell) import some_module some_module.something() # Good (two cells) import some_module some_module.something() ``` -------------------------------- ### unused-import rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Remove imports that are never referenced. The example shows a bad unused import and a good used import. ```python # Bad import os # Good import os print(os.getcwd()) ``` -------------------------------- ### closing-bracket rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Don't leave a bare closing ), ], or } on a line by itself. The example shows a bad multi-line list with a closing bracket on its own line and a good version with the closing bracket on the last item's line. ```python # Bad items = [ one, two, ] # Good items = [ one, two] ``` -------------------------------- ### single-statement-body rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md If the body is one simple statement, keep it on the header line when the result is short enough and no comments need to move. The example shows a bad multi-line if and a good one-liner. ```python # Bad if ready: return True # Good if ready: return True ``` -------------------------------- ### single-line-docstring rule example Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Single-line docstrings should use regular quotes, not triple quotes. The example shows a bad triple-quoted docstring and a good single-quoted one. ```python # Bad def foo(): """Return the value.""" return x # Good def foo(): "Return the value." return x ``` -------------------------------- ### Example notebook violation output Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Shows the format of a violation reported for a Jupyter notebook cell, including the cell ID and line number. ```text # notebook.ipynb:cell[abc123]:3: lhs assignment annotation x: int = 1 ``` -------------------------------- ### nested-generics: keep parameter annotations simple Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Use when parameter annotations have deeply nested generics. The example shows a bad deeply nested annotation and a good version using a type alias. ```python # Bad def process(items: list[dict[str, list[int]]]): ... # Good Payload = dict[str, list[int]] def process(items: list[Payload]): ... ``` -------------------------------- ### lhs-assignment-annotation: avoid variable annotations in normal code Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Use for code that should avoid `x: int = 1` or bare `x: int` annotations outside dataclass/BaseModel fields. The example shows a bad annotated assignment and a good function signature annotation. ```python # Bad x: int = 1 name: str = "hello" # Good (in a function signature) def process(x: int, name: str) -> Result: ... ``` -------------------------------- ### inefficient-multiline-expression: condense multiline expressions Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Use when a multiline expression, def/class header, or type annotation would fit on fewer lines. The example contrasts a bad multiline call with a good single-line version. ```python # Bad result = call( a, b, c, ) # Good result = call(a, b, c) ``` -------------------------------- ### Shared chkstyle configuration file Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Settings shared by all projects go in $XDG_CONFIG_HOME/chkstyle/config.toml (~/.config/chkstyle/config.toml by default), with the same keys at top level. Project [tool.chkstyle] adds to it: skip_paths, ignore, and fix lists are concatenated; skip-path-re patterns are joined; other keys take the project's value. ```toml skip_paths = ["_docs", "_proc", "_modidx.py", "node_modules"] ``` -------------------------------- ### Create a PR with a label for release notes Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Creates a pull request with a label for release notes. Use `enhancement` for new features and `bug` for fixes. ```bash ./tools/pr.sh enhancement "Add new feature" ./tools/pr.sh bug "Fix something" ``` -------------------------------- ### Naming conventions (good vs bad) Source: https://github.com/answerdotai/fastaistyle/blob/main/style.md Shows preferred short, conventional names and discouraged verbose alternatives. ```python msg_id, iopub, ctx, i ``` ```python message_identifier, io_pub_channel, context_object, loop_index ``` -------------------------------- ### Configure chkstyle in pyproject.toml Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Configuration for the chkstyle tool in a project's pyproject.toml. Command-line skip arguments override config values; --ignore adds to configured ignores, --fix-rule selects fixer rules, and --show-rule includes rule IDs in output. ```toml [tool.chkstyle] skip_paths = ["vendor", "src/generated"] skip-path-re = "test|migrations|vendor|src/gen" ignore = ["line-too-long"] fix = ["dict-literal", "single-statement-body"] nb-narrative = false # disable the notebook narrative rules ``` -------------------------------- ### Check current directory with chkstyle Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Runs the style checker on the current directory. ```bash chkstyle ``` -------------------------------- ### Skip specific paths with chkstyle --skip-path Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Skips specific paths by name or path. The flag is repeatable. ```bash chkstyle --skip-path vendor --skip-path src/generated ``` -------------------------------- ### Import grouping (good vs bad) Source: https://github.com/answerdotai/fastaistyle/blob/main/style.md Shows the preferred grouped import style and the discouraged separate-line or comma-separated forms. ```python import json, os, time ``` ```python import json import os import time ``` ```python from mymod import ( a, b) ``` ```python from mymod import a,b ``` -------------------------------- ### Check multiple paths with chkstyle Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Checks multiple files and folders in a single invocation. ```bash chkstyle path/to/code tests unit.py ``` -------------------------------- ### Comment explaining why (good vs bad) Source: https://github.com/answerdotai/fastaistyle/blob/main/style.md Shows the preferred comment that explains why and the discouraged comment that restates the action. ```python self._stop_event.set() # ensures poll loop exits before socket close ``` ```python self._stop_event.set() # stop the event ``` -------------------------------- ### Single-line docstring (good vs bad) Source: https://github.com/answerdotai/fastaistyle/blob/main/style.md Shows the preferred brief single-line docstring and the discouraged multi-line docstring. ```python def run(self): "Run the thread." ... ``` ```python def run(self): """ Run the thread. """ ... ``` -------------------------------- ### Small helper vs repetitive block (good vs bad) Source: https://github.com/answerdotai/fastaistyle/blob/main/style.md Shows the preferred use of a small helper function and the discouraged repetitive inline call. ```python def _send_stream(self, name, text): ...; self._send_stream("stdout", out) ``` ```python self._send(sock, "stream", {"name": "stdout", "text": out}, parent) ``` -------------------------------- ### Check a specific path with chkstyle Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Runs the style checker on a given file or directory. ```bash chkstyle path/to/code/ ``` -------------------------------- ### Run tests with pytest Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Runs the test suite. There are currently no tests marked slow; running `pytest -m slow` selects 0 tests. ```bash pytest ``` -------------------------------- ### chkstyle CLI Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Command-line interface for checking and fixing code style violations in Python files and Jupyter notebooks. ```APIDOC ## [CLI] chkstyle ### Description Checks code style violations in Python files and Jupyter notebooks. Can also fix mechanical violations in place. ### Usage ```bash chkstyle [options] [paths...] ``` ### Options - `--show-rule` - Show each violation's rule id. - `--fix` - Fix selected mechanical violations in place. - `--fix-rule ` - Specify a rule to fix (repeatable). - `--skip-path-re ` - Skip paths matching a regex (uses Python `re.match`). - `--skip-path ` - Skip specific paths by name/path (repeatable). ### Arguments - `paths` - One or more files or folders to check. If omitted, checks the current directory. ### Examples ```bash chkstyle chkstyle path/to/code/ chkstyle path/to/code tests unit.py chkstyle --show-rule chkstyle --fix path/to/code chkstyle --fix --fix-rule dict-literal --fix-rule single-statement-body path/to/code chkstyle --skip-path-re 'test|migrations|vendor|src/gen' chkstyle --skip-path vendor --skip-path src/generated ``` ### Output Violations are printed with file paths, line numbers, the offending code, and a short fix hint. When violations are found, a reminder to prioritize clarity is also printed. For notebooks, violations show the cell ID and line number within the cell: ``` # notebook.ipynb:cell[abc123]:3: lhs assignment annotation x: int = 1 ``` ``` -------------------------------- ### Release after PRs are merged Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Releases the current version. Default is a patch bump; pass `minor` or `major` for other bumps. Tags the current version, pushes to trigger PyPI publish, then bumps for the next dev cycle. ```bash ./tools/release.sh # patch bump (default) ./tools/release.sh minor # minor bump ./tools/release.sh major # major bump ``` -------------------------------- ### Comprehension vs loop (good vs bad) Source: https://github.com/answerdotai/fastaistyle/blob/main/style.md Shows the preferred list comprehension and the discouraged manual loop with append. ```python ids = [m["header"]["msg_id"] for m in msgs] ``` ```python ids = [] for m in msgs: ids.append(m["header"]["msg_id"]) ``` -------------------------------- ### Single-line function body (good vs bad) Source: https://github.com/answerdotai/fastaistyle/blob/main/style.md Shows the preferred single-line body for small one-line functions and the discouraged multi-line form. ```python def _is_ready(self): return self._ready.is_set() ``` ```python def _is_ready(self): return self._ready.is_set() ``` -------------------------------- ### Show rule IDs with chkstyle --show-rule Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Displays each violation's rule ID, which is the name used by `ignore`, `--ignore`, and `--fix-rule`. ```bash chkstyle --show-rule ``` -------------------------------- ### Opt-out pragmas for style checker Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Shows all supported pragmas to ignore style violations: ignore a single line, ignore the next line, ignore a node, disable for a block, and skip an entire file or cell. The skip pragma must be in the first 5 lines and works even when the source cannot parse. ```python # Ignore a single line x: int = 1 # chkstyle: ignore # Ignore the next line # chkstyle: ignore y: int = 2 # Ignore every violation in the node starting here, e.g. a whole # carefully-formatted statement (a comment-only pragma marks the next line) registry = { # chkstyle: ignore-node 'text/plain': str, 'text/html': repr, } # Disable for a block # chkstyle: off carefully_formatted = { "alignment": "matters", "here": "for readability", } # chkstyle: on # Skip an entire file - or, in a notebook, the containing cell # (must be in the first 5 lines of the file or cell; works even # when the source cannot parse, unlike the other pragmas) # chkstyle: skip ``` -------------------------------- ### Single-line if statement (good vs bad) Source: https://github.com/answerdotai/fastaistyle/blob/main/style.md Shows the preferred single-line form for short if statements and the discouraged multi-line form. ```python if not data: return None ``` ```python if not data: return None ``` -------------------------------- ### Fix violations with chkstyle --fix Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Fixes selected mechanical violations in place. The second command shows how to specify which rules to fix using `--fix-rule`. ```bash chkstyle --fix path/to/code chkstyle --fix --fix-rule dict-literal --fix-rule single-statement-body path/to/code ``` -------------------------------- ### Skip paths by regex with chkstyle --skip-path-re Source: https://github.com/answerdotai/fastaistyle/blob/main/README.md Skips paths matching a regular expression. Uses Python `re.match`, so add `^` or `$` for exact matches. ```bash chkstyle --skip-path-re 'test|migrations|vendor|src/gen' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.