90

I'm reading bottle.py source code. It's a web framework, with only 3000+ lines python code. So cool.

I found some code like this:

class ServerAdapter(object):
    quiet = False
    def __init__(self, host='127.0.0.1', port=8080, **config):
        self.options = config
        self.host = host
        self.port = int(port)

    def run(self, handler): # pragma: no cover
        pass
    ... 

What does the # pragma: no cover mean? I can't find any introduce about the pragma syntax in the python documentations.

polarise
  • 2,303
  • 1
  • 19
  • 28
hhbcarl
  • 970
  • 1
  • 6
  • 8
  • 12
    I believe it is a comment for pycoverage to tell it to skip that method – Joran Beasley Aug 16 '12 at 17:34
  • 5
    The `# pragma` is definitely nothing that directly works in Python. For Python, it is **only a comment**. There is nothing similar to say C preprocesor for that. – pepr Aug 16 '12 at 17:55

2 Answers2

122

It is apparenly related to the coverage.py:

Coverage.py is a tool for measuring code coverage of Python programs. It monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not.

That exact # pragma: no cover is the hint that the part of code should be ignored by the tool -- see Excluding code from coverage .

pepr
  • 20,112
  • 15
  • 76
  • 139
17

For Python, it's simply a comment. It might be an annotation targeted at some external tool, which reads and analyzes Python code, similar, for example, to doctest's #doctest: +ELLIPSIS annotations or PyLint's # pylint: disable=W0613 style annotations.

Dirk
  • 30,623
  • 8
  • 82
  • 102