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

Add autofix for PIE800 #8668

Merged
merged 5 commits into from Nov 15, 2023
Merged

Add autofix for PIE800 #8668

merged 5 commits into from Nov 15, 2023

Conversation

alanhdu
Copy link
Contributor

@alanhdu alanhdu commented Nov 14, 2023

Summary

This adds an autofix for PIE800 (unnecessary spread) -- whenever we see a **{...} inside another dictionary literal, just delete the **{ and } to inline the key-value pairs. So {"a": "b", **{"c": "d"}} becomes just {"a": "b", "c": "d"}.

I have enabled this just for preview mode.

Test Plan

Updated the preview snapshot test.

Whenever we see a **{...} inside another dictionary literal, just
delete the `**{` and `}` to inline the key-value pairs
Comment on lines 78 to 82
9 |- **{
9 |+
10 10 | "bar": 10
11 |- },
11 |+ ,
Copy link
Contributor Author

@alanhdu alanhdu Nov 14, 2023

Choose a reason for hiding this comment

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

Is there a requirement that the output be formatted nicely? The edit here is syntactically valid, but the formatting is going to be quite off after the fix if there are multiple indented keys.

Copy link
Member

Choose a reason for hiding this comment

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

It's generally seen as "best effort" but not required.

Copy link
Contributor

github-actions bot commented Nov 14, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -0 violations, +6 -0 fixes in 41 projects)

apache/airflow (+0 -0 violations, +6 -0 fixes)

ruff check --no-cache --exit-zero --select ALL --preview

- tests/providers/common/sql/operators/test_sql_execute.py:358:49: PIE800 Unnecessary spread `**`
+ tests/providers/common/sql/operators/test_sql_execute.py:358:49: PIE800 [*] Unnecessary spread `**`
- tests/providers/docker/hooks/test_docker.py:215:29: PIE800 Unnecessary spread `**`
+ tests/providers/docker/hooks/test_docker.py:215:29: PIE800 [*] Unnecessary spread `**`
- tests/providers/docker/hooks/test_docker.py:221:29: PIE800 Unnecessary spread `**`
+ tests/providers/docker/hooks/test_docker.py:221:29: PIE800 [*] Unnecessary spread `**`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PIE800 6 0 0 6 0

let mut diagnostic = Diagnostic::new(UnnecessarySpread, value.range());
if checker.settings.preview.is_enabled() {
// Delete the `**{`
let tokenizer = BackwardsTokenizer::up_to(
Copy link
Member

Choose a reason for hiding this comment

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

Is there any way to restructure this to use SimpleTokenizer, i.e., forwards lexing? We've considered removing the backwards lexer in the past since it's quite complex, so I'd prefer to minimize usages if possible.

Copy link
Contributor Author

@alanhdu alanhdu Nov 14, 2023

Choose a reason for hiding this comment

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

Yeah, I think that should be possible:

  • For deleting **{, I can lex forward from the end of the previous value in the outer dictionary.
  • For deleting the last }, I can lex forward from the last value of the inner dictionary.

I might have to change the function signature to passi n the whole dict (and not just the keys + values) to handle the case where {**{"a": "b"}} case where there's no previous value (in which case I'd lex forward from the dict start).

(might not get to this until tomorrow though).

Copy link
Contributor Author

@alanhdu alanhdu Nov 15, 2023

Choose a reason for hiding this comment

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

Ok -- I think this should work.

I ended up leaving the formatting fairly ugly -- the more I tried to make it look nice, the less confident I became that I handled all the edge-cases correctly (e.g. handling comments correctly, avoiding double commas, getting the indentation right, etc) so I ended up going for something simple and just letting an autoformatter figure it out afterwards.

I might try and take another crack at having the fix provide better formatted code later.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for doing a second pass here!

@charliermarsh charliermarsh enabled auto-merge (squash) November 15, 2023 18:05
@charliermarsh charliermarsh added fixes Related to suggested fixes for violations preview Related to preview mode features labels Nov 15, 2023
@charliermarsh charliermarsh merged commit 2083352 into astral-sh:main Nov 15, 2023
16 checks passed
@alanhdu alanhdu deleted the alan/pie800 branch November 16, 2023 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants