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

Try supporting yield functions for setup/teardown. #1

Merged
merged 8 commits into from
Oct 18, 2023
Merged

Conversation

okken
Copy link
Owner

@okken okken commented Oct 16, 2023

No description provided.

Copy link
Contributor

@blakeNaccarato blakeNaccarato left a comment

Choose a reason for hiding this comment

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

I've never reviewed Pytest plugin code before, so it was good practice for me to understand how Pytest connects to things under the hood. Initially I thought some yield from and de-nested logic could help in the param_scope function, but with some local debugging I convinced myself that the way you've done it is actually the way to go.

It took me awhile to figure out that pytest_runtest_teardown is a magic function that we use to handle teardown. I can see how you need globals to handle this. Am I correct in thinking that you couldn't use a closure or inner function here because Pytest searches for pytest_runtest_teardown at module scope?

Anyways, I caught a minor copy/paste error in your newly-written test, and proposed a test (which will fail on the current state of the PR) that would check for excess teardown. Also, I think it might be better to handle the excess teardown case right away, as you're unpacking m.args, rather than handling it way down in if teardown_func:.

I appreciate the opportunity to review it, I've learned some about how to write Pytest plugins. It's more work than I thought 😅, considering you're working with lower-level constructs.

tests/test_yield.py Outdated Show resolved Hide resolved
tests/test_yield.py Outdated Show resolved Hide resolved
examples/test_yield.py Show resolved Hide resolved
src/pytest_param_scope/plugin.py Outdated Show resolved Hide resolved
@@ -44,7 +47,17 @@ def param_scope(request):

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I'm not sure how to comment on un-diffed areas in a PR review! But anyways, a few lines above this where the if m: clause appears, the case of excess teardown could be handled earlier there, rather than handling it on the line comment below.

tests/test_yield.py Outdated Show resolved Hide resolved
@blakeNaccarato
Copy link
Contributor

blakeNaccarato commented Oct 18, 2023

Looks like one of my review comments got orphaned onto the wrong line, I think because I did it in my IDE and something went awry, so I've repeated it just now. Next time I'll just do the review in the GitHub UI instead.

Also I have a question on PR etiquette. I submitted this review as "request changes". As a visiting reviewer, could "request changes" be misconstrued as overstepping when you're not an official reviewer for that project?

@okken
Copy link
Owner Author

okken commented Oct 18, 2023

@blakeNaccarato Thanks for the feedback. I'll do a more thorough review this week.

Copy link
Owner Author

@okken okken left a comment

Choose a reason for hiding this comment

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

thanks for the great feedback

tests/test_yield.py Outdated Show resolved Hide resolved
examples/test_yield.py Show resolved Hide resolved
src/pytest_param_scope/plugin.py Outdated Show resolved Hide resolved
tests/test_yield.py Outdated Show resolved Hide resolved
@okken
Copy link
Owner Author

okken commented Oct 18, 2023

I tried removing the teardown parameter to the maker.
But ran into an oddity with pytest where you can't send one function. Weird.
Filed pytest-dev/pytest#11519

@RonnyPfannschmidt
Copy link

@okken this plugin seems to be only necessary to work around the fact that I haven't yet managed to make function definition Part of the scopes list and collection tree

@blakeNaccarato
Copy link
Contributor

blakeNaccarato commented Oct 18, 2023

So it looks pytest-dev/pytest#1552 (comment) summarizes the simplest framing of the request that this plugin attempts to satisfy, while pytest-dev/pytest#1552 (comment) and pytest-dev/pytest#1552 (comment) detail the implementation problem of how a param scope is not so simply defined, due to the fact that parametrize can be used on functions as well as fixtures, operating at various scopes? So a param scope would be orthogonal to the scope system defined as is?

With my limited understanding of pytest internals, the statement, "I haven't yet managed to make function definition part of the scopes list and collection tree," suggests that as a prerequisite feature for param scope? Because currently pytest's scope mechanism doesn't "see" functions at signature/definition time?

@RonnyPfannschmidt
Copy link

Exactly, currently there's nodes in the collection tree for anything that's contains functions and functions after parameterization

The node type for the definition was already added,but the de-spagettification off that part is a major undertaking

@okken
Copy link
Owner Author

okken commented Oct 18, 2023

@okken this plugin seems to be only necessary to work around the fact that I haven't yet managed to make function definition Part of the scopes list and collection tree

@RonnyPfannschmidt yes, that's true. When the plugin is no longer necessary, I'll happily deprecate it and point people to a recommended solution.

@blakeNaccarato
Copy link
Contributor

Thanks for clarifying. Okay I'm going to cross-link a few related efforts. Looks like the groundwork was laid in https://github.com/pytest-dev/pytest/projects/1, while the node cleanup in https://github.com/pytest-dev/pytest/projects/3 will pave the way to the feature in question (also seen in pytest-dev/pytest#3432).

@okken, maybe it's a good idea to track those upstream efforts in a meta-issue, and possibly point willing contributors in that direction (as much as mere mortals are able to help with pytest internals)?

See also the related API reference on FunctionDefinition and an indication that such an effort would constitute a true breakage in the backwards compatibility policy. Wow, sometimes a seemingly innocuous feature is actually a can of worms.

@okken
Copy link
Owner Author

okken commented Oct 18, 2023

@okken, maybe it's a good idea to track those upstream efforts in a meta-issue, and possibly point willing contributors in that direction (as much as mere mortals are able to help with pytest internals)?

@blakeNaccarato I like this idea. Will you do this? :)

@okken okken merged commit 32f9907 into main Oct 18, 2023
8 checks passed
@blakeNaccarato
Copy link
Contributor

@blakeNaccarato I like this idea. Will you do this? :)

Sure, I'll open a meta-issue in pytest-param-scope detailing the major open blockers and where additional help is needed. I will probably need to back-burner this though. I'll try to write it up this weekend.

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.

3 participants