uses asserts

Issue #316 invalid
Matteo Kloiber created an issue

Although asserts are an easy way to ensure that your program has the right arguments, it is removed when using optimization. I know that only a few python programs actually use optimization (because it only removes asserts) but I believe you shouldn't use asserts. Here is more information about asserts in python code.

So far, I've found out that you use an assert in on line 490. You might want to keep it but I think throwing a KeyError is better. Just imagine somebody used your module with optimization and help() is for some reason called without any argument. It would always say "Don't know topic ...".

Here is what you should do instead.

def help(self, error=None, topic=None, parser=None):
    """Display an error message, or the named topic."""
    if error:
        print("Use 'coverage help' for help.")
    elif parser:
    elif topic:
        help_msg = HELP_TOPICS.get(topic, '').strip()
        if help_msg:
            print(help_msg % self.covpkg.__dict__)
            print("Don't know topic %r" % topic)
        raise KeyError("At least one argument needs to be given.")

Comments (1)

  1. Ned Batchelder repo owner

    Asserts are meant for making claims about how your code is being called. This is an internal-use function. The assert is appropriate.

  2. Log in to comment