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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)