https://github.com/python/cpython/commit/ee9102a53565f694c37ca1bcc6bb1239db7207d9 commit: ee9102a53565f694c37ca1bcc6bb1239db7207d9 branch: main author: Hugo van Kemenade <1324225+hug...@users.noreply.github.com> committer: hugovk <1324225+hug...@users.noreply.github.com> date: 2025-04-28T14:23:57Z summary:
gh-75223: Deprecate undotted extensions in `mimetypes.MimeTypes.add_type` (#128638) Co-authored-by: Petr Viktorin <encu...@gmail.com> Co-authored-by: Daniel Watkins <dan...@daniel-watkins.co.uk> Co-authored-by: Oleg Iarygin <o...@arhadthedev.net> files: A Misc/NEWS.d/next/Library/2019-09-10-09-28-52.gh-issue-75223.VyAJS9.rst M Doc/deprecations/pending-removal-in-3.16.rst M Doc/library/mimetypes.rst M Doc/whatsnew/3.14.rst M Lib/mimetypes.py M Lib/test/test_mimetypes.py diff --git a/Doc/deprecations/pending-removal-in-3.16.rst b/Doc/deprecations/pending-removal-in-3.16.rst index acb53198bdc07e..90183f1ff233c1 100644 --- a/Doc/deprecations/pending-removal-in-3.16.rst +++ b/Doc/deprecations/pending-removal-in-3.16.rst @@ -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 diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index 5af69455032d71..13511b16a0ed8c 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -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: diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 95a4d0d5822362..82ef636778183b 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -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. diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index de842eabea8093..e93e1d5a03a454 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -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 diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index f2a19c0863570a..b9197069a08a88 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -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): diff --git a/Misc/NEWS.d/next/Library/2019-09-10-09-28-52.gh-issue-75223.VyAJS9.rst b/Misc/NEWS.d/next/Library/2019-09-10-09-28-52.gh-issue-75223.VyAJS9.rst new file mode 100644 index 00000000000000..d3c8d1b747e5c9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-09-10-09-28-52.gh-issue-75223.VyAJS9.rst @@ -0,0 +1,2 @@ +Deprecate undotted extensions in :meth:`mimetypes.MimeTypes.add_type`. +Patch by Hugo van Kemenade. _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com