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 — Show repr for callables used in an assert directly · Issue #11525 · pytest-dev/pytest

Show repr for callables used in an assert directly #11525

New issue
Copy link
New issue
Copy link
Closed
#12473
Closed
Show repr for callables used in an assert directly#11525
#12473
Copy link
Labels
topic: reportingrelated to terminal output and user-facing messages and errors

Description

@The-Compiler
The-Compiler
opened on Oct 20, 2023
Issue body actions

With something like

import pytest

@pytest.fixture
def fixt():
    return 42

def test_something():  # missing "fixt" argument
    print(fixt)
    assert fixt == 42

(which is a common beginners mistake), pytest prints:

======================================= test session starts ========================================
collected 1 item

tmp31nzhe4b.py F                                                                             [100%]

============================================= FAILURES =============================================
__________________________________________ test_something __________________________________________

    def test_something():  # missing "fixt" argument
        print(fixt)
>       assert fixt == 42
E       assert fixt == 42

/tmp/ipykernel_77860/2395276065.py:9: AssertionError
--------------------------------------- Captured stdout call ---------------------------------------
<function fixt at 0x7fd7745c4160>
===================================== short test summary info ======================================
FAILED tmp31nzhe4b.py::test_something - assert fixt == 42
======================================== 1 failed in 0.03s =========================================

notably, without anything making it clear that fixt is a function, and not the value the user actually expected. Only when we print(fixt) it becomes clear that this is a function object and what the mistake was.

This is due to:

pytest/src/_pytest/assertion/rewrite.py

Lines 459 to 461 in cdddd6d

def _should_repr_global_name(obj: object) -> bool:
if callable(obj):
return False

which was originally introduced in 65edf87.

The rationale for it can be found in the related issue:

  • Global variables are not explained on failure #171

and it boils down to:

I think in some cases it can result in too verbose output, especially when using builtins where you don't really want to have extra information on "len", "callable" etc.

and

Another reason for this heuristic occurred to me: Many commonly used global names like classes and modules are more nicely displayed by their name rather than their repr.

I believe this is useful in cases like assert len(values) == 2 where we really don't need to drill down into what len is. However, it hides vital information in the case that one side of the == is a global callable directly, rather than it being used as a part of a bigger expression.

Activity

  1. RonnyPfannschmidt commented on Oct 20, 2023

    @RonnyPfannschmidt
    RonnyPfannschmidt
    on Oct 20, 2023
    Member
    More actions

    As fixtures are no longer callable, how about turning them into actual objects

  2. Zac-HD
    added
    topic: reportingrelated to terminal output and user-facing messages and errors
    on Oct 21, 2023
  3. Zac-HD commented on Oct 21, 2023

    @Zac-HD
    Zac-HD
    on Oct 21, 2023
    Member
    More actions

    I think changing the if callable(obj): return False may make sense; but I support changing the repr of fixtures to make it obvious that they are fixtures regardless of that.

    Reacted by Freya Bruhin
  4. The-Compiler commented on Oct 23, 2023

    @The-Compiler
    The-Compiler
    on Oct 23, 2023
    MemberAuthor
    More actions

    As fixtures are no longer callable, how about turning them into actual objects

    Depends on what you mean by "actual objects". As long as they are still defined as functions, I'd be somewhat conservative about doing too much stuff with them. I don't know, but decorating a function turning that into something completely different from a function sounds kinda weird to me. Then again, maybe it's fine...

  5. RonnyPfannschmidt commented on Oct 23, 2023

    @RonnyPfannschmidt
    RonnyPfannschmidt
    on Oct 23, 2023
    Member
    More actions

    Having fixtures as Definition objects that aren't callable instead of functions that fail might be a blessing when just wanting to search them

  6. The-Compiler
    added this to pytest sprint 2024on Jun 18, 2024
  7. The-Compiler
    moved this to In Progress in pytest sprint 2024on Jun 18, 2024
  8. nicoddemus
    closed this as completedin #12473on Dec 7, 2024
  9. nicoddemus
    closed this as completedin ecde993on Dec 7, 2024
  10. github-project-automation
    moved this from In Progress to Done in pytest sprint 2024on Dec 7, 2024
  11. nicoddemus
    added a commit that references this issue on Dec 7, 2024

    fixtures: replace fixture representation with a class (pytest-dev#12473)

    This issue will close once commit ecde993 is merged into the 'main' branch.
    ecde993
  12. luketainton
    added a commit that references this issue on Jan 5, 2026

    chore(deps): update dependency pytest to v9 (#305)

    4b75154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: reportingrelated to terminal output and user-facing messages and errors

    Type

    No type

    Projects

    • pytest sprint 2024
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    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.