Skip to content

gh-131178: Fix mimetypes CLI to write error messages to stderr#149683

Open
htjworld wants to merge 5 commits into
python:mainfrom
htjworld:gh-149682-mimetypes-fix-stderr
Open

gh-131178: Fix mimetypes CLI to write error messages to stderr#149683
htjworld wants to merge 5 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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants