Skip to content

Enable pretty by default #19510

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

null-dreams
Copy link

@null-dreams null-dreams commented Jul 25, 2025

This change enables mypy's "pretty" error reporting by default to provide a better out-of-the-box experience for users.

Fixes #19108.

Core Changes

  • The internal default for the pretty option is now True.
  • The command-line flags have been reconfigured so that --no-pretty is the primary, user-facing flag to disable pretty-printing.
  • The old --pretty flag is now a no-op (for backward compatibility) and has been hidden from the --help message.

Test Suite Modifications

Enabling pretty output by default caused widespread test failures, as most tests were written to expect the old plain output. Instead of modifying thousands of test case files, this PR introduces targeted fixes into the central test runners to default them to --no-pretty mode.

This required modifying several distinct test runners:

  • TypeCheckSuite: The main test runner's option parsing was updated in mypy/test/helpers.py.
  • CmdlineSuite: The raw command-line argument parser in mypy/test/testcmdline.py was updated.
  • PEP561Suite & others: Other smaller test runners (testpythoneval, testerrorstream) were also adapted.
  • DaemonSuite: This was the most complex fix due to the daemon's stateful, client-server architecture. The solution required a combination of central logic and manual test case edits:
    • Central Logic: The run_cmd helper in mypy/test/testdaemon.py was updated to inject --no-pretty only into state-setting subcommands (run, check). This handles the majority of cases.
    • Manual Overrides: The test cases in daemon.test were manually updated based on how they interact with the daemon's lifecycle:
      • For tests beginning with $ dmypy start, the --no-pretty flag was added to the server's default flags (e.g., start -- --no-pretty) to set the server's state for the entire run.
      • For tests that rely on an "implicit start" (i.e., the first command is $ dmypy run), the flag was passed via the -- separator (e.g., run -- --no-pretty test.py) to configure the new server instance.
      • For the few tests that expect pretty output, a `# NO-MODIFY "magic comment" was used to have the central logic skip them entirely.
  • Stubtest Unit Tests: The handful of stubtest tests were updated manually to assert against the new pretty-printed output, as their runner does not accept formatting flags.

This overall approach keeps the git diff minimal and isolates the test-related changes to the test infrastructure itself.

Documentation

  • The command-line and config file documentation has been updated to reflect that pretty-printing is the new default and to document the --no-pretty flag.

@sterliakov
Copy link
Collaborator

Supersedes #19122 and #19380. If any of those is merged, please ping me to contribute an envvar:) Anyway, seems like this feature is really wanted...

@null-dreams
Copy link
Author

I'll make sure to ping.

This comment has been minimized.

@null-dreams null-dreams force-pushed the enable-pretty-by-default branch from 964f17f to 8214b19 Compare July 25, 2025 21:42

This comment has been minimized.

@null-dreams null-dreams force-pushed the enable-pretty-by-default branch from e60744f to 8afae7a Compare July 25, 2025 22:35
@null-dreams
Copy link
Author

Hi, looks like the Check documentation build / docs (pull_request) is failing consistently.
The error log points to a problem with intersphinx being unable to fetch the Cython documentation index:

/home/runner/work/mypy/mypy/docs/source/faq.rst:159: WARNING: unknown document: 'cython:index' [ref.doc]

This seems to be an issue with the CI environment and is unrelated to my code changes. I've re-triggered the CI once by pushing an empty commit, but the failure is persistent.

Could a maintainer with permissions please take a look or re-run the failed job? It seems to be the only thing blocking the CI from going green. Thanks!

This comment has been minimized.

@sterliakov
Copy link
Collaborator

Yep, this has nothing to do with your PR, docs.cython.org is down (domain not renewed), we should update the URL.

hauntsaninja pushed a commit that referenced this pull request Jul 26, 2025
Fixes docs build failure discovered in #19510.

Updated setuptools URL too because previous CI runs say that

> intersphinx inventory has moved:
https://setuptools.readthedocs.io/en/latest/objects.inv ->
https://setuptools.pypa.io/en/latest/objects.inv
@sterliakov
Copy link
Collaborator

JFYI the docs issue is now fixed on master

Change the default mypy output to be pretty-printed, providing users with more readable error messages that include code context out-of-the-box.

This commit implements the core logic for this feature:
- In 'mypy/options.py', the default value for 'pretty' is set to 'True'.
- In 'mypy/main.py', the command-line flags are reconfigured:
    - '--no-pretty' is now the primary, user-facing flag to disable the pretty printing.
    - The old '--pretty' flag is deprecated and hidden from help text to reflect that the pretty output is now default.

This change intentionally breaks the test suite, which will be fixediIn subsequent commits by adapting the test runners.

Part of python#19108
This commit adapts the main test runner (`TypeCheckSuite`) to handle the new pretty-by-default behaviour.

- For tests with a `# flags: ` line `--no-pretty` is now injected unless `--pretty` is explicitly requested.
- For tests without any flags, `options.pretty` is directly set to `False` to ensure plain output.

This strategy avoids modifying thousands of individual test-data files.
This commit adapts the `CmdlineSuite` runner to handle the new pretty-by-default behavior.

The `parse_args` helper in `mypy/test/testcmdline.py` is modified to inject the `--no-pretty` flag into the arguments parsed from '# cmd:' lines in test cases.

This ensures that command-line-driven tests, which run in a separate subprocess and bypass other test helpers, are also executed in non-pretty mode by default.

Part of python#19108.
This commit adapts the `pythoneval` test runner to handle the new pretty-by-default behavior.

The `test_python_evaluation` function in `mypy/test/testpythoneval.py` now injects the `--no-pretty` flag by default into the mypy command line it constructs.

Logic is also included to respect a `# flags: --pretty` directive, ensuring that tests designed to check pretty-printing still work correctly.

Part of python#19108.
This commit modifies the `test_error_stream` runner to set `options.pretty = False`, ensuring its tests continue to pass with the new pretty-by-default behavior.

Part of python#19108.
This commit adapts the `DaemonSuite` test runner and its corresponding data files to accommodate the new pretty-by-default behavior.

This fix is nuanced due to the stateful, client-server nature of the daemon and required a two-part approach:

1.  **Central Logic:** The `run_cmd` helper in `mypy/test/testdaemon.py` was modified to inject `--no-pretty` only into state-setting subcommands (`run`, `check`). This handles the majority of daemon tests.

2.  **Manual Overrides:** A number of test cases in `daemon.test` were manually updated. This was necessary for tests that start the daemon with `dmypy start` or have other unique requirements, ensuring  the server is correctly configured in non-pretty mode from the outset.

This combined strategy ensures the entire daemon test suite passes.

Part of python#19108.
This commit adapts the `PEP561Suite` test runner to handle the new pretty-by-default behavior.

The `parse_mypy_args` helper in `mypy/test/testpep561.py` is modified to inject the `--no-pretty` flag into the arguments parsed from '# flags:' lines in test cases.

Part of python#19108.
This commit adapts several unit tests in the `StubtestMiscUnit` suite to handle the new pretty-by-default behavior.

Because the `stubtest` runner is a distinct command-line tool, injecting the `--no-pretty` flag was not feasible as it's an unrecognized argument.

Instead, the `assert` statements in the failing tests have been updated to match the new, pretty-printed error output from mypy.

Part of python#19108.
This commit updates the user-facing documentation to reflect that pretty-printing is now the default output format.

- The `--pretty` flag documentation in `command_line.rst` has been replaced with documentation for the new `--no-pretty` flag.
- The `config_file.rst` documentation for the `pretty` option has been updated to state that its default is now `True`.

Fixes python#19108.
@null-dreams null-dreams force-pushed the enable-pretty-by-default branch from 73c1b5b to d67c367 Compare July 26, 2025 06:04
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@null-dreams null-dreams marked this pull request as ready for review July 26, 2025 06:40
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.

Enable --pretty by default
2 participants