Search code examples
pythonindentationpep8autopep8

autopep8 indenting inconsistently


I'm implementing autopep8 into an existing project and sometimes longer lines are being formatting weirdly. For example, there is this code snippet:

client_data={'id': str(self.user.client.client_id), 'type': self.user.client.client_type},

which is being formatted like:

self.setup_auth(UserProxy(self.user.sub, [],
                          client_data={
    'id': str(
        self.user.client.client_id),
    'type': self.user.client.client_type},
    roles=[]))

So the arguments passed to UserProxy have two elements on the first line, then the third element is on a new line indented correctly but the elements of the dictionary are only indented once instead of being indented once from the line it came off of.

If I try to manually fix it, it just reverts back.

Does any one know how I can improve the indentation for this case?

Edit: I am running autopep8 with this in pyproject.toml

[tool.autopep8]
max_line_length = 88
in-place = true
recursive = true
aggressive = 3

Solution

  • The indentation is somewhat consistent, just not what you'd like. There's not much you can do with your original code to conform to PEP8 because there's so much going on in one line. Break it up - it'll be more readable and make autopep8 happy.

    client_data = {
        'id': str(self.user.client.client_id),
        'type': self.user.client.client_type }
    proxy = UserProxy(self.user.sub, [], client_data=client_data, roles=[])
    self.setup_auth(proxy)