On this page
tabnanny
Prev: syslog | Up: stdlib_module | Next: tarfile
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — tabnanny
Tags: #public #python3 #stdlib
Import
import tabnanny
Overview
Ambiguous-indentation checker (check(), process_tokens(), or python3 -m tabnanny <path>) that scans Python source files for mixed tabs and spaces that could mean different things depending on tab-stop width. Run tabnanny as a quick legacy-code sanity check; for modern codebases, a linter like ruff or flake8 catches indentation issues alongside everything else.
Quick Start
from pathlib import Path
import tabnanny
import tempfile
with tempfile.TemporaryDirectory() as tmp:
clean = Path(tmp) / "ok.py"
clean.write_text("def f():\n return 1\n", encoding="utf-8")
tabnanny.check(str(clean)) # prints nothing when indentation is unambiguous
Implementation Walkthrough
1. Checking a Directory Tree from the CLI
Run python3 -m tabnanny src/ (add -v for verbose output) to scan every .py file recursively and report any file with ambiguous tab/space mixing.
Gotchas & Boundaries
- Python 3 already rejects mixed tabs/spaces at compile time: Python 3 raises
TabErrorautomatically when tabs and spaces are mixed inconsistently in the same block, makingtabnannymostly relevant for auditing legacy Python 2→3 migrations. - Prints diagnostics instead of returning a boolean:
tabnanny.check()writes findings tosys.stdoutrather than raising or returning a status code.
Common Functions
- tabnanny.check() —
check(file)— If file_or_dir is a directory and not a symbolic link, then recursively descend the directory tree named by file_or_dir, checking all .py files along the way. If file_or_dir is an ordinary Python source file, it is - tabnanny.process_tokens() —
process_tokens(tokens)—tabnanny.process_tokens()function.
Classes & Objects
- tabnanny.NannyNag — Raised by process_tokens() if detecting an ambiguous indent. (Methods:
.add_note(),.get_line(),.get_lineno(),.get_msg(),.with_traceback())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/syslog
- tuts/python3/stdlib_module/tabnanny/check
- tuts/python3/stdlib_module/tabnanny/NannyNag
- tuts/python3/stdlib_module/tabnanny/process_tokens
- tuts/python3/stdlib_module/tarfile
- tuts/python3/tutorials/intermediate_python_and_software_engineering/type_hinting_and_static_analysis
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/syslog Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tabnanny/check Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tabnanny/NannyNag Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tabnanny/process_tokens Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tarfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sys Linked here; shared tags: #stdlib