Skip to content

gh-92455: Respect case-sensitive mimetype suffixes#148782

Open
yuanx749 wants to merge 15 commits into
python:mainfrom
yuanx749:fix-mimetypes-case-sensitive-add-type
Open

gh-92455: Respect case-sensitive mimetype suffixes#148782
yuanx749 wants to merge 15 commits into
python:mainfrom
yuanx749:fix-mimetypes-case-sensitive-add-type

Conversation

@yuanx749

Copy link
Copy Markdown
Contributor

Fixes gh-92455.

As described in the issue, the current implementation cannot find uppercase
extensions added via mimetypes.add_type(). This appears to be a regression from gh-30229.

This restores the lookup order documented for mimetypes.guess_type():
type suffixes are tried case-sensitively first, then case-insensitively.

@ifrh ifrh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I cannot approve the changes formaly, but they look good for me.

Comment thread Lib/test/test_mimetypes.py Outdated
eq(self.db.guess_type("scheme:foobar.tar.z"), (None, None))

def test_added_types_case_sensitive_preferred(self):
self.db.add_type("text/x-r-script", ".R")

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.

"text/x-r-script" could be added to the database in future. It is better to use something less likely to be added.

Please add also tests for strict=False.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I changed the test type and added tests for strict=False.

@serhiy-storchaka

Copy link
Copy Markdown
Member

#30229 changed also the code related to suffix_map. I wonder if it needs correction too:

        while True:
            if ext in self.suffix_map:
                base, ext = posixpath.splitext(base + self.suffix_map[ext])
                continue
            ext_lower = ext.lower():
            if ext_lower in self.suffix_map:
                base, ext = posixpath.splitext(base + self.suffix_map[ext_lower])
                continue
            break

@kumaraditya303, what are your thoughts?

@kumaraditya303

Copy link
Copy Markdown
Contributor

what are your thoughts?

I think we should do the same for suffix_map i.e. first try case sensitively then case-insensitively.

@yuanx749

yuanx749 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

Would you prefer handling suffix_map in this PR, or keeping this focused on types_map?

@serhiy-storchaka

Copy link
Copy Markdown
Member

It is better to handle all in one PR. Also please update the documentation.

@yuanx749

yuanx749 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Added a failing test, then updated suffix_map to try the original suffix before the lower case suffix. I also updated the documentation. The existing strict=False lookup order is unchanged.

@read-the-docs-community

read-the-docs-community Bot commented Jun 8, 2026

Copy link
Copy Markdown

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

Projects

None yet

4 participants