-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
base: master
Are you sure you want to change the base?
Enable pretty by default #19510
Conversation
I'll make sure to ping. |
This comment has been minimized.
This comment has been minimized.
964f17f
to
8214b19
Compare
This comment has been minimized.
This comment has been minimized.
e60744f
to
8afae7a
Compare
Hi, looks like the
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.
This comment has been minimized.
Yep, this has nothing to do with your PR, |
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
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.
73c1b5b
to
d67c367
Compare
for more information, see https://pre-commit.ci
This comment has been minimized.
This comment has been minimized.
Hello, just wanted to gently bump this for review when you have a moment. To summarize, this PR enables --pretty by default and adapts the various test runners to handle the new behavior. All checks are passing. Let me know if there's any feedback or anything else I can do to help get this merged. No rush, and thanks for your time! |
You know, it's funny... even though I have no interest in this feature myself (besides the cursory interest of thinking it's probably a good idea for mypy), I've considered several times trying to solve it myself, just because it seems so simple and doable. (The main thing that stopped me was the fact that the mypy test suite already doesn't run green on my machine, so it would be hard for me to use --update-data without breaking something else, presumably.) I suspect most people who have tried to solve the problem thus far, no offense to them, have mostly been thinking along the same lines, especially because the issue is labeled good-first-issue, and the other attempted-solvers never got around to fixing the test problems, suggesting they were at the very least not desperate to get this functionality into a release of mypy... Anyway, congrats to @null-dreams for going the distance on this one, and also congrats to him on his first ever PR according to github! |
I'm just some random guy with no authority on this project, but I do have some thoughts:
Again, this is just my professional opinion as an interested 3rd party, and has no bearing on the mypy maintainers' opinion of this PR nor the speed at which they choose to adopt or comment upon it. |
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Sorry for the late comment.
|
@@ -79,6 +79,18 @@ def parse_script(input: list[str]) -> list[list[str]]: | |||
def run_cmd(input: str) -> tuple[int, str]: | |||
if input[1:].startswith("mypy run --") and "--show-error-codes" not in input: | |||
input += " --hide-error-codes" | |||
is_pretty_test = "# NO-MODIFY" in input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I must be missing something - why won't just checking for --pretty
flag do? Yes, it will deviate from normal behavior, but be consistent with all other tests - essentially sticking to old default unless requested otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @sterliakov , this is still my main approach and it is also partly working in the this test-runner to maintain the consistent behavior with all other test-runners.However, I discovered a critical technical constraint: the dmypy
client itself does not accept the --pretty
or --no-pretty flags
.
The formatting flags must be directly passed to the server, which only worked in these two ways for me:
- During startup: $ dmypy start -- --no-pretty
- During one-shot run: $ dmypy run -- --no-pretty
Because the testcases contains a mix of commands where the flag will and won't work, I had to make #NO-MODIFY
as the magic comment to recognize where to not modify the command.
For demonstration:
[case testDaemonRecheck]
$ dmypy start -- --follow-imports=error --no-error-summary
Daemon started
$ dmypy check foo.py bar.py
$ dmypy recheck
$ dmypy recheck --update foo.py --remove bar.py sir_not_appearing_in_this_film.py
foo.py:1: error: Import of "bar" ignored [misc]
foo.py:1: note: (Using --follow-imports=error, module not passed on command line)
== Return code: 1
$ dmypy recheck --update bar.py
$ dmypy recheck --update sir_not_appearing_in_this_film.py
$ dmypy recheck --update --remove
$ dmypy stop
Daemon stopped
[file foo.py]
In this case, my first approach was to insert the --pretty
flag in all of them. But dmypy recheck
threw an unrecognized argument error. So I realized I cannot insert any flag in the commands following dmypy start...
Therefore, I preferred to adding --pretty
flag during startup. But then the test-runner logic detecting so such --pretty
flag in the other commands automatically injects the --no-pretty
flag which also throws an unrecognized argument error. This led to the realization, that I also need to mark these commands someway, that the test-runner doesn't inject --no-pretty
in them. The best I could think of was a magic comment #NO-MODIFY
which I can use to detect these lines where the test-runner shouldn't be interfering.
I understand that this is a very bare-bones workaround, and I am open to any suggestions to make it cleaner.
No matter what, we should definitely keep most of the tests non-pretty. Most of them use inline error comments (they are much easier to verify manually, compared to separate snapshots in I agree, though, that probably more tests should be using And yes, |
Huh, it hadn't even occurred to me — there's no syntax for multi-line inline comments, is there? Nor for ignoring subsequent lines of a pretty comment in a test... |
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
pretty
option is nowTrue
.--no-pretty
is the primary, user-facing flag to disable pretty-printing.--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 inmypy/test/helpers.py
.CmdlineSuite
: The raw command-line argument parser inmypy/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:run_cmd
helper inmypy/test/testdaemon.py
was updated to inject--no-pretty
only into state-setting subcommands (run
,check
). This handles the majority of cases.daemon.test
were manually updated based on how they interact with the daemon's lifecycle:$ 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.$ dmypy run
), the flag was passed via the--
separator (e.g.,run -- --no-pretty test.py
) to configure the new server instance.Stubtest
Unit Tests: The handful ofstubtest
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
--no-pretty
flag.