Skip to content

Commit 0534774

Browse files
authored
pythongh-149720: Remove support for undotted ext in mimetypes.MimeType.add_type (python#149721)
1 parent f5fb491 commit 0534774

5 files changed

Lines changed: 22 additions & 10 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+
.. deprecated:: 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: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,7 @@ 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),
103-
)
96+
raise ValueError(f"Extension {ext!r} must start with '.'")
10497

10598
if not type:
10699
return

Lib/test/test_mimetypes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,12 @@ 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 = "Extension 'undotted' must start with '.'"
394+
with self.assertRaisesRegex(ValueError, msg):
394395
mimetypes.add_type("testing/type", "undotted")
396+
with self.assertRaisesRegex(ValueError, msg):
397+
mimetypes.add_type("", "undotted")
395398

396399

397400
@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)