google / google/yapf

indent_dictionary_value after a comment breaks spaces around kw args

Open
#546 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

Here is one case where an inline comment between dictionary key and value introduces unrelated changes that are no longer PEP8-compliant.

**long-line.py**:
```python
return {
'link': # the link
'https://documents.example.com' + flask.url_for('document.index', document_id=1, document_title='foo'),
}
```

**yapf command to reproduce**
```
$ yapf --style '{based_on_style:pep8, indent_dictionary_value:True}' --diff long-line.py
--- long-line.py (original)
+++ long-line.py (reformatted)
@@ -1,4 +1,5 @@
return {
'link': # the link
- 'https://documents.example.com' + flask.url_for('document.index', document_id=1, document_title='foo'),
+ 'https://documents.example.com' + flask.url_for(
+ 'document.index', document_id = 1, document_title = 'foo'),
}
```

yapf correctly wraps the line to 80 chars and correctly indents the dictionary value... but it inserts unwanted spaces in the function's keyword args: `document_id = 1, document_title = 'foo'` where there were none before.

**Sub-par Workarounds**

* This is caused by the presence of the inline comment; deleting the comment produces somewhat different but at least PEP8-compliant code:
```
return {
'link':
- 'https://documents.example.com' + flask.url_for('document.index', document_id=1, document_title='foo'),
+ 'https://documents.example.com' +
+ flask.url_for('document.index', document_id=1, document_title='foo'),
}
```
* If we use the pep8 style's default value `indent_dictionary_value=True`, then yapf bails and does not re-format the line at all. I think this different issue is covered in #523. (My motivation for forcing `indent_dictionary_value=True` is to workaround #392)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.