Indentation is an important feature of Python syntax. Code blocks in function, class or loops are required to follow same indent level for statements in it. The tabnanny module in Python's standard library is able to detect any violation in this stipulation.
This module is primarily intended to be used in command line mode with –m switch. However, it can also be imported in an interpreter session.
python –m tabnanny –q example.py
For verbose output use –v switch
python –m tabnanny –v example.py
Following functions are defined in tabnanny module for checking indentation programmatically.
This function checks for ambiguously indented lines in a given file. You can also pass a directory as parameter. All file in it will be recursively checked.
import tabnanny tabnanny.check('example.py')
tabnanny.verbose − This flag indicates whether to print verbose messages. This is incremented by the -v option if called as a script.
tabnanny.filename_only − This flag indicates whether to print only the filenames of files containing whitespace related problems. This is set to true by the -q option if called as a script.
This function is used by check() to process tokens generated by the tokenize module. This function raises NannyNag exception if an ambiguous indent is detected. It is captured and handled in check().