Skip to content

gh-118626: Use less red-ish colour for non-error traceback #119966

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

Closed
wants to merge 2 commits into from

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Jun 3, 2024

Fixes #118626.

The magenta color used by the new REPL and within the non-error text in the new colorized tracebacks in 3.13 looks quite a bit like an error-related color in some popular color schemes.

In this PR, I'm trying to mirror something like GitHub's pytb colour groups to avoid having too many colours:

>>> 4 / 0
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    4 / 0
    ~~^~~
ZeroDivisionError: division by zero
>>>
>>> 3.to_bytes(1, 'little')"
  File "<string>", line 1
    3.to_bytes(1, 'little')
     ^
SyntaxError: invalid decimal literal

In the before and after below, I'm using macOS and have iTerm with a dark theme, and Terminal with light theme (Novel), to make it easy to compare without having to dig in the settings each time.

Before image image
After image image
GitHub pytb image image

And a longer traceback using this script based on a test:

2.py
def foo(*args):
    x = {'a': {'b': None}}
    y = x['a']['b']['c']


def baz2(*args):
    return (lambda *args: foo(*args))(1, 2, 3, 4)


def baz1(*args):
    return baz2(1, 2, 3, 4)


def bar():
    return baz1(1,
                2, 3
                , 4)


bar()
pytb
❯ ./python.exe 2.py
Traceback (most recent call last):
  File "/Users/hugo/github/python/cpython/main/2.py", line 20, in <module>
    bar()
    ~~~^^
  File "/Users/hugo/github/python/cpython/main/2.py", line 15, in bar
    return baz1(1,
                2, 3
                , 4)
  File "/Users/hugo/github/python/cpython/main/2.py", line 11, in baz1
    return baz2(1, 2, 3, 4)
  File "/Users/hugo/github/python/cpython/main/2.py", line 7, in baz2
    return (lambda *args: foo(*args))(1, 2, 3, 4)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Users/hugo/github/python/cpython/main/2.py", line 7, in <lambda>
    return (lambda *args: foo(*args))(1, 2, 3, 4)
                          ~~~^^^^^^^
  File "/Users/hugo/github/python/cpython/main/2.py", line 3, in foo
    y = x['a']['b']['c']
        ~~~~~~~~~~~^^^^^
TypeError: 'NoneType' object is not subscriptable
Before image image
After image image
GitHub pytb image image

@nineteendo
Copy link
Contributor

nineteendo commented Jun 3, 2024

Hugo, could you change the color of ">>>" as well? It's not magenta for pytb. Or does that need its own PR?

ps1 = f"{ANSIColors.BOLD_MAGENTA}{ps1}{ANSIColors.RESET}"

prompt = f"{ANSIColors.BOLD_MAGENTA}{prompt}{ANSIColors.RESET}"

@hugovk
Copy link
Member Author

hugovk commented Jun 3, 2024

How keen is @ambv on keeping the magenta prompt?

main
PR now
PR + blue prompt

@ambv
Copy link
Contributor

ambv commented Jun 3, 2024

Let's keep the prompt color. In the end we want to make this configurable anyway since there's no way we will keep everybody happy.

@serban
Copy link

serban commented Jun 3, 2024

I find the red and magenta combination in the stack trace jarring, and I endorse this pull request with the following suggestion:

Given the vast number of environments and terminals that Python runs in, it should stick to a conservative ANSI color palette and avoid the use of “bold” colors since the result of setting the “bold” attribute varies between terminal emulators and user configuration [1] [2]. Using the six basic ANSI colors without any attributes set would be the safest option:

Color Foreground
1 Red 31
2 Green 32
3 Yellow 33
4 Blue 34
5 Magenta 35
6 Cyan 36

[1] https://invisible-island.net/xterm/xterm.faq.html#bold_vs_16colors
[2] https://unix.stackexchange.com/a/696593

@hugovk
Copy link
Member Author

hugovk commented Jun 18, 2024

Last week I made some slides to show the improved error messages coming in 3.13, and used the example from What's New.

These have quite big error messages, and I find the blue of this PR (lower example) much better than the original magenta (upper example):

@ambv @pablogsal You've spoken of adding config for all these things, but I think this improves the defaults. What do you think?

And cc @treyhunner as issue reporter, what do you think?

@pablogsal
Copy link
Member

pablogsal commented Jun 18, 2024

@ambv @pablogsal You've spoken of adding config for all these things, but I think this improves the defaults. What do you think?

I'm unfortunately very colourblind so I fear my answer here it's going to be very biased and maybe not representative of the average user when selecting the defaults 😕

@nineteendo
Copy link
Contributor

Could you remove the quoted text from your comment? Or only include the question?

@treyhunner
Copy link
Member

And cc @treyhunner as issue reporter, what do you think?

I like the blue that is currently in this PR better (on my color scheme at least). I like the blue prompt in the bottom row of #119966 (comment) even better.

I believe @ambv mentioned trying to keep the changes to just 2 colors, so as not to cause visual overwhelm.

A note: depending on whether "bold as bright" is enabled on the terminal emulator, the blue may show up as a different color instead (it does in the Solarized Light color scheme).

Without "bold text as bright colors" enabled:

image

With "bold text as bright colors" enabled:

image

With the default Ubuntu Terminal settings, the blue looks a bit low contrast:

image

"Bold as bright" fixes that, though this setting does not seem to be the default on Gnome Terminal in Ubuntu 22.04. I believe it is the default on many Mac terminal emulators, but I have not researched this.

@ambv ambv removed the needs backport to 3.13 bugs and security fixes label Sep 6, 2024
@hugovk
Copy link
Member Author

hugovk commented May 4, 2025

Closing in favour of theming support: #133346

@hugovk hugovk closed this May 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review topic-repl Related to the interactive shell
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use a less red-ish color for the new prompt and non-error colorized traceback text
6 participants