Skip to content

Commit e4aad49

Browse files
committed
Hoist os, posixpath and urllib.parse to module-level lazy imports
Per review: place the lazy imports at the top of the module with the other imports, above the platform try/except blocks, instead of inside the functions, and drop the now-redundant 'Lazy import to improve module import time' comments.
1 parent a05f1d1 commit e4aad49

1 file changed

Lines changed: 4 additions & 14 deletions

File tree

Lib/mimetypes.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
read_mime_types(file) -- parse one file, return a dictionary or None
2424
"""
2525

26+
lazy import os
27+
lazy import posixpath
28+
lazy import urllib.parse
29+
2630
try:
2731
from _winapi import _mimetypes_read_windows_registry
2832
except ImportError:
@@ -33,8 +37,6 @@
3337
except ImportError:
3438
_winreg = None
3539

36-
lazy import urllib.parse
37-
3840
__all__ = [
3941
"knownfiles", "inited", "MimeTypes",
4042
"guess_type", "guess_file_type", "guess_all_extensions", "guess_extension",
@@ -123,9 +125,6 @@ def guess_type(self, url, strict=True):
123125
Optional 'strict' argument when False adds a bunch of commonly found,
124126
but non-standard types.
125127
"""
126-
# Lazy import to improve module import time
127-
import os
128-
129128
# TODO: Deprecate accepting file paths (in particular path-like objects).
130129
url = os.fspath(url)
131130
# Without a ':' the argument cannot carry a URL scheme, so it cannot
@@ -159,19 +158,13 @@ def guess_type(self, url, strict=True):
159158
type = 'text/plain'
160159
return type, None # never compressed, so encoding is None
161160

162-
# Lazy import to improve module import time
163-
import posixpath
164-
165161
return self._guess_file_type(url, strict, posixpath.splitext)
166162

167163
def guess_file_type(self, path, *, strict=True):
168164
"""Guess the type of a file based on its path.
169165
170166
Similar to guess_type(), but takes file path instead of URL.
171167
"""
172-
# Lazy import to improve module import time
173-
import os
174-
175168
path = os.fsdecode(path)
176169
path = os.path.splitdrive(path)[1]
177170
return self._guess_file_type(path, strict, os.path.splitext)
@@ -418,9 +411,6 @@ def init(files=None):
418411
else:
419412
db = _db
420413

421-
# Lazy import to improve module import time
422-
import os
423-
424414
for file in files:
425415
if os.path.isfile(file):
426416
db.read(file)

0 commit comments

Comments
 (0)