Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/library/mimetypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

.. deprecated:: 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

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------

Expand Down
9 changes: 1 addition & 8 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,7 @@ 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(f"Extension {ext!r} must start with '.'")

if not type:
return
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,12 @@ 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 = "Extension 'undotted' must start with '.'"
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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove support for undotted *ext* in :meth:`mimetypes.MimeTypes.add_type`.
Loading