Detection of ambiguous indentation in python

PythonObject Oriented ProgrammingProgramming

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.

Command line usage

python –m tabnanny –q

For verbose output use –v switch

python –m tabnanny –v

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.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.

process_tokens() −

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().

Published on 20-Feb-2019 11:51:13