From 9c8bc62de74b0248984cecd981d45e9b5183cfc2 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 12 May 2026 14:45:16 +0300 Subject: [PATCH 1/3] gh-149720: Remove support for undotted `ext` in `mimetypes.MimeType.add_type` --- Doc/library/mimetypes.rst | 8 ++++++++ Doc/whatsnew/3.16.rst | 7 +++++++ Lib/mimetypes.py | 10 +++------- Lib/test/test_mimetypes.py | 16 ++++++++++++++-- ...026-05-12-14-44-39.gh-issue-149720.ccsoW-.rst | 1 + 5 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-05-12-14-44-39.gh-issue-149720.ccsoW-.rst diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index 0facacd50fd389..52c6cc781797cf 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -129,10 +129,18 @@ behavior of the module. Add a mapping from the MIME type *type* to the extension *ext*. When the extension is already known, the new type will replace the old one. When the type is already known the extension will be added to the list of known extensions. + Valid extensions are empty or start with a ``'.'``. When *strict* is ``True`` (the default), the mapping will be added to the official MIME types, otherwise to the non-standard ones. + .. versinchanged:: 3.14 + *ext* values that do not start with ``'.'`` are deprecated. + + .. versionchanged:: next + *ext* now must start with ``'.'``. Otherwise :exc:`ValueError` is raised. + + .. data:: inited diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index 6d91d53f478d8e..f79f976ee55cfa 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -127,6 +127,13 @@ logging and scheduled for removal in Python 3.16. Define handlers with the *stream* argument instead. +mimetypes +--------- + +* Valid extensions start with a '.' or are empty for + :meth:`mimetypes.MimeTypes.add_type`. + Undotted extensions now raise a :exc:`ValueError`. + symtable -------- diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index 6d9278bccf927e..f261141c50c96e 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -93,13 +93,9 @@ def add_type(self, type, ext, strict=True): Valid extensions are empty or start with a '.'. """ if ext and not ext.startswith('.'): - from warnings import _deprecated - - _deprecated( - "Undotted extensions", - "Using undotted extensions is deprecated and " - "will raise a ValueError in Python {remove}", - remove=(3, 16), + raise ValueError( + "Adding an extension without a leading dot " + f"{ext!r} is not supported" ) if not type: diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index 2d618081521e10..850f968474b5f5 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -389,9 +389,21 @@ def test_added_types_are_used(self): mime_type, _ = mimetypes.guess_type('test.myext') self.assertEqual(mime_type, 'testing/type') - def test_add_type_with_undotted_extension_deprecated(self): - with self.assertWarns(DeprecationWarning): + def test_add_type_with_undotted_extension_not_supported(self): + msg = ( + "Adding an extension without a leading dot " + "'undotted' is not supported" + ) + with self.assertRaisesRegex( + ValueError, + msg, + ): mimetypes.add_type("testing/type", "undotted") + with self.assertRaisesRegex( + ValueError, + msg, + ): + mimetypes.add_type("", "undotted") @unittest.skipUnless(sys.platform.startswith("win"), "Windows only") diff --git a/Misc/NEWS.d/next/Library/2026-05-12-14-44-39.gh-issue-149720.ccsoW-.rst b/Misc/NEWS.d/next/Library/2026-05-12-14-44-39.gh-issue-149720.ccsoW-.rst new file mode 100644 index 00000000000000..f16d8770d740f4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-12-14-44-39.gh-issue-149720.ccsoW-.rst @@ -0,0 +1 @@ +Remove support for undotted *ext* in :meth:`mimetypes.MimeTypes.add_type`. From 0a16e9c83d085322297f30bb01b00b3ce4ea858a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 12 May 2026 14:50:44 +0300 Subject: [PATCH 2/3] Fix typo --- Doc/library/mimetypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index 52c6cc781797cf..f9eeaa2f650dfd 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -134,7 +134,7 @@ behavior of the module. When *strict* is ``True`` (the default), the mapping will be added to the official MIME types, otherwise to the non-standard ones. - .. versinchanged:: 3.14 + .. versionchanged:: 3.14 *ext* values that do not start with ``'.'`` are deprecated. .. versionchanged:: next From c3dbd0d1b6d215421ad22afb6dc6e476c5641a1f Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 12 May 2026 16:07:35 +0300 Subject: [PATCH 3/3] Address review --- Doc/library/mimetypes.rst | 2 +- Lib/mimetypes.py | 5 +---- Lib/test/test_mimetypes.py | 15 +++------------ 3 files changed, 5 insertions(+), 17 deletions(-) diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index f9eeaa2f650dfd..eed9acb92e0c95 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -134,7 +134,7 @@ behavior of the module. When *strict* is ``True`` (the default), the mapping will be added to the official MIME types, otherwise to the non-standard ones. - .. versionchanged:: 3.14 + .. deprecated:: 3.14 *ext* values that do not start with ``'.'`` are deprecated. .. versionchanged:: next diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index f261141c50c96e..15e8c0a437bfd9 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -93,10 +93,7 @@ def add_type(self, type, ext, strict=True): Valid extensions are empty or start with a '.'. """ if ext and not ext.startswith('.'): - raise ValueError( - "Adding an extension without a leading dot " - f"{ext!r} is not supported" - ) + raise ValueError(f"Extension {ext!r} must start with '.'") if not type: return diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index 850f968474b5f5..607aff8418edcf 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -390,19 +390,10 @@ def test_added_types_are_used(self): self.assertEqual(mime_type, 'testing/type') def test_add_type_with_undotted_extension_not_supported(self): - msg = ( - "Adding an extension without a leading dot " - "'undotted' is not supported" - ) - with self.assertRaisesRegex( - ValueError, - msg, - ): + msg = "Extension 'undotted' must start with '.'" + with self.assertRaisesRegex(ValueError, msg): mimetypes.add_type("testing/type", "undotted") - with self.assertRaisesRegex( - ValueError, - msg, - ): + with self.assertRaisesRegex(ValueError, msg): mimetypes.add_type("", "undotted")