Skip to content

gh-131178: Fix mimetypes CLI docs to mention that errors go to stdout#149683

Merged
sobolevn merged 10 commits into
python:mainfrom
htjworld:gh-149682-mimetypes-fix-stderr
May 31, 2026
Merged

gh-131178: Fix mimetypes CLI docs to mention that errors go to stdout#149683
sobolevn merged 10 commits into
python:mainfrom
htjworld:gh-149682-mimetypes-fix-stderr

Conversation

@htjworld
Copy link
Copy Markdown
Contributor

@htjworld htjworld commented May 11, 2026

The mimetypes documentation states that errors go to the standard error stream, but the CLI has always written all output — including errors — to stdout.

Fix the documentation to match the actual behavior, and add subprocess-based tests for the command-line interface.

Changes:

  • Doc/library/mimetypes.rst: Correct "standard error stream" → "standard output stream"; update stale examples (filename.pict is now a recognized type, error message strings did not match actual output).
  • Lib/test/test_mimetypes.py: Add subprocess-based tests verifying CLI output format and exit codes.

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 11, 2026

@htjworld htjworld changed the title gh-149682: Fix mimetypes CLI to write error messages to stderr Fix mimetypes CLI to write error messages to stderr May 11, 2026
@htjworld htjworld changed the title Fix mimetypes CLI to write error messages to stderr gh-149682: Fix mimetypes CLI to write error messages to stderr May 11, 2026
@htjworld htjworld changed the title gh-149682: Fix mimetypes CLI to write error messages to stderr gh-131178: Fix mimetypes CLI to write error messages to stderr May 11, 2026
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

This is a rather hard problem: when docs do not represent the reality, we can either fix docs or fix the code.

I guess a lot of places already rely on the fact that errors and results are in the stdout, not in stderr.

So, I think that it would be better to change the docs.

Comment thread Lib/test/test_mimetypes.py Outdated

def test_type_lookup(self):
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf')
self.assertIn(b'application/pdf', stdout)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not asserting the full stdout contents?

Comment thread Lib/test/test_mimetypes.py Outdated
def test_type_lookup_unknown(self):
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', 'foo.unknownext12345')
self.assertEqual(stdout, b'')
self.assertIn(b'error:', stderr)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And the same here: we can assert the full error response.

Comment thread Lib/mimetypes.py Outdated
has_error = False
for result in results:
if result.startswith("error: "):
print(result, file=sys.stderr, flush=True)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a behavior change, please revert it.

@@ -0,0 +1,2 @@
Fix :mod:`mimetypes` CLI to write error messages to stderr instead of stdout,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And since we revert the behavior change, we need to remove this file as well :)

htjworld added 2 commits May 12, 2026 00:33
Per sobolevn's review: revert the behavior change that routed error
messages to stderr, as existing code may rely on stdout behavior.
Instead update docs to accurately reflect that errors go to stdout.

Also update tests to assert full output contents as suggested.
@htjworld
Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 11, 2026

Thanks for making the requested changes!

: please review the changes made to this pull request.

Comment thread Lib/test/test_mimetypes.py Outdated

def test_unknown_flag(self):
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '--unknown-flag')
self.assertNotEqual(rc, 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

assert the output please :)

@@ -0,0 +1,2 @@
Add subprocess-based tests for the :mod:`mimetypes` command-line interface,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we don't add news for docs / tests only changes.

Comment thread Lib/test/test_mimetypes.py Outdated

def test_extension_flag(self):
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '-e', 'image/jpeg')
self.assertIn(b'.jpg', stdout)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's use assertEqual here as well.

@htjworld
Copy link
Copy Markdown
Contributor Author

thanks for the review appreciate it
I have made the requested changes; please review again

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 11, 2026

Thanks for making the requested changes!

: please review the changes made to this pull request.

Comment thread Lib/test/test_mimetypes.py Outdated
def test_unknown_flag(self):
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '--unknown-flag')
self.assertEqual(stdout, b'')
self.assertIn(b'error', stderr)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
self.assertIn(b'error', stderr)
self.assertIn(b'error: unrecognized arguments: --unknown-flag', stderr)

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 12, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks!

@sobolevn
Copy link
Copy Markdown
Member

CC @clin1234 who commits regularly to mimetypes :)

Copy link
Copy Markdown
Contributor

@clin1234 clin1234 left a comment

Choose a reason for hiding this comment

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

If CI passes, it LGTM ;-)

@htjworld
Copy link
Copy Markdown
Contributor Author

Hi @sobolevn, gentle ping 🙂 — is there anything else needed on my end for this to move forward? Thanks!

@sobolevn sobolevn changed the title gh-131178: Fix mimetypes CLI to write error messages to stderr gh-131178: Fix mimetypes CLI docs to mention that errors go to stdout May 31, 2026
@sobolevn sobolevn added needs backport to 3.15 pre-release feature fixes, bugs and security fixes needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels May 31, 2026
Comment thread Doc/library/mimetypes.rst
Comment thread Doc/library/mimetypes.rst
…docs

Restore the original filename.pict / --lenient example and the
filename.xxx multi-input example. Keep only the genuinely-needed
fix: the error message string ("unknown extension of X" was never
emitted; the CLI prints "media type unknown for X").
Comment thread Doc/library/mimetypes.rst
Co-authored-by: sobolevn <mail@sobolevn.me>
@sobolevn
Copy link
Copy Markdown
Member

Thanks a lot!

@sobolevn sobolevn enabled auto-merge (squash) May 31, 2026 07:17
@htjworld
Copy link
Copy Markdown
Contributor Author

Thank you for the thorough review!

@sobolevn sobolevn merged commit 2b94b92 into python:main May 31, 2026
52 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @htjworld for the PR, and @sobolevn for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app
Copy link
Copy Markdown

Sorry, @htjworld and @sobolevn, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 2b94b923943a1f75cdbd5a5e4e2eb5f93eb43e23 3.13

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 31, 2026

GH-150655 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 31, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 31, 2026

GH-150656 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label May 31, 2026
@sobolevn
Copy link
Copy Markdown
Member

@htjworld can you please follow the instructions in #149683 (comment) and create a manual 3.13 backport?

@htjworld
Copy link
Copy Markdown
Contributor Author

@sobolevn, I looked into the 3.13 backport — the mimetypes CLI was reworked after 3.13, so the changes don't seem to apply cleanly:

  • The Command-line usage section this PR edits doesn't exist in 3.13 (it appears to have been added in 3.14).
  • The 3.13 CLI still uses the old getopt-based implementation and prints I don't know anything about type X rather than error: media type unknown for X, so the new CommandLineSubprocessTest assertions would fail there.

Would it make sense to drop the needs backport to 3.13 label?

@sobolevn
Copy link
Copy Markdown
Member

Then no - thanks for the investigation :)

sobolevn added a commit that referenced this pull request May 31, 2026
…tdout (GH-149683) (#150656)

gh-131178: Fix mimetypes CLI docs, mention that errors go to stdout (GH-149683)
(cherry picked from commit 2b94b92)

Co-authored-by: htjworld <116538001+htjworld@users.noreply.github.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this pull request May 31, 2026
…tdout (GH-149683) (#150655)

gh-131178: Fix mimetypes CLI docs, mention that errors go to stdout (GH-149683)
(cherry picked from commit 2b94b92)

Co-authored-by: htjworld <116538001+htjworld@users.noreply.github.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants