Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coverage: pragma: no branch with multiline statement

For the python coverage package, a missing else can be ignored for the resulting coverage by using # pragma: no branch.

It seems, that this is not working when breaking a long if statement in multiple lines like this:

if this_is_a_verylong_boolean_expression == True and another_long_expression \
    and here_another_expression:  # pragma: no branch
    do_something()

Is this a bug of coverage or intended behavior? Is there a way to handle these kind of multiline statements and ignore the missing branch in the coverage? Or do I just have to accept the missing branches in my coverage summary?

like image 620
Igle Avatar asked Oct 28 '25 05:10

Igle


1 Answers

I realize this is not exactly what you asked, but I would recommend that you refactor that line to not be so long. I would guess that the code will be much more readable and maintainable if you change it to:

some_condition = this_is_a_verylong_boolean_expression
another_test = another_long_expression
last_check = here_another_expression
if some_condition and another_test and last_check:     # pragma: no branch
    do_something()

This gives you a chance to give these expressions mnemonic names.

On the coverage.py question itself: you can make the pragma work like this:

if (this_is_a_verylong_boolean_expression == True and another_long_expression   # pragma: no branch
    and here_another_expression):
    do_something()
like image 124
Ned Batchelder Avatar answered Oct 30 '25 22:10

Ned Batchelder