Skip to content

Commit 4b8515d

Browse files
Address PR feedback: fix mimetypes CLI, lazy imports, and doc deprecations
1 parent ec6e1da commit 4b8515d

6 files changed

Lines changed: 14 additions & 13 deletions

File tree

‎Doc/library/mimetypes.rst‎

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

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

31-
Guess the type of a file based on its URL, given by *url*.
32-
URL can be a string.
31+
Guess the type of a file based on its URL, given as a string.
3332

3433
The return value is a tuple ``(type, encoding)`` where *type* is ``None`` if the
3534
type can't be guessed (missing or unknown suffix) or a string of the form
@@ -54,10 +53,9 @@ the information :func:`init` sets up.
5453
.. versionchanged:: 3.8
5554
Added support for *url* being a :term:`path-like object`.
5655

57-
.. deprecated:: 3.16
56+
.. deprecated-removed:: 3.16 3.21
5857
Passing a file path (or path-like object) instead of a URL.
5958
Use :func:`guess_file_type` instead.
60-
Soft-deprecated since Python 3.13, scheduled for removal in Python 3.21.
6159

6260

6361
.. function:: guess_file_type(path, *, strict=True)
@@ -263,10 +261,9 @@ than one MIME-type database; it provides an interface similar to the one of the
263261
Similar to the :func:`guess_type` function, using the tables stored as part of
264262
the object.
265263

266-
.. deprecated:: 3.16
264+
.. deprecated-removed:: 3.16 3.21
267265
Passing a file path (or path-like object) instead of a URL.
268-
Use :meth:`guess_file_type` instead.
269-
Soft-deprecated since Python 3.13, scheduled for removal in Python 3.21.
266+
Use :func:`guess_file_type` instead.
270267

271268
.. method:: MimeTypes.guess_file_type(path, *, strict=True)
272269

‎Doc/whatsnew/3.16.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ New deprecations
581581
:func:`mimetypes.guess_type` is now deprecated.
582582
Use :func:`mimetypes.guess_file_type` instead.
583583
(Contributed by Naveen Kumar G in :gh:`151575`.)
584-
584+
585585
* :mod:`struct`:
586586

587587
* Soft-deprecated since Python 3.15, using ``'F'`` and ``'D'`` type codes are now

‎Lib/mimetypes.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ def guess_type(self, url, strict=True):
127127
# Lazy import to improve module import time
128128
import os
129129
import urllib.parse
130-
import warnings
131130

132131
url = os.fspath(url)
133132
p = urllib.parse.urlparse(url)
134133
if p.scheme and len(p.scheme) > 1:
135134
scheme = p.scheme
136135
url = p.path
137136
else:
137+
import warnings
138138
warnings.warn(
139139
"Passing a file path to guess_type() is deprecated and will be "
140140
"removed in a future version. Use guess_file_type() instead.",
@@ -758,8 +758,13 @@ def _main(args=None):
758758
results.append(f"error: unknown type {gtype}")
759759
return results
760760
else:
761+
import urllib.parse
761762
for gtype in args.type:
762-
guess, encoding = guess_file_type(gtype, strict=not args.lenient)
763+
p = urllib.parse.urlparse(gtype)
764+
if p.scheme and len(p.scheme) > 1:
765+
guess, encoding = guess_type(gtype, strict=not args.lenient)
766+
else:
767+
guess, encoding = guess_file_type(gtype, strict=not args.lenient)
763768
if guess:
764769
results.append(f"type: {guess} encoding: {encoding}")
765770
else:

‎Lib/test/test_mimetypes.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import io
22
import mimetypes
33
import os
4-
54
import shlex
65
import sys
76
import unittest

‎Misc/mypy/_colorize.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../Lib/_colorize.py
1+
../../Lib/_colorize.py

‎Misc/mypy/token.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../Lib/token.py
1+
../../Lib/token.py

0 commit comments

Comments
 (0)