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

PythonAssertPep8

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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!')

Solution 2 - Python

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 ..

Solution 3 - Python

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.

Solution 4 - Python

This is described in pep8 in the end of the Maximum Line Length section.

> Backslashes may still be appropriate at times. For example, [...] Another such case is with assert statements.

So the pep8 recommendation is to do as mgilsons first example, e.g.:

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

Solution 5 - Python

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

This gives you the implied line continuation from parentheses that is recommended by PEP 8 without breaking the assert behavior.

Alternatively:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstantonkView Question on Stackoverflow
Solution 1 - PythonmgilsonView Answer on Stackoverflow
Solution 2 - PythonJoran BeasleyView Answer on Stackoverflow
Solution 3 - PythonGareth LattyView Answer on Stackoverflow
Solution 4 - PythonOlsgaardView Answer on Stackoverflow
Solution 5 - PythonAndrew ClarkView Answer on Stackoverflow