Skip to content

Commit 9c8bc62

Browse files
committed
gh-149720: Remove support for undotted ext in mimetypes.MimeType.add_type
1 parent f5fb491 commit 9c8bc62

5 files changed

Lines changed: 33 additions & 9 deletions

File tree

Doc/library/mimetypes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,18 @@ behavior of the module.
129129
Add a mapping from the MIME type *type* to the extension *ext*. When the
130130
extension is already known, the new type will replace the old one. When the type
131131
is already known the extension will be added to the list of known extensions.
132+
Valid extensions are empty or start with a ``'.'``.
132133

133134
When *strict* is ``True`` (the default), the mapping will be added to the
134135
official MIME types, otherwise to the non-standard ones.
135136

137+
.. versinchanged:: 3.14
138+
*ext* values that do not start with ``'.'`` are deprecated.
139+
140+
.. versionchanged:: next
141+
*ext* now must start with ``'.'``. Otherwise :exc:`ValueError` is raised.
142+
143+
136144

137145
.. data:: inited
138146

Doc/whatsnew/3.16.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ logging
127127
and scheduled for removal in Python 3.16. Define handlers with the *stream*
128128
argument instead.
129129

130+
mimetypes
131+
---------
132+
133+
* Valid extensions start with a '.' or are empty for
134+
:meth:`mimetypes.MimeTypes.add_type`.
135+
Undotted extensions now raise a :exc:`ValueError`.
136+
130137
symtable
131138
--------
132139

Lib/mimetypes.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,9 @@ def add_type(self, type, ext, strict=True):
9393
Valid extensions are empty or start with a '.'.
9494
"""
9595
if ext and not ext.startswith('.'):
96-
from warnings import _deprecated
97-
98-
_deprecated(
99-
"Undotted extensions",
100-
"Using undotted extensions is deprecated and "
101-
"will raise a ValueError in Python {remove}",
102-
remove=(3, 16),
96+
raise ValueError(
97+
"Adding an extension without a leading dot "
98+
f"{ext!r} is not supported"
10399
)
104100

105101
if not type:

Lib/test/test_mimetypes.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,21 @@ def test_added_types_are_used(self):
389389
mime_type, _ = mimetypes.guess_type('test.myext')
390390
self.assertEqual(mime_type, 'testing/type')
391391

392-
def test_add_type_with_undotted_extension_deprecated(self):
393-
with self.assertWarns(DeprecationWarning):
392+
def test_add_type_with_undotted_extension_not_supported(self):
393+
msg = (
394+
"Adding an extension without a leading dot "
395+
"'undotted' is not supported"
396+
)
397+
with self.assertRaisesRegex(
398+
ValueError,
399+
msg,
400+
):
394401
mimetypes.add_type("testing/type", "undotted")
402+
with self.assertRaisesRegex(
403+
ValueError,
404+
msg,
405+
):
406+
mimetypes.add_type("", "undotted")
395407

396408

397409
@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove support for undotted *ext* in :meth:`mimetypes.MimeTypes.add_type`.

0 commit comments

Comments
 (0)