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

test for autofix_files, fix some 91x autofixes, rejig eval_files #159

Merged
merged 1 commit into from
Mar 22, 2023

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Mar 17, 2023

branched on top of #158
F_ixes the sixth checkbox in #124 - "test that autofixed files don't generate errors", and also tests that autofixed files don't get further autofixes applied if run through the transformer again.

Finished, but somewhat messy - want to take a fresh look at visitor91x.py and the class structure there another day to see if I can make it less awful.

eval/autofix diffs are quite irritating when line numbers change, both in showing up in the diff and when developing when I manually edit the files - so it's possible I tweak something there in the future.

Feel free to review if you want, but I plan on cleaning it up and adding review comments so it should be easier after that.

@jakkdl jakkdl mentioned this pull request Mar 17, 2023
63 tasks
@jakkdl jakkdl force-pushed the autofix_infra branch 2 times, most recently from bce4027 to b3d8a6e Compare March 20, 2023 13:08
@jakkdl jakkdl marked this pull request as ready for review March 20, 2023 13:08
Copy link
Member Author

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

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

first review, during which I decided to change the behaviour so instead of tracking existence of files in the autofix dir it's now a magic comment # AUTOFIX. This gets rid of empty diff files and files in the autofix dir that are identical to the ones in eval_files, and simultaneously added checks that eval files without that marker doesn't change the source code - which should catch if one forgets to add the magic comment to an eval file for an autofixing visitor (and other esoteric errors)

@@ -90,9 +91,125 @@ def checkpoint_statement(library: str) -> cst.SimpleStatementLine:
)


class CommonVisitors(cst.CSTTransformer, ABC):
Copy link
Member Author

Choose a reason for hiding this comment

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

The subclassing needed to be expanded to move some import logic into it, since otherwise a file only with yields that can't be autofixed, inside loop bodies, (i.e. tests/eval_files/trio91x_noautofix) had no way of figuring out whether to add an import or not

Copy link
Member Author

Choose a reason for hiding this comment

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

most of the methods are moved up and only lightly modified

flake8_trio/visitors/visitor91x.py Show resolved Hide resolved
@@ -248,7 +317,7 @@ def check_function_exit(
# Add this as a node potentially needing checkpoints only if it
# missing checkpoints solely depends on whether the artificial statement is
# "real"
if len(self.uncheckpointed_statements) == 1:
if len(self.uncheckpointed_statements) == 1 and not self.noautofix:
Copy link
Member Author

Choose a reason for hiding this comment

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

noautofix is currently only set by boolops, but that can easily be expanded.

Comment on lines -277 to -308
# TODO: generate an error in these two if transforming+visiting is done in a single
# pass and emit-error-on-transform can be enabled/disabled. The error can't be
# generated in the yield/return since it doesn't know if it will be autofixed.

# SimpleStatementSuite and multi-statement lines can probably be autofixed, but
# for now just don't insert checkpoints in the wrong place.
def leave_SimpleStatementSuite(
self,
original_node: cst.SimpleStatementSuite,
updated_node: cst.SimpleStatementSuite,
) -> cst.SimpleStatementSuite:
self.add_statement = None
return updated_node

# insert checkpoint before yield with a flattensentinel, if indicated
def leave_SimpleStatementLine(
self,
original_node: cst.SimpleStatementLine,
updated_node: cst.SimpleStatementLine,
) -> cst.SimpleStatementLine | cst.FlattenSentinel[cst.SimpleStatementLine]:
if self.add_statement is None:
return updated_node

# multiple statements on a single line is not handled
if len(updated_node.body) > 1:
self.add_statement = None
return updated_node

res = cst.FlattenSentinel([self.add_statement, updated_node])
self.add_statement = None
return res # noqa: R504

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to CommonVisitors

flake8_trio/visitors/visitor91x.py Show resolved Hide resolved
flake8_trio/visitors/visitor91x.py Show resolved Hide resolved
flake8_trio/visitors/visitor91x.py Show resolved Hide resolved
flake8_trio/visitors/visitor91x.py Show resolved Hide resolved
flake8_trio/visitors/visitor91x.py Show resolved Hide resolved
tests/autofix_files/trio100.py Show resolved Hide resolved
@jakkdl jakkdl force-pushed the autofix_infra branch 2 times, most recently from 168c1df to c94994f Compare March 20, 2023 14:28
@jakkdl
Copy link
Member Author

jakkdl commented Mar 20, 2023

Okay I think I'm finally happy with this, gotta resist the temptation to add more features to this already messy PR.

@Zac-HD
Copy link
Member

Zac-HD commented Mar 20, 2023

I might let you rebase this before I try to review it 😅

@jakkdl
Copy link
Member Author

jakkdl commented Mar 21, 2023

Ah, you could just have reviewed the second commit - I can try to be more clear about that in the future. But rebased!

Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

I'm really looking forward to running all of this 😁

@@ -2,24 +2,24 @@
@@ -3,24 +3,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 might want to replace these numbers with x so that inserting or deleting unchanged code doesn't affect the diff? Definitely a follow-up PR if we decide to do that though.

Copy link
Member Author

Choose a reason for hiding this comment

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

ye it's very ugly - but wasn't sure if they might still be useful. But I guess if you actually want to find the line numbers you can look at the non-diff autofix file or so.

@Zac-HD Zac-HD merged commit 112b998 into python-trio:main Mar 22, 2023
@jakkdl jakkdl deleted the autofix_infra branch March 22, 2023 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants