Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a python assert statement that complies with PEP8?

How does one format a long assert statement that complies with PEP8? Please ignore the contrived nature of my example.

def afunc(some_param_name):
    assert isinstance(some_param_name, SomeClassName), 'some_param_name must be an instance of SomeClassName, silly goose!'

One cannot wrap it in parenthesis, because that changes the behavior of the assert statement since it is a keyword, not a builtin function.

like image 329
stantonk Avatar asked Apr 17 '13 16:04

stantonk


People also ask

How do you assert a statement in Python?

The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.

Should I use assert in Python production code?

"Asserts should be used to test conditions that should never happen." Yes. And the meaning of the second "should" is: If this happens, the program code is incorrect.

How do you use assert statements?

assert statement has a condition and if the condition is not satisfied the program will stop and give AssertionError . assert statement can also have a condition and a optional error message. If the condition is not satisfied assert stops the program and gives AssertionError along with the error message.

How do you assert in Python stackoverflow?

You can try the following in a interactive shell: >>> assert 5 > 2 >>> assert 2 > 5 Traceback (most recent call last): File "<string>", line 1, in <fragment> builtins. AssertionError: The first statement does nothing, while the second raises an exception.


3 Answers

It's important to remember that PEP8 is only a guideline and even states that there are times when the rules should be broken.

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply.

With that in mind, I would probably write this with old style line continuation:

def afunc(some_param_name):
    assert isinstance(some_param_name, SomeClassName), \ 
           'some_param_name must be an instance of SomeClassName, silly goose!'

If that doesn't sit well with you (or your linter), you can always do:

def afunc(some_param_name):
    assert isinstance(some_param_name, SomeClassName), ( 
           'some_param_name must be an instance of SomeClassName, silly goose!')

or even:

def afunc(some_param_name):
    assert isinstance(some_param_name, SomeClassName), ( 
           'some_param_name must be an instance of SomeClassName, '
           'silly goose!')
like image 129
mgilson Avatar answered Nov 10 '22 03:11

mgilson


ERR_MESSAGE_01 = '''
Some really long error message
'''

assert condition(a,b), ERR_MESSAGE_01

Is how I do it ...and I think that complies fine ..

like image 24
Joran Beasley Avatar answered Nov 10 '22 02:11

Joran Beasley


It's worth noting that it is possible to wrap with parenthesis, just not in the way you are thinking.

assert isinstance(some_param_name, 
                  SomeClassName), ('some_param_name must be an instance of '
                                   'SomeClassName, silly goose!')

I wouldn't argue it's particularly readable, however. In some cases, it might be the right option.

like image 28
Gareth Latty Avatar answered Nov 10 '22 01:11

Gareth Latty