You may have code in your project that you know won’t be executed, and you want
to tell coverage.py to ignore it. For example, you may have debugging-only
code that won’t be executed during your unit tests. You can tell coverage.py to
exclude this code during reporting so that it doesn’t clutter your reports with
noise about code that you don’t need to hear about.

Coverage.py will look for comments marking clauses for exclusion. In this
code, the “if debug” clause is excluded from reporting:

Any line with a comment of “pragma: no cover” is excluded. If that line
introduces a clause, for example, an if clause, or a function or class
definition, then the entire clause is also excluded. Here the __repr__
function is not reported as missing:

Coverage.py identifies exclusions by matching lines against a list of regular
expressions. Using configuration files or the coverage
API, you can add to that list. This is useful if you have
often-used constructs to exclude that can be matched with a regex. You can
exclude them all at once without littering your code with exclusion pragmas.

For example, you might decide that __repr__ functions are usually only used in
debugging code, and are uninteresting to test themselves. You could exclude
all of them by adding a regex to the exclusion list:

Note that when using the exclude_lines option in a configuration file, you
are taking control of the entire list of regexes, so you need to re-specify the
default “pragma: no cover” match if you still want it to apply.