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

Don't mistake a following if for an elif #5296

Merged
merged 2 commits into from Jun 23, 2023

Conversation

konstin
Copy link
Member

@konstin konstin commented Jun 22, 2023

In the following code, the comment used to get wrongly associated with the if False since it looked like an elif. This fixes it by checking the indentation and adding a regression test

if True:
    pass
else:  # Comment
    if False:
        pass
    pass

Originally found in https://github.com/gradio-app/gradio/blob/1570b94a02d23d051ae137e0063974fd8a48b34e/gradio/external.py#L478

@konstin
Copy link
Member Author

konstin commented Jun 22, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 22, 2023

PR Check Results

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      7.0±0.02ms     5.8 MB/sec    1.00      7.0±0.02ms     5.8 MB/sec
formatter/numpy/ctypeslib.py               1.00   1477.0±1.46µs    11.3 MB/sec    1.00   1479.2±6.78µs    11.3 MB/sec
formatter/numpy/globals.py                 1.00    161.2±0.31µs    18.3 MB/sec    1.00    160.9±0.51µs    18.3 MB/sec
formatter/pydantic/types.py                1.01      3.5±0.01ms     7.3 MB/sec    1.00      3.5±0.01ms     7.4 MB/sec
linter/all-rules/large/dataset.py          1.00     13.6±0.04ms     3.0 MB/sec    1.00     13.6±0.05ms     3.0 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.4±0.01ms     4.8 MB/sec    1.01      3.5±0.00ms     4.8 MB/sec
linter/all-rules/numpy/globals.py          1.00    359.5±2.94µs     8.2 MB/sec    1.00    361.0±3.83µs     8.2 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.0±0.01ms     4.2 MB/sec    1.00      6.0±0.09ms     4.2 MB/sec
linter/default-rules/large/dataset.py      1.00      7.0±0.06ms     5.8 MB/sec    1.00      7.0±0.03ms     5.8 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1460.8±3.12µs    11.4 MB/sec    1.00   1460.7±3.39µs    11.4 MB/sec
linter/default-rules/numpy/globals.py      1.00    155.7±0.60µs    19.0 MB/sec    1.00    156.4±0.17µs    18.9 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.2±0.01ms     8.0 MB/sec    1.00      3.2±0.01ms     8.0 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.01     10.2±0.50ms     4.0 MB/sec    1.00     10.1±0.44ms     4.0 MB/sec
formatter/numpy/ctypeslib.py               1.00      2.2±0.10ms     7.7 MB/sec    1.01      2.2±0.18ms     7.6 MB/sec
formatter/numpy/globals.py                 1.02   256.0±30.50µs    11.5 MB/sec    1.00   250.1±18.42µs    11.8 MB/sec
formatter/pydantic/types.py                1.00      5.1±0.24ms     5.0 MB/sec    1.01      5.2±0.29ms     4.9 MB/sec
linter/all-rules/large/dataset.py          1.06     20.2±0.92ms     2.0 MB/sec    1.00     19.1±0.77ms     2.1 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.03      5.4±0.28ms     3.1 MB/sec    1.00      5.2±0.36ms     3.2 MB/sec
linter/all-rules/numpy/globals.py          1.05   651.2±60.73µs     4.5 MB/sec    1.00   620.9±37.60µs     4.8 MB/sec
linter/all-rules/pydantic/types.py         1.07      9.2±0.54ms     2.8 MB/sec    1.00      8.6±0.44ms     3.0 MB/sec
linter/default-rules/large/dataset.py      1.05     10.7±0.46ms     3.8 MB/sec    1.00     10.2±0.48ms     4.0 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.03      2.2±0.10ms     7.4 MB/sec    1.00      2.2±0.12ms     7.6 MB/sec
linter/default-rules/numpy/globals.py      1.04   268.7±16.06µs    11.0 MB/sec    1.00   258.2±15.73µs    11.4 MB/sec
linter/default-rules/pydantic/types.py     1.01      4.7±0.21ms     5.4 MB/sec    1.00      4.7±0.33ms     5.4 MB/sec

## Summary

This snippet used to panic because it expected to see a comma or something similar after the `2` but met the closing parentheses that is not part of the range and panicked
```python
a = {
    1: (2),
    # comment
    3: True,
}
```

Originally found in https://github.com/bolucat/Firefox/blob/636a717ef025c16434997dc89e42351ef740ee6b/testing/marionette/client/marionette_driver/geckoinstance.py#L109

This snippet is also the test plan.
In the following code, the comment used to get wrongly associated with the `if False` since it looked like an elif. This fixes it by checking the indentation and adding a regression test
```python
if True:
    pass
else:  # Comment
    if False:
        pass
    pass
```

Originally found in https://github.com/gradio-app/gradio/blob/1570b94a02d23d051ae137e0063974fd8a48b34e/gradio/external.py#L478
@konstin konstin force-pushed the Fix_formatter_panic_with_comment_after_parenthesized_dict_value branch from 8010d93 to b963c7f Compare June 22, 2023 12:58
@konstin konstin force-pushed the check_if_indentation_for_elif_comment branch from d6f0cd1 to 97b027a Compare June 22, 2023 12:58
@konstin konstin requested a review from MichaReiser June 22, 2023 14:28
Base automatically changed from Fix_formatter_panic_with_comment_after_parenthesized_dict_value to main June 22, 2023 14:52

# Regression test for formatter panic with comment after parenthesized dict value
# Originally found in https://github.com/bolucat/Firefox/blob/636a717ef025c16434997dc89e42351ef740ee6b/testing/marionette/client/marionette_driver/geckoinstance.py#L109
a = {
Copy link
Member

Choose a reason for hiding this comment

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

This fix seems unrelated to if/elif right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

just confirmed, it's also not part of the merge commit

CommentPlacement::Default(comment)
} else {
CommentPlacement::trailing(preceding, comment)
let base_if_indent =
Copy link
Member

Choose a reason for hiding this comment

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

Just an idea. It may be annoying to do because of all the type narrowing that's necessary and you would need to identify the right parent body but I thought it may be worth mentioning. The way to normally detect whether it is an elif or not... oh no.... It is not. These two programs have the same AST

if True:
    pass
elif False:
    pass
If(
    StmtIf {
        range: 0..38,
        test: Constant(
            ExprConstant {
                range: 3..7,
                value: Bool(
                    true,
                ),
                kind: None,
            },
        ),
        body: [
            Pass(
                StmtPass {
                    range: 13..17,
                },
            ),
        ],
        orelse: [
            If(
                StmtIf {
                    range: 18..38,
                    test: Constant(
                        ExprConstant {
                            range: 23..28,
                            value: Bool(
                                false,
                            ),
                            kind: None,
                        },
                    ),
                    body: [
                        Pass(
                            StmtPass {
                                range: 34..38,
                            },
                        ),
                    ],
                    orelse: [],
                },
            ),
        ],
    },
),
if True:
    pass
else:
    if False:
        pass
If(
        StmtIf {
            range: 41..91,
            test: Constant(
                ExprConstant {
                    range: 44..48,
                    value: Bool(
                        true,
                    ),
                    kind: None,
                },
            ),
            body: [
                Pass(
                    StmtPass {
                        range: 54..58,
                    },
                ),
            ],
            orelse: [
                If(
                    StmtIf {
                        range: 69..91,
                        test: Constant(
                            ExprConstant {
                                range: 72..77,
                                value: Bool(
                                    false,
                                ),
                                kind: None,
                            },
                        ),
                        body: [
                            Pass(
                                StmtPass {
                                    range: 87..91,
                                },
                            ),
                        ],
                        orelse: [],
                    },
                ),
            ],
        },
    ),

@MichaReiser MichaReiser added internal An internal refactor or improvement formatter Related to the formatter labels Jun 23, 2023
@konstin konstin merged commit 930f03d into main Jun 23, 2023
15 of 16 checks passed
@konstin konstin deleted the check_if_indentation_for_elif_comment branch June 23, 2023 08:07
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.

None yet

2 participants