Skip to content
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

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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/deprecations/pending-removal-in-3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ Pending removal in Python 3.16
In the rare case that you need the bitwise inversion of
the underlying integer, convert to ``int`` explicitly (``~int(x)``).

* :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 @@ -299,9 +299,14 @@ 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.
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,13 @@ Deprecated
or *sequence* as keyword arguments is now deprecated.
(Contributed by Kirill Podoprigora in :gh:`121676`.)

* :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:`os`:
:term:`Soft deprecate <soft deprecated>` :func:`os.popen` and
:func:`os.spawn* <os.spawnl>` functions. They should no longer be used to
Expand Down
14 changes: 14 additions & 0 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,21 @@ 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 '.'.
The use of invalid extensions is deprecated and
will raise a ValueError in Python 3.16.
"""
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),
)

self.types_map[strict][ext] = type
exts = self.types_map_inv[strict].setdefault(type, [])
if ext not in exts:
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 @@ -325,6 +325,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