8000 gh-75223: Deprecate undotted extensions in `mimetypes.MimeTypes.add_t… · python/cpython@ee9102a · GitHub
[go: up one dir, main page]

Skip to content

Commit ee9102a

Browse files
hugovkencukouOddBlokearhadthedev
authored
gh-75223: Deprecate undotted extensions in mimetypes.MimeTypes.add_type (#128638)
Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Daniel Watkins <daniel@daniel-watkins.co.uk> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
1 parent fe8ad53 commit ee9102a

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

Doc/deprecations/pending-removal-in-3.16.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ Pending removal in Python 3.16
6161
* Calling the Python implementation of :func:`functools.reduce` with *function*
6262
or *sequence* as keyword arguments has been deprecated since Python 3.14.
6363

64+
* :mod:`mimetypes`:
65+
66+
* Valid extensions start with a '.' or are empty for
67+
:meth:`mimetypes.MimeTypes.add_type`.
68+
Undotted extensions are deprecated and will
69+
raise a :exc:`ValueError` in Python 3.16.
70+
(Contributed by Hugo van Kemenade in :gh:`75223`.)
71+
6472
* :mod:`shutil`:
6573

6674
* The :class:`!ExecError` exception

Doc/library/mimetypes.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,18 @@ than one MIME-type database; it provides an interface similar to the one of the
301301

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

304-
Add a mapping from the MIME type *type* to the extension *ext*. When the
304+
Add a mapping from the MIME type *type* to the extension *ext*.
305+
Valid extensions start with a '.' or are empty. When the
305306
extension is already known, the new type will replace the old one. When the type
306307
is already known the extension will be added to the list of known extensions.
307308

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

312+
.. deprecated-removed:: 3.14 3.16
313+
Invalid, undotted extensions will raise a
314+
:exc:`ValueError` in Python 3.16.
315+
311316

312317
.. _mimetypes-cli:
313318

Doc/whatsnew/3.14.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,13 @@ Deprecated
15891589
and scheduled for removal in Python 3.16. Define handlers with the *stream*
15901590
argument instead. (Contributed by Mariusz Felisiak in :gh:`115032`.)
15911591

1592+
* :mod:`mimetypes`:
1593+
Valid extensions start with a '.' or are empty for
1594+
:meth:`mimetypes.MimeTypes.add_type`.
1595+
Undotted extensions are deprecated and will
1596+
raise a :exc:`ValueError` in Python 3.16.
1597+
(Contributed by Hugo van Kemenade in :gh:`75223`.)
1598+
15921599
* :mod:`!nturl2path`: This module is now deprecated. Call
15931600
:func:`urllib.request.url2pathname` and :func:`~urllib.request.pathname2url`
15941601
instead.

Lib/mimetypes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,19 @@ def add_type(self, type, ext, strict=True):
8989
If strict is true, information will be added to
9090
list of standard types, else to the list of non-standard
9191
types.
92+
93+
Valid extensions are empty or start with a '.'.
9294
"""
95+
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+
)
104+
93105
if not type:
94106
return
95107
self.types_map[strict][ext] = type

Lib/test/test_mimetypes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,22 @@ def test_keywords_args_api(self):
363363
self.assertEqual(self.db.guess_extension(
364364
type='image/jpg', strict=False), '.jpg')
365365

366+
def test_added_types_are_used(self):
367+
mimetypes.add_type('testing/default-type', '')
368+
mime_type, _ = mimetypes.guess_type('')
369+
self.assertEqual(mime_type, 'testing/default-type')
370+
371+
mime_type, _ = mimetypes.guess_type('test.myext')
372+
self.assertEqual(mime_type, None)
373+
374+
mimetypes.add_type('testing/type', '.myext')
375+
mime_type, _ = mimetypes.guess_type('test.myext')
376+
self.assertEqual(mime_type, 'testing/type')
377+
378+
def test_add_type_with_undotted_extension_deprecated(self):
379+
with self.assertWarns(DeprecationWarning):
380+
mimetypes.add_type("testing/type", "undotted")
381+
366382

367383
@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
368384
class Win32MimeTypesTestCase(unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate undotted extensions in :meth:`mimetypes.MimeTypes.add_type`.
2+
Patch by Hugo van Kemenade.

0 commit comments

Comments
 (0)
0