Repository navigation
bpo-34926: Make mimetypes.guess_type accept os.PathLike objects - #9777
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! |
|
|
||
| expected = self.db.guess_type(filename) | ||
|
|
||
| eq(self.db.guess_type(filepath), expected) |
There was a problem hiding this comment.
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 |
pitrou
left a comment
There was a problem hiding this comment.
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.
| but non-standard types. | ||
| """ | ||
| url = url_or_path | ||
| if isinstance(url_or_path, os.PathLike): |
There was a problem hiding this comment.
You probably don't need the conditional here, url = os.fspath(url) also works on strings.
| 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.
Can you keep "URL" uppercase? Also, this line seems a bit long (though Github doesn't display the line length).
| import mimetypes | ||
| import sys | ||
| import unittest | ||
| import pathlib |
There was a problem hiding this comment.
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. |
pitrou
left a comment
There was a problem hiding this comment.
Thanks for doing the changes. I have two more nits below.
|
|
||
| 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.
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`.
| 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.
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