Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP8: Dictionary values should have an extra indent #113

Closed
wants to merge 1 commit into from

Conversation

JelteF
Copy link

@JelteF JelteF commented Oct 9, 2016

This adds a new indenting rule to PEP8 to make it easy to differentiate between
keys and values of dictionaries. This was already shortly discussed in the
python-ideas mailinglist:
https://mail.python.org/pipermail/python-ideas/2016-October/042753.html

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

Unfortunately we couldn't find an account corresponding to your GitHub username at bugs.python.org (b.p.o). If you don't already have an account at b.p.o, please create one and make sure to add your GitHub username. If you do already have an account at b.p.o then please go there and under "Your Details" add your GitHub username.

And in case you haven't already, please make sure to sign the PSF contributor agreement (CLA); we can't legally look at your contribution until you have signed the CLA.

Once you have done everything that's needed, please reply here and someone will verify everything is in order.

@JelteF
Copy link
Author

JelteF commented Oct 9, 2016

Done


Yes::

# Keys alligned with opening delimiter, values have an extra indent
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/alligned/aligned/
Add a dot at the end (also below).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added changes for both.

' and even on the line after that.',
}

# Keys one and values with two hanging indents
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/one/with one/
s/hanging/levels of hanging/?

@warsaw
Copy link
Member

warsaw commented Oct 9, 2016

Thanks for the contribution!

I'm not sure we need to add this to PEP 8 though. IME it's a relatively rare occurrence, and I think we should leave it up to local conventions on how to deal with it. For example, if I was code reviewing some of the given examples, I'd probably ask for a refactoring where such long, multiline strings were assigned to local or globals and only the variable names added as values to the dict literal.

Do you know of any situations in the stdlib where readability is affected by this problem? I'd like to look at actual motivating examples to see whether this was a common enough problem to benefit from adding more guidelines to PEP 8.

'firstkey':
'a very very very very very long value',
'secondkey': 'a short value',
'thirdkey': 'a very very very'
Copy link
Member

@Mariatta Mariatta Oct 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to share what other IDE is already doing with respect to this issue.

PyCharm will automatically do this for you

'thirdkey':
    'a very very very'
    'long value that continues on the next line'
    'and even on the line after that.',

And this too

'thirdkey': 'a very very very'
            'long value that continues on the next line'
            'and even on the line after that.',

But it will not do this one, which is being considered as a 'yes' in this proposal.

'thirdkey': 'a very very very'
    'long value that continues on the next line'
    'and even on the line after that.',

Perhaps something to take into account when making decision about this.
Thanks :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the option is not done by pycharm the least as well, but I didn't want to make the change to opinionated.

@@ -121,6 +121,51 @@ Optional::
var_one, var_two,
var_three, var_four)

For dictionaries there is an extra rule. When placing a dictionary key on a new
Copy link
Member

@Mariatta Mariatta Oct 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicky 😅 there should be a comma
For dictionaries, there is ...

... on a new line, an additional ...

@JelteF
Copy link
Author

JelteF commented Oct 9, 2016

@warsaw, I don't think it occurs very often and so I understand your point. The places where I found it was when writing an JSON response in dict form and having the error message passing the 80 character limit. This can even happen for relatively short strings when the code is nested a lot.

However, currently when reading PEP8 it does not even "allow" other behaviour (I know it's just a guideline), which I think would at least be a good addition.

@warsaw
Copy link
Member

warsaw commented Oct 9, 2016

On Oct 09, 2016, at 08:47 AM, Jelte Fennema wrote:

However, currently when reading PEP8 it does not even "allow" other behaviour
(I know it's just a guideline), which I think would at least be a good
addition.

Re-read the "A Foolish Consistency..." section. :)

Ultimately, you do what you have to to make the code as readable as possible.

@blueyed
Copy link

blueyed commented Oct 9, 2016

Ultimately, you do what you have to to make the code as readable as possible.

Agreed. But then it's also a matter of tools like pycodestyle accepting it (and not having to add a # noqa for it)
(The issue originated from Vimjas/vim-python-pep8-indent#61 (comment) in that regard)

@gvanrossum
Copy link
Member

gvanrossum commented Oct 9, 2016 via email

@warsaw
Copy link
Member

warsaw commented Oct 10, 2016

👍

@gvanrossum
Copy link
Member

Looks like after all we don't want PEP 8 to require a particular style for this specific case. It's pretty obscure anyway. If you don't like that error in your flake8 output just configure the tool to ignore this error. It's not a mortal sin. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants