GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: improved caching of parameterized fixtures by 0xDEC0DE · Pull Request #12600 · pytest-dev/pytest

Conversation

@0xDEC0DE

@0xDEC0DE 0xDEC0DE commented Jul 11, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The fix for Issue #6541 caused a regression where cache hits unexpectedly became cache misses. Attempt to restore the previous behavior, while also retaining the fix for the bug.

Fixes: Issue #6962

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 11, 2024
@0xDEC0DE 0xDEC0DE changed the title fix: allow caching of parameterized fixtures fix: improved caching of parameterized fixtures Jul 11, 2024
nicoddemus
nicoddemus requested changes Jul 11, 2024
View reviewed changes

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks a lot @0xDEC0DE for tracking this down!

Could you please also write a simple integration test for this specifically? It is important to have that in order to avoid future regressions, as was the case in 6541.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread changelog/6962.bugfix.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@nicoddemus nicoddemus added the backport 8.2.x label Jul 11, 2024
@0xDEC0DE

0xDEC0DE commented Jul 11, 2024

Copy link
Copy Markdown
Contributor Author

Thanks a lot @0xDEC0DE for tracking this down!

Could you please also write a simple integration test for this specifically? It is important to have that in order to avoid future regressions, as was the case in 6541.

I thought it might already be under test, but the coverage map disabused me of that notion. I'll get something together shortly.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@0xDEC0DE
0xDEC0DE requested a review from nicoddemus July 11, 2024 23:44
0xDEC0DE
0xDEC0DE commented Jul 11, 2024
View reviewed changes
Comment thread testing/python/fixtures.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@0xDEC0DE

0xDEC0DE commented Jul 12, 2024

Copy link
Copy Markdown
Contributor Author

Thanks a lot @0xDEC0DE for tracking this down!

Could you please also write a simple integration test for this specifically? It is important to have that in order to avoid future regressions, as was the case in 6541.

Done, and done.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

nicoddemus
nicoddemus reviewed Jul 12, 2024
View reviewed changes
Comment thread testing/python/fixtures.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

nicoddemus
nicoddemus approved these changes Jul 12, 2024
View reviewed changes

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks @0xDEC0DE, looks great!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nicoddemus

nicoddemus commented Jul 12, 2024

Copy link
Copy Markdown
Member

I will merge this tomorrow, unless someone wants more time to review it.

0xDEC0DE reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bluetech
bluetech requested changes Jul 13, 2024
View reviewed changes

@bluetech bluetech left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The reason for using is is written in a comment, namely that == can be expensive for large numpy arrays. How is this addressed?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nicoddemus

nicoddemus commented Jul 13, 2024

Copy link
Copy Markdown
Member

The reason for using is is written in a comment, namely that == can be expensive for large numpy arrays. How is this addressed?

Ouch missed that completely, thanks. I guess we just need to invert the order, first attempt is then fallback to ==.

But this makes me wonder, why is the == comparison is needed in the first place? I mean the list is passed to pytest in pytest_generate_tests, at which point do we get a different instance of the parametrized value, do we make a copy somewhere? 🤔

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicoddemus
nicoddemus requested a review from bluetech July 13, 2024 13:56
@0xDEC0DE

0xDEC0DE commented Jul 13, 2024

Copy link
Copy Markdown
Contributor Author

The reason for using is is written in a comment, namely that == can be expensive for large numpy arrays. How is this addressed?

Pithy version: "expensive and correct" is always an improvement over "cheap but wrong".

Looking over the original bug report, and reproducing the regression locally, I believe that comment makes an assertion wholly without evidence.

The problem wasn't that cache validation was taking a long time, it was that cache validation was throwing stack traces due to wonky inputs. As part of the fix, I updated the comment to state the things that are known to be true, and got rid of the conjecture.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

0xDEC0DE
0xDEC0DE commented Jul 13, 2024
View reviewed changes
Comment thread src/_pytest/fixtures.py Outdated
Comment on lines +1060 to +1067
# Coerce the comparison into a bool (#12600), and if that fails, fall back to an identity check:
# `__eq__` is not required to return a bool, and sometimes doesn't, e.g., numpy arrays (#6497).
try:
cache_hit = bool(my_cache_key == cache_key)
except (ValueError, RuntimeError):
cache_hit = my_cache_key is cache_key
# First attempt to use 'is' for performance reasons (for example numpy arrays (#6497)).
cache_hit = my_cache_key is cache_key
if not cache_hit:
# If they are not the same, fallback to a bool comparison (#12600).
try:
cache_hit = bool(my_cache_key == cache_key)
except (ValueError, RuntimeError):
cache_hit = False

@0xDEC0DE 0xDEC0DE Jul 13, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is suitable for speeding up cache hits, but all cache misses will go through the "expensive" code path. Which I suppose is an improvement, but I don't know if it's worth the extra complexity introduced here.

But, your house, your rules.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nicoddemus

nicoddemus commented Jul 13, 2024

Copy link
Copy Markdown
Member

The problem wasn't that cache validation was taking a long time, it was that cache validation was throwing stack traces due to wonky inputs.

I revisited the issue and indeed the problem was not performance, but the fact it was raising an error when comparing with ==.

But I think my latest change takes care of both concerns in the end? One thing I still did not get is why the parametrized values were failing the identity check... should always be the same object, unless we are making a copy somewhere?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@0xDEC0DE

0xDEC0DE commented Jul 13, 2024

Copy link
Copy Markdown
Contributor Author

The problem wasn't that cache validation was taking a long time, it was that cache validation was throwing stack traces due to wonky inputs.

I revisited the issue and indeed the problem was not performance, but the fact it was raising an error when comparing with ==.

But I think my latest change takes care of both concerns in the end? One thing I still did not get is why the parametrized values were failing the identity check... should always be the same object, unless we are making a copy somewhere?

The test case in #6962 roughly simulates how we hit it: using string interpolation to generate parameter sets, such that separate tests in the session should, by all appearances, use the same fixture. And since string operations LOVE making copies:

>>> "lol{}".format("wut") == "lol{}".format("wut")
True
>>> "lol{}".format("wut") is "lol{}".format("wut")
False

...it wasn't using the cached fixture.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicoddemus

nicoddemus commented Jul 13, 2024

Copy link
Copy Markdown
Member

Yeah that I get, I don't get is why at some point a copy of that string seems to be made... after passing the object (whatever the object is, it can be a string, a list, a custom object) over to parametrize, that object should always be the same inside the pytest machinery, but seems at some point someone makes a copy of that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicoddemus

nicoddemus commented Jul 13, 2024

Copy link
Copy Markdown
Member

Ahh of course, pytest_generate_tests is called once for each metafunc... of course we will have multiple instances then. 🤦‍♂️

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@0xDEC0DE

0xDEC0DE commented Jul 13, 2024

Copy link
Copy Markdown
Contributor Author

Ahh of course, pytest_generate_tests is called once for each metafunc... of course we will have multiple instances then. 🤦‍♂️

Is this insight worth adding to the code comments?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicoddemus

nicoddemus commented Jul 13, 2024

Copy link
Copy Markdown
Member

Don't think so, brainfart on my part I guess.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicoddemus

nicoddemus commented Jul 15, 2024

Copy link
Copy Markdown
Member

@bluetech would you like to do another review, or are we OK to merge?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

RonnyPfannschmidt
RonnyPfannschmidt reviewed Jul 15, 2024
View reviewed changes

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Makes me wonder if we shouldn't just intern hashable objects

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bluetech

bluetech commented Jul 16, 2024

Copy link
Copy Markdown
Member

I'm pretty sure there are people using some large values as parameters, and I think they will complain if we make this change. There have been previous proposals for solving the "identity instead of equality" problem, one was for example to use equality but provide a workaround to use the param id as the cache key (see #9420). But we didn't go forward with them in the end.

Generally I get the sense that the number of people complaining about is is greater than the number who will complain about ==/hashing slowness. But it's hard to be sure.

I'd be fine with merging this change as long as we consider what to say to people who will start complaining about slowness. One way will be to tell them not to do it, for example to some value in the parameter to stand in for the actual big value and only grab the actual value in the test. Another will be to provide some builtin solution like in #9420.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bluetech
bluetech requested changes Jul 16, 2024
View reviewed changes
Comment thread src/_pytest/fixtures.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@nicoddemus
nicoddemus requested a review from bluetech July 17, 2024 00:19
bluetech
bluetech approved these changes Jul 17, 2024
View reviewed changes

@bluetech bluetech left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

OK, let's try it and see what happens...

(I'm going to remove the backport label -- though we're probably not going to have a patch release I wouldn't want to backport this anyway).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread changelog/6962.bugfix.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bluetech bluetech removed the backport 8.2.x label Jul 17, 2024
@nisimond @nicoddemus
fix: improve caching of parameterized fixtures
d434bcf
The fix for Issue #6541 caused regression where cache hits became
cache misses, unexpectedly.  Attempt to restore the previous behavior,
while also retaining the fix for the bug.

Fixes: Issue #6962
nicoddemus and others added 5 commits July 17, 2024 09:35
@nicoddemus
Update testing/python/fixtures.py
a8ce305
@nicoddemus
Improve test and comment
bf5ca7b
@nicoddemus
Identity comparsion first
3153ab2
@nicoddemus
Change comparison order
fa7147b
@nicoddemus @bluetech
Update changelog/6962.bugfix.rst
a8229c2
Co-authored-by: Ran Benita <[email protected]>
@nicoddemus
nicoddemus merged commit d489247 into pytest-dev:main Jul 17, 2024
@0xDEC0DE
0xDEC0DE deleted the issue/6962 branch July 17, 2024 16:05
@ChihebBENCHEIKH1 ChihebBENCHEIKH1 mentioned this pull request Jul 13, 2026
Parametrization with Variables Unexpectedly Changes Fixture Scope from Class-Level to Function-Level #12146
Open
This was referenced Jul 22, 2026
metafunc parametrization with session scope causes multiple fixture teardowns #8102
Closed
weird behavior with tuples indirect parameterization #8281
Closed
Class scope fixture not working with indirect parametrization since 5.4.0 #9198
Closed
@RonnyPfannschmidt RonnyPfannschmidt mentioned this pull request Oct 9, 2026
Indirect parametrization with scope="function" does not always work #11575
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@RonnyPfannschmidt RonnyPfannschmidt RonnyPfannschmidt left review comments
@nicoddemus nicoddemus nicoddemus approved these changes
@bluetech bluetech bluetech approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@0xDEC0DE @nicoddemus @bluetech @RonnyPfannschmidt @nisimond
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.