Skip to content

gh-151575: Deprecate passing file paths instead of URLs to mimetypes.guess_type - #162708

Open
sirosen wants to merge 10 commits into
python:mainfrom
sirosen:guess-mimetype-deprecate-paths
Open

sirosen wants to merge 10 commits into
python:mainfrom
sirosen:guess-mimetype-deprecate-paths

Conversation

@sirosen

@sirosen sirosen commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

closes #151576

This is a continuation of the aforementioned PR.

Deprecate passing file paths (path objects or any string which urllib.parse doesn't see as having a scheme) to guess_type, slating removal of support for 3.21.
The soft-deprecation period, which started in 3.13 is noted in text under the deprecated-removed directive.

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:

  • Move all of the deprecated usage tests to a new standalone test class
  • Add tests to confirm that file:// URLs do not trigger the warning

cc @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. 😁 )

NaveenKumarG-dev and others added 4 commits November 26, 2026 17:37
…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.
@read-the-docs-community

read-the-docs-community Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

@hugovk hugovk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Doc/library/mimetypes.rst Resolved
Comment thread Doc/library/mimetypes.rst Outdated
Comment on lines +268 to +271
.. 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this method soft deprecated, or just the function above?

Suggested change
.. 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sirosen sirosen Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Lib/mimetypes.py Resolved
Comment thread Lib/mimetypes.py Resolved
Comment thread Lib/mimetypes.py Resolved
sirosen and others added 3 commits November 27, 2026 13:24
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.
Comment thread Lib/mimetypes.py Resolved
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>

@hugovk hugovk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread Lib/mimetypes.py Outdated
"and will be removed in Python {remove}. "
"Use guess_file_type() instead.",
remove=(3, 21),
stacklevel=4,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(warnings._deprecated() doesn't have this parameter, but I could add it?)

Sounds like the best way to go.

@sirosen sirosen Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 stacklevel=3. Just let me know! (Posted before reloading for the confirmation above! 😂 )

`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()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants