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

Fix ANSI escape codes for colored output not handled correctly with pytest.fail(..., pytrace=False) #12959

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

leonarduschen
Copy link

@leonarduschen leonarduschen commented Nov 13, 2024

Closes #12849


Description:


  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • Add text like closes #XYZW to the PR description and/or commits (where XYZW is the issue number). See the github docs for more information.
  • Create a new changelog file in the changelog folder, with a name like <ISSUE NUMBER>.<TYPE>.rst. See changelog/README.rst for details.
  • Add yourself to AUTHORS in alphabetical order.

@leonarduschen leonarduschen marked this pull request as draft November 13, 2024 17:34
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 14, 2024
@leonarduschen leonarduschen changed the title Fix ExcInfo.value printed as source code Fix ANSI escape codes for colored output not handled correctly with pytest.fail(..., pytrace=False) Nov 14, 2024
@leonarduschen leonarduschen marked this pull request as ready for review November 14, 2024 16:32
@@ -119,8 +119,8 @@ def markup(self, text, **kw):
return text

def get_write_msg(self, idx):
flag, msg = self.lines[idx]
assert flag == TWMock.WRITE
assert self.lines[idx][0] == TWMock.WRITE
Copy link
Author

Choose a reason for hiding this comment

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

Added this so that calling get_write_msg raises AssertionError instead of ValueError: Not enough values to unpack

I can revert it back if needed

Copy link
Member

Choose a reason for hiding this comment

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

That's fine, thanks!

@@ -1221,6 +1221,12 @@ def _write_entry_lines(self, tw: TerminalWriter) -> None:
if not self.lines:
return

if self.style == "value":
for line in self.lines:
tw.write(line)
Copy link
Author

Choose a reason for hiding this comment

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

Using TerminalWriter.write instead of TerminalWriter.line because when using TWMock . we can't distinguish lines written with TWMock._write_source and lines written with line directly, whereas with TWMock.write we can, with the flag.

We could add a flag for TWMock._write_source as well, like so:

class TWMock:
    WRITE = object()
    WRITE_SOURCE = object()

    def _write_source(self, lines, indents=()):
        if not indents:
            indents = [""] * len(lines)
        for indent, line in zip(indents, lines):
            newline = indent + line
            self.line((TWMock.WRITE_SOURCE, newline))

But then we'll have to change around half the test cases in test_excinfo.py

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation! I guess this rationale would be nice to have as a comment in the code itself, to help future readers.

Copy link
Author

Choose a reason for hiding this comment

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

sure

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the PR, appreciate it!

@@ -1221,6 +1221,12 @@ def _write_entry_lines(self, tw: TerminalWriter) -> None:
if not self.lines:
return

if self.style == "value":
for line in self.lines:
tw.write(line)
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation! I guess this rationale would be nice to have as a comment in the code itself, to help future readers.

@@ -119,8 +119,8 @@ def markup(self, text, **kw):
return text

def get_write_msg(self, idx):
flag, msg = self.lines[idx]
assert flag == TWMock.WRITE
assert self.lines[idx][0] == TWMock.WRITE
Copy link
Member

Choose a reason for hiding this comment

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

That's fine, thanks!

@nicoddemus nicoddemus added the backport 8.3.x apply to PRs at any point; backports the changes to the 8.3.x branch label Nov 21, 2024
@nicoddemus
Copy link
Member

Thanks @leonarduschen!

Will squash/merge in a few days to give others a chance to review it. 👍

@leonarduschen
Copy link
Author

Sure thing @nicoddemus, thanks for the review! Excited for my first contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 8.3.x apply to PRs at any point; backports the changes to the 8.3.x branch bot:chronographer:provided (automation) changelog entry is part of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ANSI escape codes for colored output not handled correctly with pytest.fail(reason=..., pytrace=False)
2 participants