Skip to content

TRIO112: replace one-line nursery with regular function call #34

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 2 commits into from
Aug 10, 2022

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Aug 9, 2022

Final remaining issue without a PR 💪

Code is fugly, will make a pass tomorrow, but I think the check is done.

@jakkdl jakkdl changed the title ugly implementation of trio112 TRIO112: replace one-line nursery with regular function call Aug 9, 2022
@jakkdl jakkdl force-pushed the trio112_single_statement_nursery branch 2 times, most recently from 0270629 to 28e282e Compare August 9, 2022 20:29
tests/trio112.py Outdated
Comment on lines 15 to 33
# safe?
with trio.open_nursery() as n, open("") as o:
n.start(...)
Copy link
Member

Choose a reason for hiding this comment

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

Bit suspicious for #22-like reasons, but since violating this rule is more of a style problem than a correctness issue let's err on the side of less alarms.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thinking about this one more, is it actually that big of a style problem to have multiple withitems in a single with? And if the open was placed in it's own with it'd clearly be a TRIO112 (or TRIO302/111).

I changed it to be an error, but trivial to change it back.

@jakkdl jakkdl force-pushed the trio112_single_statement_nursery branch from 28e282e to c57790b Compare August 10, 2022 10:32
@jakkdl
Copy link
Member Author

jakkdl commented Aug 10, 2022

rebased onto main, added support for multiple withitems, cleaned up code.
Also touched on some other stuff, partially related to this. Don't think worth bothering with a separate PR for it (will only be a minor conflict with #22) but can split if you want.

@@ -333,10 +353,30 @@ def has_exception(node: Optional[ast.expr]) -> str:


class Visitor102(Flake8TrioVisitor):
class TrioScope:
Copy link
Member Author

Choose a reason for hiding this comment

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

get_matching_call (previously get_trio_scope) no longer constructs a TrioScope, since only Visitor102 actually cared about it. So moved it to a subclass

Copy link
Member

Choose a reason for hiding this comment

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

IMO this isn't an affirmative reason to move it, so I'd keep the diff a bit smaller by leaving it where it was. On the other hand, nor is there any real reason to move it back!

Copy link
Member Author

Choose a reason for hiding this comment

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

haha. I liked subclassing it for people reading it later on not encountering it through a commit diff, fun tension

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.

LGTM!

Comment on lines +311 to +312
# isinstance(..., ast.Call) is done in get_matching_call
body_call = cast(ast.Call, node.body[0].value)
Copy link
Member

Choose a reason for hiding this comment

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

I'd use an assert rather than a cast() here.

Copy link
Member Author

Choose a reason for hiding this comment

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

assert is actually not possible here, the isinstance call is done later inside get_matching_call (which if it inside there evaluates false propagates to the condition calling it being false).

the easiest alternative is a redundant isinstance in the condition

I suppose the cast is incorrect in that body_call isn't actually a Call yet, but is for brevity/convenience later.

@@ -333,10 +353,30 @@ def has_exception(node: Optional[ast.expr]) -> str:


class Visitor102(Flake8TrioVisitor):
class TrioScope:
Copy link
Member

Choose a reason for hiding this comment

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

IMO this isn't an affirmative reason to move it, so I'd keep the diff a bit smaller by leaving it where it was. On the other hand, nor is there any real reason to move it back!

@Zac-HD Zac-HD merged commit d8acd4d into python-trio:main Aug 10, 2022
@Zac-HD Zac-HD mentioned this pull request Aug 11, 2022
12 tasks
@jakkdl jakkdl deleted the trio112_single_statement_nursery branch August 23, 2022 12:29
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