Skip to content

gh-75223: Deprecate undotted extensions in mimetypes.MimeTypes.add_type #128638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2e6a0e5
Add a test for mimetypes.add_type
OddBloke Jul 26, 2017
1485be9
bpo-31040: Reject undotted extensions in mimetypes.add_type
OddBloke Jul 26, 2017
f0c1bf7
Address the review
arhadthedev Apr 15, 2023
0d9bec2
Merge branch 'main' into fix-for-31040
arhadthedev Apr 15, 2023
2f23267
Merge branch 'main' into fix-for-31040
hugovk Nov 6, 2024
ef5cf14
Assert output first, expected result second; link to method in news
hugovk Nov 16, 2024
bac8d5d
Assert adding MIME type with empty extension emits no warning
hugovk Nov 16, 2024
e177398
Assert exception message when adding MIME type with undotted extension
hugovk Nov 16, 2024
094f69b
Remove unused import
hugovk Nov 16, 2024
cbc5421
Fix Sphinx reference; replace bpo with gh-issue
hugovk Nov 16, 2024
41d4e89
Replace with more useful test case
hugovk Nov 16, 2024
50179cf
Improve wording: avoid triple negative
hugovk Nov 16, 2024
352811b
Fix news filename
hugovk Nov 16, 2024
c79666b
Merge remote-tracking branch 'upstream/main' into deprecate-undotted-…
hugovk Jan 8, 2025
e3b2ee9
Deprecate undotted extensions in mimetypes.add_type
hugovk Jan 8, 2025
6f8eed7
Merge branch 'main' into 3.14-deprecate-undotted-mimetypes
hugovk Feb 7, 2025
43b37b7
Merge branch 'main' into 3.14-deprecate-undotted-mimetypes
hugovk Feb 28, 2025
8118997
Merge branch 'main' into 3.14-deprecate-undotted-mimetypes
hugovk Apr 10, 2025
0799253
Remove some info
hugovk Apr 28, 2025
f703411
Merge branch 'main' into 3.14-deprecate-undotted-mimetypes
hugovk Apr 28, 2025
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/deprecations/pending-removal-in-3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ Pending removal in Python 3.16
* Calling the Python implementation of :func:`functools.reduce` with *function*
or *sequence* as keyword arguments has been deprecated since Python 3.14.

* :mod:`mimetypes`:

* Valid extensions start with a '.' or are empty for
:meth:`mimetypes.MimeTypes.add_type`.
Undotted extensions are deprecated and will
raise a :exc:`ValueError` in Python 3.16.
(Contributed by Hugo van Kemenade in :gh:`75223`.)

* :mod:`shutil`:

* The :class:`!ExecError` exception
Expand Down
7 changes: 6 additions & 1 deletion Doc/library/mimetypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,18 @@ than one MIME-type database; it provides an interface similar to the one of the

.. method:: MimeTypes.add_type(type, ext, strict=True)

Add a mapping from the MIME type *type* to the extension *ext*. When the
Add a mapping from the MIME type *type* to the extension *ext*.
Valid extensions start with a '.' or are empty. 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.

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

.. deprecated-removed:: 3.14 3.16
Invalid, undotted extensions will raise a
:exc:`ValueError` in Python 3.16.


.. _mimetypes-cli:

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,13 @@ Deprecated
and scheduled for removal in Python 3.16. Define handlers with the *stream*
argument instead. (Contributed by Mariusz Felisiak in :gh:`115032`.)

* :mod:`mimetypes`:
Valid extensions start with a '.' or are empty for
:meth:`mimetypes.MimeTypes.add_type`.
Undotted extensions are deprecated and will
raise a :exc:`ValueError` in Python 3.16.
(Contributed by Hugo van Kemenade in :gh:`75223`.)

* :mod:`!nturl2path`: This module is now deprecated. Call
:func:`urllib.request.url2pathname` and :func:`~urllib.request.pathname2url`
instead.
Expand Down
12 changes: 12 additions & 0 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,19 @@ def add_type(self, type, ext, strict=True):
If strict is true, information will be added to
list of standard types, else to the list of non-standard
types.

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),
)

if not type:
return
self.types_map[strict][ext] = type
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,22 @@ def test_keywords_args_api(self):
self.assertEqual(self.db.guess_extension(
type='image/jpg', strict=False), '.jpg')

def test_added_types_are_used(self):
mimetypes.add_type('testing/default-type', '')
mime_type, _ = mimetypes.guess_type('')
self.assertEqual(mime_type, 'testing/default-type')

mime_type, _ = mimetypes.guess_type('test.myext')
self.assertEqual(mime_type, None)

mimetypes.add_type('testing/type', '.myext')
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):
mimetypes.add_type("testing/type", "undotted")


@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
class Win32MimeTypesTestCase(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate undotted extensions in :meth:`mimetypes.MimeTypes.add_type`.
Patch by Hugo van Kemenade.
Loading