Skip to content

Conversation

@TyYan03
Copy link

@TyYan03 TyYan03 commented Dec 13, 2023

Hello,

This PR resolves #11666.

Description:

Pytest is currently using the TerminalFormatter class from the Pygments library as the application’s universal terminal formatter. This issue focuses on using newer terminal formatters from Pygments which would improve the richness of color and the styling quality.

Solution:

We addressed the issue by implementing the Terminal256Formatter and the TerminalTrueColorFormatter classes, and selected one of the three formatters based on the user’s operating system capabilities.

image

Since the dark styling used in the TerminalFormatter class is not an option for either of the new formatters, we also created a new styling to closely match the dark Pytest design.

image

This gives us the following results:

image
image

Information

Head-fork: tyyan03/pytest compare: issue11666

base-fork: pytest-dev/pytest
base: main

Thanks,
Tyler and Rowan

@bluetech
Copy link
Member

Thanks for working on this Tyler and Rowan!

Since the dark styling used in the TerminalFormatter class is not an option for either of the new formatters, we also created a new styling to closely match the dark Pytest design.

Right, looks like Terminal256Formatter and TerminalTrueColorFormatter don't support the "dark"/"light" bg parameter. So the PYTEST_THEME_MODE envvar will have no effect. I think that's OK, but we'd need to document it.

I also noticed now that with the current pytest code, which uses TerminalFormatter, the PYTEST_THEME envvar is entirely ineffectual, because TerminalFormatter doesn't utilize the style parameter. I guess this was never tested, whoops.

However, Terminal256Formatter and TerminalTrueColorFormatter do respect the style parameter, so fortuitously the PYTEST_THEME now does become relevant.

Currently you are not doing style=getenv('PYTEST_THEME') and instead using a custom style. Any reason not to allow the user to configure it?

@TyYan03
Copy link
Author

TyYan03 commented Dec 14, 2023

Thanks for working on this Tyler and Rowan!

Since the dark styling used in the TerminalFormatter class is not an option for either of the new formatters, we also created a new styling to closely match the dark Pytest design.

Right, looks like Terminal256Formatter and TerminalTrueColorFormatter don't support the "dark"/"light" bg parameter. So the PYTEST_THEME_MODE envvar will have no effect. I think that's OK, but we'd need to document it.

I also noticed now that with the current pytest code, which uses TerminalFormatter, the PYTEST_THEME envvar is entirely ineffectual, because TerminalFormatter doesn't utilize the style parameter. I guess this was never tested, whoops.

However, Terminal256Formatter and TerminalTrueColorFormatter do respect the style parameter, so fortuitously the PYTEST_THEME now does become relevant.

Currently you are not doing style=getenv('PYTEST_THEME') and instead using a custom style. Any reason not to allow the user to configure it?

Although the 'Terminal256Formatter' and 'TerminalTrueColorFormatter' do respect the style parameter, the value has to be a valid pygment style, and os.getenv("PYTEST_THEME") returns None. However, there are many pygment styles that can be used instead of a custom style ('abap', 'algol', 'algol_nu', 'arduino', 'autumn', 'bw', 'borland', 'colorful', 'default', 'dracula', 'emacs', 'friendly_grayscale', 'friendly', 'fruity', 'github-dark', 'gruvbox-dark', 'gruvbox-light', 'igor', 'inkpot', 'lightbulb', 'lilypond', 'lovelace', 'manni', 'material', 'monokai', 'murphy', 'native', 'nord-darker', 'nord', 'one-dark', 'paraiso-dark', 'paraiso-light', 'pastie', 'perldoc', 'rainbow_dash', 'rrt', 'sas', 'solarized-dark', 'solarized-light', 'staroffice', 'stata-dark', 'stata-light', 'tango', 'trac', 'vim', 'vs', 'xcode', 'zenburn'). We can update it to one of these stylings by writing something like, style = 'github-dark', style = os.getenv('PYTEST_THEME_MODE', 'github-dark'), or style = os.getenv('PYTEST_THEME', 'github-dark'). All of these lines have the same result because for some reason os.getenv('PYTEST_THEME') and os.getenv('PYTEST_THEME_MODE') both seem to have no effect.

@bluetech
Copy link
Member

In order for PYTEST_THEME to have an effect with the new formatters, you need to pass it like this (same for the 256 formatter):

terminal_formatter = TerminalTrueColorFormatter( style=os.getenv("PYTEST_THEME"), )
@TyYan03
Copy link
Author

TyYan03 commented Dec 14, 2023

In order for PYTEST_THEME to have an effect with the new formatters, you need to pass it like this (same for the 256 formatter):

terminal_formatter = TerminalTrueColorFormatter( style=os.getenv("PYTEST_THEME"), )

When I pass style=os.getenv("PYTEST_THEME") into the TerminalTrueColorFormatter and Terminal256Formatter it returns "TypeError: 'NoneType' object is not iterable" as an error because os.getenv("PYTEST_THEME") returns None.

@bluetech
Copy link
Member

When I pass style=os.getenv("PYTEST_THEME") into the TerminalTrueColorFormatter and Terminal256Formatter it returns "TypeError: 'NoneType' object is not iterable" as an error because os.getenv("PYTEST_THEME") returns None.

Hmm right, it doesn't accept None. So it would be os.getenv("PYTEST_THEME", "default") to use the pygments default theme, os.getenv('PYTEST_THEME', 'github-dark') if you think that's the better default theme.

I'm not sure why it's not working for you, I suggest you push the change and I can check.

@bluetech
Copy link
Member

I'm going to close this in case anyone else wants to work on it, but let me know if you come back to it.

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

Labels

None yet

2 participants