2

For the following code I am getting the below error:

config = {
    'bucket': json.loads(record['body'])
                ['Records'][0]['s3']['bucket']['name'],
    'key': json.loads(record['body'])
           ['Records'][0]['s3']['object']['key']
}
E131 continuation line unaligned for hanging indent
                              ['Records'][0]['s3']['bucket']['name'],

E131 continuation line unaligned for hanging indent
                           ['Records'][0]['s3']['object']['key']

I have tried a few options including the below - but it is not working:

config = {
    'bucket': json.loads(
                        record['body']
                        )
                        ['Records'][0]['s3']['bucket']['name'],
    'key': json.loads(record['body'])
           ['Records'][0]['s3']['object']['key']
}

I have also tried + \ at the end of the line but does not work also

nsc060
  • 417
  • 6
  • 16

2 Answers2

4

To conform with PEP8, the below worked for this scenario:

config = {
    'bucket': json.loads(record['body'])
    ['Records'][0]['s3']['bucket']['name'],

    'key': json.loads(record['body'])
    ['Records'][0]['s3']['object']['key']
}
nsc060
  • 417
  • 6
  • 16
  • Yes this is the best way, I just check with my IDE. Just get the same result as much as like you answer. – Kushan Gunasekera Jul 13 '19 at 13:34
  • This may conform to PEP8, but the real hint here should be to store `json.loads(record['body'])['Records'][0]['s3']` in a variable, since it is duplicate and contains a parsing-step. This is actually not very readable. – Jeppe Oct 27 '20 at 12:13
2

Try this, it should be either this format

config = {
    'bucket': json.loads(record['body'])['Records'][0]['s3']['bucket']['name'],
    'key': json.loads(record['body'])['Records'][0]['s3']['object']['key']
}

or in this,

config = {
    'bucket': json.loads(record['body']) \
                ['Records'][0]['s3']['bucket']['name'],
    'key': json.loads(record['body']) \
           ['Records'][0]['s3']['object']['key']
}

For more information, please check What is PEP8's E128: continuation line under-indented for visual indent? question.

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
  • 1
    Thanks. For option 1, I have `max_line_length` of 79 so would not be an option. For option 2 `\` was redundant between brackets. I have found an answer and will post that. – nsc060 Jul 13 '19 at 13:29