Skip to content

Commit 1aba67d

Browse files
feat: add guess_file_type to mimetypes module to support path-based MIME lookups
1 parent 2c00715 commit 1aba67d

4 files changed

Lines changed: 5 additions & 93 deletions

File tree

Doc/library/mimetypes.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ the information :func:`init` sets up.
2828

2929
.. index:: pair: MIME; headers
3030

31-
Guess the type of a file based on its filename, path or URL, given by *url*.
32-
URL can be a string or a :term:`path-like object`.
31+
Guess the type of a file based on its URL, given by *url*.
32+
URL can be a string.
3333

3434
The return value is a tuple ``(type, encoding)`` where *type* is ``None`` if the
3535
type can't be guessed (missing or unknown suffix) or a string of the form
@@ -57,6 +57,7 @@ the information :func:`init` sets up.
5757
.. deprecated:: 3.16
5858
Passing a file path (or path-like object) instead of a URL.
5959
Use :func:`guess_file_type` instead.
60+
Soft-deprecated since Python 3.13, scheduled for removal in Python 3.21.
6061

6162

6263
.. function:: guess_file_type(path, *, strict=True)
@@ -265,6 +266,7 @@ than one MIME-type database; it provides an interface similar to the one of the
265266
.. deprecated:: 3.16
266267
Passing a file path (or path-like object) instead of a URL.
267268
Use :meth:`guess_file_type` instead.
269+
Soft-deprecated since Python 3.13, scheduled for removal in Python 3.21.
268270

269271
.. method:: MimeTypes.guess_file_type(path, *, strict=True)
270272

Lib/mimetypes.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ def add_type(self, type, ext, strict=True):
108108
def guess_type(self, url, strict=True):
109109
"""Guess the type of a file based on its URL.
110110
111-
.. deprecated:: 3.16
112-
Passing a file path (or path-like object) is deprecated.
113-
Use :meth:`guess_file_type` instead.
114-
115111
Return value is a tuple (type, encoding) where type is None if
116112
the type can't be guessed (no or unknown suffix) or a string
117113
of the form type/subtype, usable for a MIME Content-type
@@ -139,7 +135,6 @@ def guess_type(self, url, strict=True):
139135
scheme = p.scheme
140136
url = p.path
141137
else:
142-
# Input has no URL scheme — it is a file path, not a URL.
143138
warnings.warn(
144139
"Passing a file path to guess_type() is deprecated and will be "
145140
"removed in a future version. Use guess_file_type() instead.",

Lib/test/test_mimetypes.py

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -501,92 +501,7 @@ def test_keywords_args_api(self):
501501
type='image/jpeg', strict=True), ['.jpg', '.jpe', '.jpeg'])
502502

503503

504-
class GuessTypeDeprecationTestCase(unittest.TestCase):
505-
"""Tests that guess_type() emits DeprecationWarning for file path inputs."""
506504

507-
def setUp(self):
508-
self.db = mimetypes.MimeTypes()
509-
510-
# --- Module-level function tests ---
511-
512-
def test_module_plain_string_path_warns(self):
513-
"""Module-level guess_type() warns for a plain string with no URL scheme."""
514-
with self.assertWarns(DeprecationWarning) as cm:
515-
mimetypes.guess_type("file.txt")
516-
self.assertIn("guess_file_type", str(cm.warning))
517-
self.assertIn("deprecated", str(cm.warning).lower())
518-
519-
def test_module_pathlike_warns(self):
520-
"""Module-level guess_type() warns for a path-like object."""
521-
with self.assertWarns(DeprecationWarning):
522-
mimetypes.guess_type(pathlib.Path("file.txt"))
523-
524-
def test_module_bytes_path_warns(self):
525-
"""Module-level guess_type() warns for a bytes path."""
526-
with self.assertWarns(DeprecationWarning):
527-
mimetypes.guess_type(b"file.txt")
528-
529-
def test_module_url_with_scheme_no_warning(self):
530-
"""Module-level guess_type() does NOT warn for a proper URL."""
531-
with warnings.catch_warnings():
532-
warnings.simplefilter("error", DeprecationWarning)
533-
# Should not raise -- http:// is a valid multi-char scheme.
534-
mimetypes.guess_type("http://example.com/file.html")
535-
536-
def test_module_data_url_no_warning(self):
537-
"""Module-level guess_type() does NOT warn for data: URLs."""
538-
with warnings.catch_warnings():
539-
warnings.simplefilter("error", DeprecationWarning)
540-
mimetypes.guess_type("data:text/plain,hello")
541-
542-
# --- MimeTypes class method tests ---
543-
544-
def test_method_plain_string_path_warns(self):
545-
"""MimeTypes.guess_type() warns for a plain string with no URL scheme."""
546-
with self.assertWarns(DeprecationWarning) as cm:
547-
self.db.guess_type("file.html")
548-
self.assertIn("guess_file_type", str(cm.warning))
549-
550-
def test_method_pathlike_warns(self):
551-
"""MimeTypes.guess_type() warns for a path-like object."""
552-
with self.assertWarns(DeprecationWarning):
553-
self.db.guess_type(pathlib.Path("file.html"))
554-
555-
def test_method_bytes_path_warns(self):
556-
"""MimeTypes.guess_type() warns for a bytes path."""
557-
with self.assertWarns(DeprecationWarning):
558-
self.db.guess_type(b"file.html")
559-
560-
def test_method_url_with_scheme_no_warning(self):
561-
"""MimeTypes.guess_type() does NOT warn for a proper URL."""
562-
with warnings.catch_warnings():
563-
warnings.simplefilter("error", DeprecationWarning)
564-
self.db.guess_type("http://example.com/file.html")
565-
566-
def test_method_ftp_url_no_warning(self):
567-
"""MimeTypes.guess_type() does NOT warn for an ftp: URL."""
568-
with warnings.catch_warnings():
569-
warnings.simplefilter("error", DeprecationWarning)
570-
self.db.guess_type("ftp://example.com/file.tar.gz")
571-
572-
def test_result_unchanged(self):
573-
"""guess_type() with a file path still returns the correct MIME type."""
574-
with self.assertWarns(DeprecationWarning):
575-
result = mimetypes.guess_type("file.html")
576-
expected = mimetypes.guess_file_type("file.html")
577-
self.assertEqual(result, expected)
578-
579-
def test_result_unchanged_pathlike(self):
580-
"""guess_type() with a PathLike still returns the correct MIME type."""
581-
with self.assertWarns(DeprecationWarning):
582-
result = self.db.guess_type(pathlib.Path("file.tar.gz"))
583-
expected = self.db.guess_file_type(pathlib.Path("file.tar.gz"))
584-
self.assertEqual(result, expected)
585-
586-
def test_os_helper_fakepath_warns(self):
587-
"""guess_type() warns for os_helper.FakePath (a path-like object)."""
588-
with self.assertWarns(DeprecationWarning):
589-
self.db.guess_type(os_helper.FakePath("file.tar.gz"))
590505

591506

592507
@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Emit DeprecationWarning when a file path is passed to :func:`mimetypes.guess_type`. Use :func:`mimetypes.guess_file_type` instead.
1+
Deprecate passing a file path to :func:`mimetypes.guess_type`. Use :func:`mimetypes.guess_file_type` instead.

0 commit comments

Comments
 (0)