Stylistic question regarding no-op code and tests

Chris Angelico rosuav at gmail.com
Wed Oct 14 21:58:47 EDT 2015


On Thu, Oct 15, 2015 at 12:49 PM, Jason Swails <jason.swails at gmail.com> wrote:
> My question is, what do you think of the "else: pass" statement?  It is a
> complete no-op and is syntactically equivalent to the same code with those
> lines removed.  Up until earlier today, I would look at that and cringe (I
> still do, a little).
>
> What I recently realized, though, that what this construct allows is for the
> coverage testing package (which I have recently started employing for my
> project... thanks Ned and others!) to detect whether or not both code paths
> are covered in the test suite.

If that's the case, it absolutely MUST have some acknowledgement in
the code. Maybe a comment, or maybe replace the 'pass' with an
'assert', or (a neat trick I've used in a few places) a statistical
counter:

    if filename is not None:
        process_file(filename)
        files_processed += 1
    else:
        nonfiles_not_processed += 1

Your coverage tester should be just as happy with that, and it doesn't
look pointless.

ChrisA



More information about the Python-list mailing list