I get 'continuation line under-indented for visual indent' error

21.8k views Asked by At

I get a continuation line under-indented for visual indent error on the code below:

    command = 'ffmpeg -i downloaded.mp4 -codec:v libx264 -codec:a \
            aac -map 0 -f ssegment -segment_format mpegts \
            -segment_list %s/%skbps.m3u8 -segment_time 10 \
            %s/%skbps_%%03d.ts' % (path, options['video_bitrate'],
                path, options['video_bitrate'])

How should this code be formatted to remove the error?

2

There are 2 answers

0
Jamie C On BEST ANSWER

This case is covered in PEP-8. In summary, to fix this you need to ensure that when you indent lines that are inside parentheses that you align the next line with the character that is after the opening parenthesis. For example, you should do this when you continue code in parentheses.

foo(a, b, c, 
    d, e)

You are currently doing it like this.

foo(a, b, c, 
  d, e)

To fix your indentation you should do it like this.

command = 'ffmpeg -i downloaded.mp4 -codec:v libx264 -codec:a \
           aac -map 0 -f ssegment -segment_format mpegts \
           -segment_list %s/%skbps.m3u8 -segment_time 10 \
           %s/%skbps_%%03d.ts' % (path, options['video_bitrate'],
                                  path, options['video_bitrate'])

From Stephen Rauch's answer you may have noticed that there is a little more to this as well. instead of using slashes to do line continuation you may surround the entire line in parenthesis and then break the lines into strings, python automatically joins adjacent string literals. For example if you had a string and you did this before.

greeting = 'Hello, \
            World!'

You should do this instead.

greet = ('Hello, '
         'World!')

This way is a lot more readable and a lot nicer for you to work with. Also do note, there is another PEP-8 approved way to continue parenthesis lines. Instead of doing.

foo(a, b, c, 
    d, e)

You may also do this.

foo(
    a, b, c, 
    d, e)

To do this you must leave the first line blank after the opening parentheses and then you MUST indent in from the current block to start your continuation. I hope this edit furthers your understanding more. If you want to learn more about python style, then just give the PEP guides a quick read through (they are the standard for python code).

0
Stephen Rauch On

When doing continuation of a string it is often good practice to use either implicit (or explicit) string concatenation. This gives better control over the created string. Also if the string is parenthesized, the continuation characters are not needed:

command = ('ffmpeg -i downloaded.mp4 -codec:v libx264 -codec:a '
           'aac -map 0 -f ssegment -segment_format mpegts '
           '-segment_list %s/%skbps.m3u8 -segment_time 10 '
           '%s/%skbps_%%03d.ts' % (
               path, options['video_bitrate'],
               path, options['video_bitrate']))

Side Note: pycharm has a very nice feature of live highlighting pep8 violation right in the editor.