0

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
Reimus Klinsman
  • 898
  • 2
  • 12
  • 23

3 Answers3

3

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)
Woodford
  • 3,746
  • 1
  • 15
  • 29
1

As a side note, I'm not sure how attached you are to autopep8, but here's what Black looks like on the same code:

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

I like the way that looks.

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
  • 1
    Black was another possibility but I want to avoid it due to the fact that it tends to add a lot of unnecessary lines. On some test code autopep8 added 80 new lines of code while black added over 400. Black is definitely handling this case better than autopep8 though – Reimus Klinsman Aug 28 '21 at 00:11
0

Looks like your choices are:

  1. Use --SELECT = <Features> to select only the types of things you want to fix.
  2. Provide a config file as specified here and here. In a config file you can select the types of things to ignore.
bfris
  • 5,272
  • 1
  • 20
  • 37
  • Thanks. I attempted to have autopep8 ignore E501 which ignores line length but I would like to keep that rule, I just want the way that it reforms the line to be different. I am hoping that there might be more granularity with how the rules are applied like being able to create a script to overwrite how E501 works but I'm not finding anything like that. So far it looks like Woodfords suggestion is the best fit at this time though I am introducing this to an already existing project so some code is going to look off until it is manually fixed – Reimus Klinsman Aug 27 '21 at 21:59
  • I have to admit @Woodford's answer is pretty good. – bfris Aug 27 '21 at 22:02