Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f62d929
features to align assignment operators and dictionary colons
lizawang Aug 18, 2022
7a7de67
update the changelog and readme
lizawang Aug 30, 2022
94cca9b
small fixes
lizawang Aug 30, 2022
2ecde17
Update README.rst
lizawang Aug 30, 2022
e2181d8
fix readme
lizawang Aug 30, 2022
4275bb7
fix readme
lizawang Aug 30, 2022
82f1326
fix align_dict_colon
lizawang Sep 13, 2022
6c273dc
add test for the case when the dict starts with a comment
lizawang Sep 15, 2022
db00217
branch for assignment alignment
lizawang Oct 3, 2022
beec868
run the assignment align over the yapf codes
lizawang Oct 3, 2022
aa3f8a7
change the format back to yapf-based
lizawang Jan 3, 2023
ad90616
change the format back to yapf-based 2
lizawang Jan 3, 2023
23c8a80
Started addressing PR #1030. Easy format fixes applied.
Andrea-Oliveri Aug 12, 2025
5f77974
Added new tests for alignment edge cases
Andrea-Oliveri Aug 12, 2025
f9822f7
Refactored _AlignTrailingComments to reuse logic in _AlignAssignment
Andrea-Oliveri Aug 12, 2025
901ff12
Updated name of knob in style
Andrea-Oliveri Aug 13, 2025
549fdd1
Removed unused imports
Andrea-Oliveri Aug 13, 2025
677ac01
Refactored _AlignAssignment
Andrea-Oliveri Aug 13, 2025
7b6293e
Added more tests for assignement alignment
Andrea-Oliveri Aug 13, 2025
99663f1
Ran yapf on codebase
Andrea-Oliveri Aug 13, 2025
5c46a86
Solved merge conflicts with yapf/main
Andrea-Oliveri Aug 13, 2025
ac9ad91
Fixed error during merge conflict which left a comment line in
Andrea-Oliveri Aug 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,46 @@

## (0.41.0) UNRELEASED
### Added
- New `ALIGN_ASSIGNMENT` and `ALIGN_ASSIGNMENT_RESTART_AFTER_COMMENTS` flags.
`ALIGN_ASSIGNMENT` is a new knob that enables alignment of assignment
operators in consecutive lines.

A blank line or multiline object (function, multiline dictionary, ...) will
always interrupt the alignment block and start a new one. Comments can also
optionally interrupt the block, a behaviour that can be turned on with
`ALIGN_ASSIGNMENT_RESTART_AFTER_COMMENTS`.

For example, with `ALIGN_ASSIGNMENT` off:
```
val_first = 1
val_second += 2
# comment
val_third = 3
```

will be formatted as:
```
val_first = 1
val_second += 2
# comment
val_third = 3
```
whereas with `ALIGN_ASSIGNMENT` on and
`ALIGN_ASSIGNMENT_RESTART_AFTER_COMMENTS` off we get:
```
val_first = 1
val_second += 2
# comment
val_third = 3
```
while if they are both on:
```
val_first = 1
val_second += 2
# comment
val_third = 3
```

- New `DISABLE_SPLIT_LIST_WITH_COMMENT` flag.
`DISABLE_SPLIT_LIST_WITH_COMMENT` is a new knob that changes the
behavior of splitting a list when a comment is present inside the list.
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ Sam Clegg <[email protected]>
Łukasz Langa <[email protected]>
Oleg Butuzov <[email protected]>
Mauricio Herrera Cuadra <[email protected]>
Xiao Wang <[email protected]>
Andrea Oliveri <[email protected]>
Kyle Gottfried <[email protected]>
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,64 @@ optional arguments:

## Knobs

#### `ALIGN_ASSIGNMENT`

> Align assignment or augmented assignment operators. If there is a blank
> line or objects with newline entries in between, it will start new block
> alignment. For exemple:

```python
val_first = 1
val_second += 2
object = {
entry1:1,
entry2:2,
entry3:3,
}
val_third = 3
```

> will be formatted as follows:

```python
val_first = 1
val_second += 2
object = {
entry1: 1,
entry2: 2,
entry3: 3,
}
val_third = 3
```

#### `ALIGN_ASSIGNMENT_RESTART_AFTER_COMMENTS`

> Allows to choose whether to align assignments before and after a comment
> line independently. For exemple, if off:

```python
val_first = 1
val_second += 2
# comment
val_third = 3
```

will be aligned as:
```python
val_first = 1
val_second += 2
# comment
val_third = 3
```

whereas if we turn it on:
```python
val_first = 1
val_second += 2
# comment
val_third = 3
```

#### `ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT`

> Align closing bracket with visual indentation.
Expand Down
23 changes: 23 additions & 0 deletions yapf/yapflib/format_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,26 @@ def is_pytype_comment(self):
def is_copybara_comment(self):
return self.is_comment and re.match(
r'#.*\bcopybara:\s*(strip|insert|replace)', self.value)

@property
def is_assign(self):
return subtypes.ASSIGN_OPERATOR in self.subtypes

@property
@lru_cache()
def is_augassign(self):
return self.value in frozenset({
'+=',
'-=',
'*=',
'@=',
'/=',
'//=',
'%=',
'<<=',
'>>=',
'|=',
'&=',
'^=',
'**=',
})
Loading