Skip to content

Commit 29a0c10

Browse files
authored
Use BestFit layout even for attributes with a short name (#6872)
1 parent 15b7525 commit 29a0c10

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/unsplittable.py

+5
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ def test():
6565

6666
m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person, blank=True)
6767
m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyFieldAttributeChainFieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeld
68+
69+
def test():
70+
if True:
71+
VLM_m2m = VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through
72+
allows_group_by_select_index = self.connection.features.allows_group_by_select_index

crates/ruff_python_formatter/src/expression/expr_attribute.rs

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ impl NeedsParentheses for ExprAttribute {
153153
OptionalParentheses::Multiline
154154
} else if context.comments().has_dangling(self) {
155155
OptionalParentheses::Always
156+
} else if self.value.is_name_expr() {
157+
OptionalParentheses::BestFit
156158
} else {
157159
self.value.needs_parentheses(self.into(), context)
158160
}

crates/ruff_python_formatter/tests/snapshots/format@expression__unsplittable.py.snap

+15
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def test():
7171
7272
m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person, blank=True)
7373
m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyFieldAttributeChainFieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeld
74+
75+
def test():
76+
if True:
77+
VLM_m2m = VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through
78+
allows_group_by_select_index = self.connection.features.allows_group_by_select_index
7479
```
7580

7681
## Output
@@ -152,6 +157,16 @@ m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = (
152157
models.ManyToManyField(Person, blank=True)
153158
)
154159
m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyFieldAttributeChainFieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeld
160+
161+
162+
def test():
163+
if True:
164+
VLM_m2m = (
165+
VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through
166+
)
167+
allows_group_by_select_index = (
168+
self.connection.features.allows_group_by_select_index
169+
)
155170
```
156171

157172

0 commit comments

Comments
 (0)