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 — (fixtures): Replace fixture represenation with a class by Glyphack · Pull Request #12473 · pytest-dev/pytest

Conversation

@Glyphack

@Glyphack Glyphack commented Jun 18, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Closes #11525

During the sprint we discussed fixing the above issue and thought maybe it's a better idea to add a better represenation to fixtures. To do this without patching the object more, this PR refactors fixtures to have a class with attributes.
The main rational behind that is:

  • Now we have a type for fixtures makes it easier to check for fixture types and no need to perform cast on objects. Removed monkey patching.
  • They are no longer functions that carry a class within them.
  • When you decorate a function with a fixture it's not possible to call it anymore. So it makes more sense for it to not be a function or method.

Example

Previously we had:

===================================== short test summary info ======================================
FAILED tmp31nzhe4b.py::test_something - assert fixt == 42
======================================== 1 failed in 0.03s =========================================

where fixt is a pytest fixture function that is not replaced by it's value(directly used)

Now we print:

===================================== short test summary info ======================================
FAILED tmp31nzhe4b.py::test_something - assert pytest_fixture(<function fixt at *>) == 42 
======================================== 1 failed in 0.03s =========================================

Sorry, something went wrong.

Uh oh!

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

All reactions
Glyphack
Glyphack commented Jun 20, 2024
View reviewed changes
Comment thread testing/code/test_source.py Resolved

Uh oh!

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

@Glyphack Glyphack changed the title (assertion): Use repr for pytest fixtures and set repr for pytest fixtures (fixtures): Replace fixture represenation with a class Jun 20, 2024
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 20, 2024
@Glyphack
Glyphack force-pushed the reprs-assert-callable branch 3 times, most recently from bf90fca to d25a8d9 Compare June 20, 2024 13:51
Glyphack
Glyphack commented Jun 20, 2024
View reviewed changes
Comment thread testing/test_compat.py Resolved

Uh oh!

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

@Glyphack
Glyphack marked this pull request as ready for review June 20, 2024 15:19
@Glyphack
Glyphack force-pushed the reprs-assert-callable branch 2 times, most recently from 3d2ec33 to 84cc5b7 Compare July 4, 2024 18:48
@Glyphack

Glyphack commented Jul 4, 2024

Copy link
Copy Markdown
Contributor Author

Hey @The-Compiler @RonnyPfannschmidt I rebased the PR could you please take a look?

All reactions

Sorry, something went wrong.

Uh oh!

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

webknjaz
webknjaz reviewed Jul 4, 2024
View reviewed changes
Comment thread changelog/11525.improvement.rst Resolved

Uh oh!

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

Comment thread src/_pytest/compat.py Resolved

Uh oh!

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

Comment thread src/_pytest/compat.py Resolved

Uh oh!

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

Comment thread src/_pytest/fixtures.py Resolved

Uh oh!

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

Comment thread src/_pytest/fixtures.py Resolved

Uh oh!

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

Comment thread testing/code/test_source.py Outdated

src = inspect.getsource(deco_fixture)
# Since deco_fixture is now an instance of FixtureFunctionDef the getsource function will not work on it.
with pytest.raises(Exception):

@webknjaz webknjaz Jul 4, 2024

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.

Could you assert against a more specific exception? As I understand, this is not testing that something went wrong but it's not clear what exactly. Also, it's usually a good idea to use the match= regex with this helper.

Sorry, something went wrong.

Uh oh!

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

All reactions

@Glyphack Glyphack Jul 4, 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.

Yes will apply. This tries to document the behavior more than testing anything. Before introducing the FixtureFunctionDefinition it was possible to run inspect.getsource on fixtures but now it's not. This is just describing the behavior. I'm open to removing it as well.

Sorry, something went wrong.

Uh oh!

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

All reactions

@Glyphack Glyphack Jul 30, 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.

I changed it to a more specific error. Do you think we can remove the test? This just tests that you cannot use inspect on fixture functions.

Sorry, something went wrong.

Uh oh!

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

All reactions

@Glyphack Glyphack Oct 23, 2024 •
edited
Loading

Uh oh!

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

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.

I think we can remove this test. I removed it.

Sorry, something went wrong.

Uh oh!

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

All reactions
@Glyphack
Glyphack force-pushed the reprs-assert-callable branch 3 times, most recently from f0324a5 to 9d2a916 Compare July 30, 2024 18:37
@RonnyPfannschmidt RonnyPfannschmidt mentioned this pull request Aug 19, 2024
Pytest integration reagento/dishka#124
Open
@webknjaz

webknjaz commented Oct 23, 2024

Copy link
Copy Markdown
Member

Hey @Glyphack, could you resolve the merge conflicts / rebase this?

Glyphack 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.

@Glyphack

Glyphack commented Oct 23, 2024

Copy link
Copy Markdown
Contributor Author

Hey @Glyphack, could you resolve the merge conflicts / rebase this?

Yes will do today.

All reactions

Sorry, something went wrong.

Uh oh!

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

@Glyphack
Glyphack force-pushed the reprs-assert-callable branch from 641a3a1 to bc0a0e5 Compare October 23, 2024 14:30
@Glyphack
Glyphack requested review from RonnyPfannschmidt and webknjaz October 23, 2024 19:37
RonnyPfannschmidt
RonnyPfannschmidt approved these changes Oct 25, 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.

Iove where this is going

It opens up nice follow ups

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread src/_pytest/compat.py Resolved

Uh oh!

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

Comment thread src/_pytest/compat.py Outdated
"""Get the real function object of the (possibly) wrapped object by
functools.wraps or functools.partial."""
:func:`functools.wraps`, or :func:`functools.partial`, or :func:`pytest.fixture`."""
from _pytest.fixtures import FixtureFunctionDefinition

@RonnyPfannschmidt RonnyPfannschmidt Oct 25, 2024

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.

I believe we can drop fixture definition from that , we should pass the definitions attribute

Sorry, something went wrong.

Uh oh!

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

Glyphack reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@Glyphack Glyphack Oct 27, 2024 •
edited
Loading

Uh oh!

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

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.

Are you suggesting to pass the FixtureFunctionDefinition object to this function? If I understand correctly this function is expected to take any object and return the real object according to this test testing the behavior:
https://github.com/Glyphack/pytest/blob/3d7416c2dd5dcf2d60a8b30636dcc90978c64215/testing/test_compat.py#L63

Sorry, something went wrong.

Uh oh!

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

All reactions

@RonnyPfannschmidt RonnyPfannschmidt Nov 3, 2024

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.

As fixture definitions are no longer wrapping functions into other functions

The suggestion is to no longer consider them wrappers and passing the function directly

Sorry, something went wrong.

Uh oh!

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

All reactions

@Glyphack Glyphack Nov 15, 2024 •
edited
Loading

Uh oh!

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

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.

@RonnyPfannschmidt Sorry I didn't fully understand your comment.

The current unwrapping behavior here is only for two cases:

  1. The pytest fixture is applied on a function so I can display this case with Fixture(f)
  2. The pytest fixture is decorated so we have deco(Fixture(f))

The first case is handled by obj = obj._get_wrapped_function() line and second case is handled by obj = inspect.unwrap(obj, stop=lambda x: type(x) is FixtureFunctionDefinition).

If we drop any of them we cannot handle one of those two cases.

But I feel I am missing your point but unwrapping to me are these two lines.

Sorry, something went wrong.

Uh oh!

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

All reactions

@RonnyPfannschmidt RonnyPfannschmidt Nov 15, 2024

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.

Decorators cannot apply to a fixture definition

So that should trigger a error

It's wrong even now

Sorry, something went wrong.

Uh oh!

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

All reactions

@Glyphack Glyphack Nov 17, 2024 •
edited
Loading

Uh oh!

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

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.

I was wrong about that but the behavior is still needed. For example this test fails if we don't do unwrapping in this function:
https://github.com/Glyphack/pytest/blob/a6318c944f907bc350922c3961365e9ecd8d3e81/testing/python/integration.py#L11

The reason is that getfslineno is using this function to unwrap a decorator:
https://github.com/Glyphack/pytest/blob/a6318c944f907bc350922c3961365e9ecd8d3e81/src/_pytest/_code/code.py#L1341

I think the solution is to make a separate function for the cases when we want to perform the unwrapping? What is your suggestion?

Sorry, something went wrong.

Uh oh!

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

All reactions

@RonnyPfannschmidt RonnyPfannschmidt Nov 22, 2024

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.

What I meant is that we no longer ought to pass fixture definition to anything that unwrapped

Definition is no longer a function

Perhaps we should drop the call altogether

Sorry, something went wrong.

Uh oh!

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

All reactions

@Glyphack Glyphack Nov 23, 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.

Thanks I realized the reason my code required this check to be here was that I was getting objects using the fixture name instead of their actual name and mistakenly I calling this on non fixture objects. After fixing that I could remove the get_real_method and simplify this function.

Sorry, something went wrong.

Uh oh!

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

RonnyPfannschmidt reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread src/_pytest/fixtures.py Resolved

Uh oh!

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

Comment thread src/_pytest/fixtures.py Resolved

Uh oh!

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

@Glyphack

Glyphack commented Oct 27, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

Thank you both for the review. I left some TODO comments I will address them in a follow up PR.

All reactions

Sorry, something went wrong.

Uh oh!

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

@RonnyPfannschmidt RonnyPfannschmidt added this to the 8.4 milestone Nov 12, 2024
Glyphack added 6 commits November 23, 2024 16:20
@Glyphack
feat: use repr for pytest fixtures
41c7c2c
@Glyphack
(fixture) create a new class to represent fixtures
cee9044
@Glyphack
fix: update code to use new fixture definitions
004f29f
@Glyphack
Update AUTHORS
224595a
@Glyphack
chore: add changelog entry
8666dfb
@Glyphack
add test case for asserting missing fixture
b724030
@Glyphack
Merge branch 'main' into reprs-assert-callable
723a8f3
bluetech
bluetech approved these changes Dec 7, 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.

Looks good to me!

Sorry, something went wrong.

Uh oh!

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

Glyphack reacted with heart emoji
All reactions
  • ❤️ 1 reaction
nicoddemus
nicoddemus approved these changes Dec 7, 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 @Glyphack!

Sorry, something went wrong.

Uh oh!

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

Glyphack reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@nicoddemus
nicoddemus merged commit ecde993 into pytest-dev:main Dec 7, 2024
nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Dec 7, 2024
@nicoddemus
typing: Improve FixtureDefinition and FixtureDef
ec71fce
* Carry around parameters and return value in `FixtureFunctionDefinition`.
* Add `FixtureParams` to `FixtureDef`.

Follow up to pytest-dev#12473.
@nicoddemus nicoddemus mentioned this pull request Dec 7, 2024
typing: Improve FixtureDefinition and FixtureDef #13036
Closed
nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Dec 7, 2024
@nicoddemus
typing: Improve FixtureDefinition and FixtureDef
0303285
* Carry around parameters and return value in `FixtureFunctionDefinition`.
* Add `FixtureParams` to `FixtureDef`.

Follow up to pytest-dev#12473.
@youtux youtux mentioned this pull request Jan 11, 2025
Fix compatibility with pytest >= 8.4 pytest-dev/pytest-factoryboy#232
Closed
@youtux

youtux commented Jan 11, 2025

Copy link
Copy Markdown

FYI, this breaks pytest-factoryboy, but that's something I have to deal with.
Just wanted to let you know, in case maybe other users could have the same issue.

That being said, __pytest_wrapped__ was an internal API, so 100% agree that there is no guaranteed behavior on these.

nicoddemus 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.

pbrezina added a commit to pbrezina/pytest-mh that referenced this pull request Jun 3, 2025
@pbrezina
mh_fixture: make it work with pytest-8.4.0
292488d
pytest-8.4.0 removed the __pytest_wrapped__ atribute on a fixture object,
instead of unwrapping the object, it now keeps __wrapped__ that we can
use instead.

This commit makes sure we can work with both old and new pytest.

See: pytest-dev/pytest#12473
@pbrezina pbrezina mentioned this pull request Jun 3, 2025
mh_fixture: make it work with pytest-8.4.0 next-actions/pytest-mh#102
Merged
pbrezina added a commit to pbrezina/pytest-mh that referenced this pull request Jun 3, 2025
@pbrezina
mh_fixture: make it work with pytest-8.4.0
3e57cf6
pytest-8.4.0 removed the __pytest_wrapped__ atribute on a fixture object,
instead of unwrapping the object, it now keeps __wrapped__ that we can
use instead.

This commit makes sure we can work with both old and new pytest.

See: pytest-dev/pytest#12473
pbrezina added a commit to next-actions/pytest-mh that referenced this pull request Jun 3, 2025
@pbrezina
mh_fixture: make it work with pytest-8.4.0
96721b7
pytest-8.4.0 removed the __pytest_wrapped__ atribute on a fixture object,
instead of unwrapping the object, it now keeps __wrapped__ that we can
use instead.

This commit makes sure we can work with both old and new pytest.

See: pytest-dev/pytest#12473
@lkubb lkubb mentioned this pull request Jun 4, 2025
Broken with pytest 8.4 saltstack/pytest-salt-factories#187
Closed
@The-Compiler The-Compiler mentioned this pull request Jun 5, 2025
fxref broken in pytest >=8.4.0? smarie/python-pytest-cases#364
Closed
@Zac-HD Zac-HD mentioned this pull request Jun 6, 2025
[Python] Test regressions with pytest-8.4.0: tests/pytest/test_fixtures.py::test_given_fails_if_already_decorated_with_fixture and tests/pytest/test_capture.py::test_healthcheck_traceback_is_hidden HypothesisWorks/hypothesis#4432
Closed
mdujava added a commit to mdujava/3scale-tests that referenced this pull request Jun 6, 2025
@mdujava
Freeze pytest before 8.4.0
26f1e23
Reference: pytest-dev/pytest#12473
Caused-by: smarie/python-pytest-cases#364
@mdujava mdujava mentioned this pull request Jun 6, 2025
Freeze pytest before 8.4.0 3scale-qe/3scale-tests#941
Merged
mdujava added a commit to mdujava/3scale-tests that referenced this pull request Jun 6, 2025
@mdujava
Freeze pytest before 8.4.0
74879c6
Reference: pytest-dev/pytest#12473
Caused-by: smarie/python-pytest-cases#364
@delatrie delatrie mentioned this pull request Jun 9, 2025
fix(pytest): can't change a fixture's title in pytest 8.4 or later allure-framework/allure-python#866
Merged
@smarie smarie mentioned this pull request Jun 9, 2025
Fixed compat with pytest 8.4.0 smarie/python-pytest-cases#367
Merged
@eldjh3 eldjh3 mentioned this pull request Jun 20, 2025
Fails with pytest >= 8.4 theY4Kman/pytest-lambda#9
Open
@dvcorreia dvcorreia mentioned this pull request Jun 25, 2025
Fix fixture wrap with pytest 8.4 pytest-dev/pytest-factoryboy#238
Closed
@ayakael ayakael mentioned this pull request Jul 6, 2025
Does not support pytest >= 8.5.0 dimagi/pytest-unmagic#3
Closed
@alexw-arista alexw-arista mentioned this pull request Aug 26, 2025
Update fixture creation to handle pytest 8.4.0 aristanetworks/pytest-netdut#34
Merged
@cjwatson cjwatson mentioned this pull request Sep 14, 2025
[self-tests] test_skips_pytest_fixtures fails against pytest 8.4.0+ bitprophet/pytest-relaxed#33
Open
cjwatson added a commit to cjwatson/pytest-relaxed that referenced this pull request Sep 14, 2025
@cjwatson
Fix compatibility with pytest 8.4
df6dc3c
pytest-dev/pytest#12473 removed
`_pytestfixturefunction`.  The `getfixturemarker` function is still
private, but has existed across all pytest versions that we support.

Fixes: bitprophet#33
@cjwatson cjwatson mentioned this pull request Sep 14, 2025
Fix compatibility with pytest 8.4 bitprophet/pytest-relaxed#34
Open
@Zac-HD Zac-HD mentioned this pull request Jul 22, 2026
Cannot create two fixtures based on the same function #1875
Closed
@teddygood teddygood mentioned this pull request Aug 15, 2026
feat: expose FixtureFunctionDefinition for typing #14862
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@RonnyPfannschmidt RonnyPfannschmidt RonnyPfannschmidt approved these changes
@nicoddemus nicoddemus nicoddemus approved these changes
@bluetech bluetech bluetech approved these changes
@webknjaz webknjaz Awaiting requested review from webknjaz

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

8.4

Development

Successfully merging this pull request may close these issues.

Show repr for callables used in an assert directly

Uh oh!

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

6 participants

@Glyphack @webknjaz @nicoddemus @bluetech @youtux @RonnyPfannschmidt
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.