Skip to content

reimplement trio100 & trio101 with libcst #142

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

Merged
merged 1 commit into from
Feb 21, 2023

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Feb 19, 2023

Also changed how trio100 works to avoid missed cases where a function is defined inside the with statement.
100 & 101 moved into their own files.

I spent some time trying to get batching to work, BatchedVisitor is not compatible with MatcherDecoratableVisitor - and especially not with transformers. Tried dynamically creating a class that inherited from all enabled visitors, but ran into so many minor headaches with that approach I abandoned it. Might revisit once all visitors are changed to cst, but for now it's certainly not worth the effort for the sake of speeding it up. There might be a case of wanting to do the transforms simultaneously in case one transform unlocks another, and stuff like that.
I thought batching would be required, since the utility visitors are separate classes, but I think that can be made to work by having error classes that require the functionality of a utility visitor to inherit from it. But neither 100 nor 101 require any of the utility ones so didn't bother with it too much.

I'm waiting with enabling transforming for trio100 for a later PR, since that will require a bunch of UI changes / flags / etc.

Ended up opening a bunch of issues in pyright/libcst/mypy during the process, Instagram/LibCST#870 is probably the most relevant one - which is probably gonna be somewhat of a pain going forward if not fixed. (I didn't bother trying to stop shed from removing Union, but if possible that'd make it less painful.)

@jakkdl
Copy link
Member Author

jakkdl commented Feb 20, 2023

Ah, looks like the CI failure is due to shed not fixing everything in one go. I've noticed that happening frequently which is somewhat annoying, will track down the cause and open an issue in shed.

@jakkdl
Copy link
Member Author

jakkdl commented Feb 20, 2023

Fixed the CI, and cribbed your try/finally solution for LIBCST_PARSER_TYPE - except switched out pop() for del to avoid a reportUnusedCallResult

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.

This is looking nice! I think I was anticipating a considerably gnarlier migration...

I'd appreciate a response to super().__init__() and the WithItem thing; other comments don't need to be addressed before merging or perhaps at all.


return m.matches(
func,
m.FunctionDef(
Copy link
Member

Choose a reason for hiding this comment

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

I'm generally a little wary about such deeply-indented nested calls; maybe the m.Decorator(...) could be pulled out as a named variable?

Copy link
Member Author

@jakkdl jakkdl Feb 20, 2023

Choose a reason for hiding this comment

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

pulled out a list_contains - it's close to being usable in with_has_item as well, will likely iterate on these as I use them in more rewritten visitors/transformers.

return updated_node

@m.visit(m.Await() | m.For(asynchronous=m.Asynchronous()))
# can't use m.call_if_inside(m.With), since it matches parents *or* the node itself
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 write a helper function for this at some point?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oooh, I hadn't even considered writing my own decorators. Hell yes I want this, would be very useful in shed as well - I'm pretty sure there's at least one bad matcher due to this. But yeah, for another PR.

@jakkdl
Copy link
Member Author

jakkdl commented Feb 20, 2023

This is looking nice! I think I was anticipating a considerably gnarlier migration...

you keep underestimating how good the [infra]structure/patterns I've built up are 😁 Though I did spend a lot of time on polishing this, and there's quite a bit of potential future gnarl left.

@Zac-HD
Copy link
Member

Zac-HD commented Feb 21, 2023

you keep underestimating how good the [infra]structure/patterns I've built up are 😁

Hofstadter's law has earned my respect, but you're right - kudos!

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.

Tiny tweak below, but this looks good to me. I'm inclined to merge (with or without tweak) in the morning and release a new version, what do you reckon?

@jakkdl
Copy link
Member Author

jakkdl commented Feb 21, 2023

Tweaked and ready to be shipped! And good call, this is nicer.

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.

🚢

@Zac-HD Zac-HD merged commit d8325ad into python-trio:main Feb 21, 2023
@jakkdl jakkdl deleted the libcst_rewrite branch March 6, 2023 15:30
@jakkdl jakkdl mentioned this pull request Mar 11, 2023
63 tasks
Comment on lines +54 to +57
# need to use Union due to https://github.com/Instagram/LibCST/issues/870
def checkpoint_node(self, node: Union[cst.Await, cst.For, cst.With]):
if self.has_checkpoint_stack:
self.has_checkpoint_stack[-1] = True
Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out getting future annotations with get_type_hints is not possible, though I think an alternative to using Union is stringifying the annotation.
https://stackoverflow.com/questions/66006087/how-to-use-typing-get-type-hints-with-pep585-in-python3-8

Copy link
Member

Choose a reason for hiding this comment

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

See https://discuss.python.org/t/pep-649-deferred-evaluation-of-annotations-tentatively-accepted/21331 for background, this is a long-running problem which should at least get better from 3.12 😌

Copy link
Member Author

Choose a reason for hiding this comment

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

Time to re-read that PEP again :D
Oh and this will solve @dataclass annotation troubles as well!

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