Repository navigation
Conversation
…r file paths feat: introduce MimeTypes class and deprecate file path support in guess_type with new guess_file_type method feat: add guess_file_type to mimetypes module to support path-based MIME lookups test: add test suite for mimetypes module Address PR feedback: fix mimetypes CLI, lazy imports, and doc deprecations
os, sys, and posixpath are loaded on interpreter start -- making them lazy has no meaningful effect, so they are made into plain imports. urllib.parse, warnings, and argparse.ArgumentParser are made into lazy imports. Inline imports are removed.
Per discussion and feedback, passing a Path is noted in text (not with a directive) as soft deprecated, and deprecated-removed for the 3.16 - 3.21 timeline.
Documentation build overview
38 files changed ·
|
hugovk
left a comment
There was a problem hiding this comment.
Are we satisfied with the test changes? I considered the following additional changes but decided to wait for feedback:
- Move all of the deprecated usage tests to a new standalone test class
Could do, but less churn to leave them there.
- Add tests to confirm that
file://URLs do not trigger the warning
Yes please.
| .. deprecated-removed:: 3.16 3.21 | ||
| Passing a file path (or path-like object) instead of a URL. | ||
| Use :func:`guess_file_type` instead. | ||
| Soft deprecated since version 3.13. |
There was a problem hiding this comment.
Was this method soft deprecated, or just the function above?
| .. deprecated-removed:: 3.16 3.21 | |
| Passing a file path (or path-like object) instead of a URL. | |
| Use :func:`guess_file_type` instead. | |
| Soft deprecated since version 3.13. | |
| .. deprecated-removed:: 3.16 3.21 | |
| Passing a file path (or path-like object) instead of a URL. | |
| Use :func:`guess_file_type` instead. |
There was a problem hiding this comment.
| .. deprecated-removed:: 3.16 3.21 | |
| Passing a file path (or path-like object) instead of a URL. | |
| Use :func:`guess_file_type` instead. | |
| Soft deprecated since version 3.13. | |
| .. deprecated-removed:: 3.16 3.21 | |
| Passing a file path (or path-like object) instead of a URL. | |
| Use :func:`guess_file_type` instead. | |
| This has been soft deprecated since version 3.13. |
My suggestion.
There was a problem hiding this comment.
Was this method soft deprecated, or just the function above?
It was not marked as such... but that doesn't make any sense to me? I think it must have been a documentation oversight. Does that mean that it wasn't soft-deprecated? The intent was almost certainly to do so.
(All of this originates from #66543 , for context)
I'm going to update both to match Stan's suggestion ("This has been ...") -- I don't feel strongly and I trust both of your judgement ahead of my own on this. Although the method was missed in the initial PR, I think it makes the most sense to document both in this way.
There was a problem hiding this comment.
I've applied this, so I'm going to mark this resolved. I feel comfortable calling both soft deprecated back in 3.13. It's a mild retcon, I guess. 😅
EDIT: this started to bother me from the moment I marked it resolved. It makes it harder for a reviewer to read. Came back to mark it unresolved.
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Add two checks for file URLs, and remove a vestigial line from a nearby test.
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
| "and will be removed in Python {remove}. " | ||
| "Use guess_file_type() instead.", | ||
| remove=(3, 21), | ||
| stacklevel=4, |
There was a problem hiding this comment.
This is off-by-one for callees of MimeTypes().guess_type():
>>> import mimetypes
>>> mimetypes.guess_type("a.txt")
<python-input-1>:1: DeprecationWarning: Passing a file path to guess_type() is deprecated and will be removed in Python 3.21. Use guess_file_type() instead.
('text/plain', None)
>>> mimetypes.MimeTypes().guess_type("a.txt")
/home/stan/dev/cpython/Lib/_pyrepl/console.py:231: DeprecationWarning: Passing a file path to guess_type() is deprecated and will be removed in Python 3.21. Use guess_file_type() instead.
exec(code, self.locals)
('text/plain', None)
There was a problem hiding this comment.
Oh, good call! I don't find simply changing to stacklevel=3 very satisfactory though.
Can I use skip_file_prefixes here to eliminate mimetypes.py as the source of these warnings? (warnings._deprecated() doesn't have this parameter, but I could add it?)
There was a problem hiding this comment.
(warnings._deprecated() doesn't have this parameter, but I could add it?)
Sounds like the best way to go.
There was a problem hiding this comment.
By adding skip_file_prefixes support to _deprecated() and applying this change:
stacklevel=1,
skip_file_prefixes=(__file__,)I get what I think is the best possible output:
>>> import mimetypes
>>> mimetypes.guess_type("a.txt")
<python-input-1>:1: DeprecationWarning: Passing a file path to guess_type() is deprecated and will be removed in Python 3.21. Use guess_file_type() instead.
('text/plain', None)
>>> mimetypes.MimeTypes().guess_type("a.txt")
<python-input-2>:1: DeprecationWarning: Passing a file path to guess_type() is deprecated and will be removed in Python 3.21. Use guess_file_type() instead.
('text/plain', None)
I went ahead with that change, but I understand if you prefer that I revert it and switch to (Posted before reloading for the confirmation above! 😂 )stacklevel=3. Just let me know!
`mimetypes.guess_type()` wraps `mimetypes.MimeType.guess_type()`, and we want an appropriate warning emitted for both callpaths. However, the stack depth is different for these two cases. In order to get appropriate warnings, pass `skip_file_prefixes` to skip the `mimetypes` module and `stacklevel=1` to force the warning to be emitted at the level of the caller into `mimetypes`. This requires `warnings._deprecated` to gain support for `skip_file_prefixes` as a passthrough argument for `warn()`.
closes #151576
This is a continuation of the aforementioned PR.
Deprecate passing file paths (path objects or any string which
urllib.parsedoesn't see as having a scheme) toguess_type, slating removal of support for 3.21.The soft-deprecation period, which started in 3.13 is noted in text under the
deprecated-removeddirective.As part of this work, lazy imports are converted to the 3.15+ syntax and moved to the top of the file. And lazy imports which were not meaningfully lazy (
sys,os,posixpath) were made non-lazy.Are we satisfied with the test changes? I considered the following additional changes but decided to wait for feedback:
file://URLs do not trigger the warningcc @StanFromIreland, @hugovk, I reviewed the original PR, and recall making a bit of a mistake with the way the deprecation should be stated. I'd especially appreciate your eyes on this one.
(Aside: I want to work through the deprecation process because there's an outstanding TODO I have on my list to go back to the devguide and document it. 😁 )
mimetypes.guess_type()#151575