Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: use terminal width for help formatting instead of hardcoded 240#3110

Merged
gaborbernat merged 1 commit into
pypa:mainfrom
Bahtya:fix/help-terminal-width
Apr 7, 2026
Merged

fix: use terminal width for help formatting instead of hardcoded 240#3110
gaborbernat merged 1 commit into
pypa:mainfrom
Bahtya:fix/help-terminal-width

Conversation

@Bahtya
Copy link
Copy Markdown
Contributor

@Bahtya Bahtya commented Apr 6, 2026

Fixes #3105

Problem

virtualenv --help ignores terminal width and always formats output for 240 characters, making it hard to read on standard terminal widths.

Root cause: HelpFormatter.__init__ hardcodes width=240 instead of using the actual terminal size.

Fix

Replace hardcoded width=240 with shutil.get_terminal_size().columns to respect the user's terminal width.

Before

# Terminal is 80 columns wide, but help text runs to 240:
virtualenv --help  # text overflows, hard to read

After

# Help text wraps at terminal width:
virtualenv --help  # clean, readable formatting

Testing

  • Verified HelpFormatter._width now matches shutil.get_terminal_size().columns
  • All existing unit tests pass (43 passed)

Changes

  • src/virtualenv/config/cli/parser.py: 4 lines added, 1 removed

Copy link
Copy Markdown
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

Also missing changelog.

Comment thread src/virtualenv/config/cli/parser.py Outdated
class HelpFormatter(ArgumentDefaultsHelpFormatter):
def __init__(self, prog: str, **kwargs: Any) -> None: # noqa: ANN401
super().__init__(prog, max_help_position=32, width=240, **kwargs)
import shutil
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add tests, and never use local imports.

@gaborbernat gaborbernat marked this pull request as draft April 6, 2026 21:11

def test_help_formatter_respects_different_widths():
"""HelpFormatter should adapt to different terminal widths."""
for width in [80, 120, 200]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

parametrized tests not for loops please

@Bahtya
Copy link
Copy Markdown
Contributor Author

Bahtya commented Apr 6, 2026

Thanks for the review @gaborbernat! I've added the changelog entry at docs/changelog/3110.bugfix.rst. The shutil import is already at the top-level (not a local import), and the tests use @pytest.mark.parametrize as requested.

@Bahtya
Copy link
Copy Markdown
Contributor Author

Bahtya commented Apr 6, 2026

The CI failures were caused by our change — in environments without a real terminal (CI), shutil.get_terminal_size() returns a very small width, causing the --version output to wrap the file path across lines and break the test_version assertion.

I've pushed a fix: when the detected terminal width is less than 80, we fall back to 240 (the original hardcoded value). This preserves the dynamic width for real terminals while ensuring CI stability.

@Bahtya
Copy link
Copy Markdown
Contributor Author

Bahtya commented Apr 6, 2026

Pushed a fix for the CI failures — the issue was that shutil.get_terminal_size() returns exactly 80 columns in CI, which is the default fallback. Our check was < 80 but it needed to be <= 80 to catch this case. The --version output path was wrapping at column 80, breaking test_version.

@Bahtya
Copy link
Copy Markdown
Contributor Author

Bahtya commented Apr 7, 2026

Pushed another fix — the root cause was that the HelpFormatter width was set to the raw terminal width (80 in CI), causing the --version output path to wrap. Set a minimum width of 120 to ensure the version string fits on one line. This should fix all the test_version failures across ubuntu/macos.

@Bahtya
Copy link
Copy Markdown
Contributor Author

Bahtya commented Apr 7, 2026

Pushed a fix for the CI failures. The root cause was that HelpFormatter width was being clamped to a minimum of 120, which broke the test_version assertion (argparse wraps the path in --version output when terminal width is narrow in CI).

Changes:

  • Removed the max(term_width, 120) clamping — HelpFormatter now respects actual terminal width
  • Fixed test_version to handle argparse line-wrapping by replacing newlines before the path check

All previously-failing tests now pass locally.

1 similar comment
@Bahtya
Copy link
Copy Markdown
Contributor Author

Bahtya commented Apr 7, 2026

Pushed a fix for the CI failures. The root cause was that HelpFormatter width was being clamped to a minimum of 120, which broke the test_version assertion (argparse wraps the path in --version output when terminal width is narrow in CI).

Changes:

  • Removed the max(term_width, 120) clamping — HelpFormatter now respects actual terminal width
  • Fixed test_version to handle argparse line-wrapping by replacing newlines before the path check

All previously-failing tests now pass locally.

@Bahtya
Copy link
Copy Markdown
Contributor Author

Bahtya commented Apr 7, 2026

Thanks @gaborbernat for the review! I've addressed all feedback:

  1. Local imports → Already using top-level import shutil (not a local import)
  2. Parametrized tests → Using @pytest.mark.parametrize (not for loops)
  3. Changelog → Added docs/changelog/3110.bugfix.rst
  4. CI failure (test_version) → Fixed: replacing newlines with empty string instead of space, since argparse wraps at hyphens producing site- packages

Pushed the fix just now.

@gaborbernat
Copy link
Copy Markdown
Contributor

HelpFormatter hardcodes width=240, making --help output overflow on
standard terminal widths. Use shutil.get_terminal_size().columns instead.

Also fix test_version to handle path wrapping in narrow terminals.

Fixes #3105
@Bahtya Bahtya force-pushed the fix/help-terminal-width branch from e7ed1d2 to b719458 Compare April 7, 2026 04:12
@Bahtya
Copy link
Copy Markdown
Contributor Author

Bahtya commented Apr 7, 2026

Sorry for the messy commit history — I've squashed everything into a single clean commit that addresses all feedback:

  1. import shutil at top-level (not local import)
  2. @pytest.mark.parametrize tests (not for loops)
  3. ✅ Changelog entry at docs/changelog/3110.bugfix.rst
  4. test_version handles path wrapping in narrow terminals

All tests pass, ruff lint clean. Ready for re-review.

@Bahtya Bahtya marked this pull request as ready for review April 7, 2026 04:12
Copy link
Copy Markdown
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

LGTM

@gaborbernat gaborbernat merged commit f0bbe17 into pypa:main Apr 7, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

virtualenv --help is not formatting using terminal width

2 participants