Skip to content

format StmtWith#5350

Merged
MichaReiser merged 7 commits intoastral-sh:mainfrom
davidszotten:format-stmt-with
Jun 26, 2023
Merged

format StmtWith#5350
MichaReiser merged 7 commits intoastral-sh:mainfrom
davidszotten:format-stmt-with

Conversation

@davidszotten
Copy link
Contributor

Summary

format StmtWith (and WithItem)

one outstanding todo (see the bottom of with.py)

Test Plan

snapshots

@davidszotten davidszotten marked this pull request as ready for review June 24, 2023 07:35
@github-actions
Copy link
Contributor

github-actions bot commented Jun 24, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      6.6±0.01ms     6.1 MB/sec    1.02      6.8±0.02ms     6.0 MB/sec
formatter/numpy/ctypeslib.py               1.00  1553.9±48.63µs    10.7 MB/sec    1.00   1554.2±5.23µs    10.7 MB/sec
formatter/numpy/globals.py                 1.00    184.2±1.03µs    16.0 MB/sec    1.03    189.0±0.44µs    15.6 MB/sec
formatter/pydantic/types.py                1.00      3.5±0.01ms     7.3 MB/sec    1.00      3.5±0.02ms     7.3 MB/sec
linter/all-rules/large/dataset.py          1.00     13.1±0.11ms     3.1 MB/sec    1.00     13.1±0.06ms     3.1 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.3±0.01ms     5.0 MB/sec    1.00      3.3±0.01ms     5.0 MB/sec
linter/all-rules/numpy/globals.py          1.00    426.4±0.40µs     6.9 MB/sec    1.00    426.9±0.88µs     6.9 MB/sec
linter/all-rules/pydantic/types.py         1.00      5.8±0.02ms     4.4 MB/sec    1.00      5.8±0.02ms     4.4 MB/sec
linter/default-rules/large/dataset.py      1.00      6.6±0.02ms     6.2 MB/sec    1.01      6.6±0.01ms     6.2 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1443.4±2.42µs    11.5 MB/sec    1.01   1455.1±3.12µs    11.4 MB/sec
linter/default-rules/numpy/globals.py      1.00    162.2±0.20µs    18.2 MB/sec    1.00    162.6±0.64µs    18.1 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.0±0.01ms     8.5 MB/sec    1.00      3.0±0.01ms     8.5 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00     10.9±0.54ms     3.7 MB/sec    1.05     11.5±0.65ms     3.5 MB/sec
formatter/numpy/ctypeslib.py               1.00      2.5±0.14ms     6.8 MB/sec    1.02      2.5±0.15ms     6.6 MB/sec
formatter/numpy/globals.py                 1.00   298.8±27.70µs     9.9 MB/sec    1.06   317.5±40.02µs     9.3 MB/sec
formatter/pydantic/types.py                1.00      5.4±0.29ms     4.7 MB/sec    1.05      5.7±0.39ms     4.5 MB/sec
linter/all-rules/large/dataset.py          1.00     19.7±0.75ms     2.1 MB/sec    1.09     21.4±0.78ms  1943.2 KB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      5.5±0.24ms     3.0 MB/sec    1.03      5.7±0.33ms     2.9 MB/sec
linter/all-rules/numpy/globals.py          1.00   669.7±34.54µs     4.4 MB/sec    1.00   667.8±30.88µs     4.4 MB/sec
linter/all-rules/pydantic/types.py         1.01      9.5±0.32ms     2.7 MB/sec    1.00      9.4±0.44ms     2.7 MB/sec
linter/default-rules/large/dataset.py      1.00     10.8±0.35ms     3.8 MB/sec    1.02     11.0±0.39ms     3.7 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00      2.3±0.08ms     7.2 MB/sec    1.03      2.4±0.16ms     7.0 MB/sec
linter/default-rules/numpy/globals.py      1.02   280.9±17.45µs    10.5 MB/sec    1.00   276.3±13.74µs    10.7 MB/sec
linter/default-rules/pydantic/types.py     1.00      4.9±0.15ms     5.2 MB/sec    1.07      5.2±0.39ms     4.9 MB/sec

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

This looks great. Thank you! I've one suggestion on how to format the with items that span over multiple lines. But this is otherwise good to go.

with (a): # should remove brackets
...

# TODO: black doesn't wrap this, but maybe we want to anyway?
Copy link
Member

Choose a reason for hiding this comment

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

This sounds reasonable to me. We should make it wrap the same as for function definition

# All flat
def test(a, b, c): 
	pass

# parentheses expanded
def test(
	a, b, c
): 
	pass

# all expanded
def test(
	a, 
	b, 
	c
): 
	pass

You can achieve this by using two nested groups:

  • outer group: For the parentheses and indent
  • inner group: Around the with items

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh wait. when there is an as, wrapping isn't allowed in python < 3.9 (3.8 isn't eol until oct '24)

Copy link
Member

Choose a reason for hiding this comment

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

We can change that when adding support for targeting different python versions. It's something that we'll need to add to the formatter options.


with call(arg):
call(arg)
# fmt: skip
Copy link
Member

Choose a reason for hiding this comment

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

Is this fmt: skip intentional?

Copy link
Contributor Author

@davidszotten davidszotten Jun 24, 2023

Choose a reason for hiding this comment

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

oops, this whole block is leftover from debugging. will remove

Comment on lines +21 to +29
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
let trailing_items_comments = body.first().map(|body_start| {
let before_body = dangling_comments
.partition_point(|comment| comment.slice().end() < body_start.start());

let (trailing_items_comments, rest) = dangling_comments.split_at(before_body);
debug_assert!(rest.is_empty());
trailing_items_comments
});
Copy link
Member

Choose a reason for hiding this comment

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

Do we need the partition_point or can we format all dangling_comments at once? Because it seems there's only a single place where dangling comments can appear (and thus, splitting is unnecessary).

Comment on lines +38 to +34
group(&format_args![
if_group_breaks(&text("(")),
soft_block_indent(&joined_items),
if_group_breaks(&text(")")),
]),
Copy link
Member

Choose a reason for hiding this comment

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

Nit: You can use optional_parentheses(&joined_items) if this lands after #5328

@davidszotten davidszotten force-pushed the format-stmt-with branch 2 times, most recently from 8ef512f to 2458c3f Compare June 26, 2023 10:25
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Thank you!

@MichaReiser MichaReiser merged commit d00559e into astral-sh:main Jun 26, 2023
@MichaReiser MichaReiser added internal An internal refactor or improvement formatter Related to the formatter labels Jun 26, 2023
@davidszotten davidszotten deleted the format-stmt-with branch July 7, 2023 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants