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 โ€” (๐ŸŽ) color the diff of strings ยท Issue #13175 ยท pytest-dev/pytest

(๐ŸŽ) color the diff of strings #13175

New issue
Copy link
New issue
Copy link
Closed
#13189
Closed
(๐ŸŽ) color the diff of strings#13175
#13189
Copy link

Description

@KotlinIsland
KotlinIsland
opened on Jan 30, 2025
Issue body actions
def test_list():
    assert [1, 2, 3] == [1, 4, 3]
def test_str():
    assert "asdf" == "asgf"

Warning

it is CRITICAL that pygments is installed, otherwise the diff won't be colored at all
this is a secret undocumented feature, see: #13174

> uv init
> uv add pytest pygments
...
> uv run pytest -vv
FAILED test_test.py::test_list - assert [1, 2, 3] == [1, 4, 3]

  At index 1 diff: 2 != 4

  Full diff:
    [
        1,
  -     4,
  ?     ^
  +     2,
  ?     ^
        3,
    ]
FAILED test_test.py::test_str - AssertionError: assert 'asdf' == 'asgf'

  - asgf
  ?   ^
  + asdf
  ?   ^

Image

here we can observe that no coloring has been applied to the string diff

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
    > uv tree
    uv tree
    Resolved 7 packages in 4ms
    test v1
    โ”œโ”€โ”€ pygments v2.19.1
    โ””โ”€โ”€ pytest v8.3.4
        โ”œโ”€โ”€ colorama v0.4.6
        โ”œโ”€โ”€ iniconfig v2.0.0
        โ”œโ”€โ”€ packaging v24.2
        โ””โ”€โ”€ pluggy v1.5.0
    
  • pytest and operating system versions
    windows 10
  • minimal example if possible
Reacted by DetachHead, Freya Bruhin and 5j9

Activity

  1. The-Compiler commented on Jan 30, 2025

    @The-Compiler
    The-Compiler
    on Jan 30, 2025
    Member
    More actions

    Huh, interesting!

    Here is a quick hack that extends _diff_text the same way other diffing functions were in fbe3e29:

    diff --git i/src/_pytest/assertion/util.py w/src/_pytest/assertion/util.py
    index 3fe7eb9d8..9a7c6da72 100644
    --- i/src/_pytest/assertion/util.py
    +++ w/src/_pytest/assertion/util.py
    @@ -242,7 +242,7 @@ def _compare_eq_any(
     ) -> list[str]:
         explanation = []
         if istext(left) and istext(right):
    -        explanation = _diff_text(left, right, verbose)
    +        explanation = _diff_text(left, right, highlighter, verbose)
         else:
             from _pytest.python_api import ApproxBase
     
    @@ -274,7 +274,9 @@ def _compare_eq_any(
         return explanation
     
     
    -def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
    +def _diff_text(
    +    left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0
    +) -> list[str]:
         """Return the explanation for the diff between text.
     
         Unless --verbose is used this will skip leading and trailing
    @@ -315,10 +317,15 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> list[str]:
             explanation += ["Strings contain only whitespace, escaping them using repr()"]
         # "right" is the expected base against which we compare "left",
         # see https://github.com/pytest-dev/pytest/issues/3333
    -    explanation += [
    -        line.strip("\n")
    -        for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
    -    ]
    +    explanation.extend(
    +        highlighter(
    +            "\n".join(
    +                line.strip("\n")
    +                for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
    +            ),
    +            lexer="diff",
    +        ).splitlines()
    +    )
         return explanation
     

    and from a quick test, that seems to help:

    Screenshot

    @BenjaminSchubert was there a reason you didn't do _diff_text as part of #11530 as well, or was that just an oversight perhaps?

  2. BenjaminSchubert commented on Feb 1, 2025

    @BenjaminSchubert
    BenjaminSchubert
    on Feb 1, 2025
    Contributor
    More actions

    Hey!

    This was an oversight, I rarely use the direct string diff and thus did not realize I missed something. I think it's a good idea to extend it here too!

    Reacted by KotlinIsland
  3. The-Compiler
    added a commit that references this issue on Feb 3, 2025

    Also highlight comparisons between strings

    This issue will close once commit 3378a16 is merged into the 'master' branch.
    3378a16
  4. The-Compiler commented on Feb 3, 2025

    @The-Compiler
    The-Compiler
    on Feb 3, 2025
    Member
    More actions

    Thanks @BenjaminSchubert, and thanks @KotlinIsland for the detailed report! I opened #13189 to fix this and also make pygments a mandatory dependecy.

    Reacted by KotlinIsland
    Reacted by KotlinIsland
  5. The-Compiler
    added 3 commits that reference this issue on Feb 3, 2025

    Also highlight comparisons between strings

    This issue will close once commit 44e9f68 is merged into the 'master' branch.
    44e9f68

    Also highlight comparisons between strings

    This issue will close once commit ca6557e is merged into the 'master' branch.
    ca6557e

    Also highlight comparisons between strings

    This issue will close once commit cb089b6 is merged into the 'master' branch.
    cb089b6
  6. The-Compiler
    closed this as completedin #13189on Feb 8, 2025
  7. The-Compiler
    closed this as completedin a3d55a6on Feb 8, 2025
  8. hugovk
    added a commit that references this issue on Feb 9, 2025

    Make pygments mandatory and fix string highlighting (pytest-dev#13189)

    This issue will close once commit a3d55a6 is merged into the 'main' branch.
    a3d55a6
  9. 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      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.