-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-34926: Make mimetypes.guess_type accept os.PathLike objects #9777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for your contribution, we look forward to reviewing it! |
Lib/test/test_mimetypes.py
Outdated
|
||
expected = self.db.guess_type(filename) | ||
|
||
eq(self.db.guess_type(filepath), expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use self.assertEqual
instead of using alias to be consistent with rest of the tests.
Thanks for the PR. Adding a NEWS entry helps since this is an enhancement. Please refer to https://devguide.python.org/committing/#what-s-new-and-news-entries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mayankasthana. As @tirkarthi said, you need to add a NEWS entry for this change. Also, you need to sign the Python CLA if you haven't already done so (see automated message above).
As for the PR itself, here a couple review comments.
Lib/mimetypes.py
Outdated
@@ -113,6 +113,10 @@ def guess_type(self, url, strict=True): | |||
Optional `strict' argument when False adds a bunch of commonly found, | |||
but non-standard types. | |||
""" | |||
url = url_or_path | |||
if isinstance(url_or_path, os.PathLike): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably don't need the conditional here, url = os.fspath(url)
also works on strings.
Lib/mimetypes.py
Outdated
def guess_type(self, url, strict=True): | ||
"""Guess the type of a file based on its URL. | ||
def guess_type(self, url_or_path, strict=True): | ||
"""Guess the type of a file which can either be a url or an os.PathLike object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you keep "URL" uppercase? Also, this line seems a bit long (though Github doesn't display the line length).
Lib/test/test_mimetypes.py
Outdated
@@ -3,6 +3,7 @@ | |||
import mimetypes | |||
import sys | |||
import unittest | |||
import pathlib |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please keep imports in alphebatical order?
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Doc can be updated at https://docs.python.org/3/library/mimetypes.html#mimetypes.guess_type . I think |
@tirkarthi You're right, I forgot about the necessary doc changes. |
I have made the requested changes; please review again. Also, I have signed the CLA, might take a little for the status to update. |
Thanks for making the requested changes! @pitrou: please review the changes made to this pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing the changes. I have two more nits below.
Doc/library/mimetypes.rst
Outdated
|
||
.. index:: pair: MIME; headers | ||
|
||
Guess the type of a file based on its filename or URL, given by *url*. The | ||
return value is a tuple ``(type, encoding)`` where *type* is ``None`` if the | ||
Guess the type of a file based on its filename/URL or by its path (any :class:`os.PathLike`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would phrase it this way:
Guess the type of a file based on its filename, path or URL, given by *url*.
*url* can be a string or a :term:`path-like object`.
Lib/mimetypes.py
Outdated
def guess_type(self, url, strict=True): | ||
"""Guess the type of a file based on its URL. | ||
def guess_type(self, url_or_path, strict=True): | ||
"""Guess the type of a file which is either a URL or an os.PathLike object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say "path-like object" rather than "os.PathLike object", since os.PathLike isn't something that users instantiate explicitly.
Also, I think the argument name can be kept as url
rather than url_or_path
. People may be passing the argument by name.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I hadn't realized that I had changed the API. Changed it back. Also added a test when named arguments are used. I have made the requested changes; please review again. |
Thanks for making the requested changes! @pitrou: please review the changes made to this pull request. |
Thanks for polishing this! The PR is good to go now. |
Thanks @pitrou and @tirkarthi. |
@mayankasthana You're welcome. Looking forward to more contributions :) |
https://bugs.python.org/issue34926