Skip to content

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

Merged
merged 11 commits into from
Oct 10, 2018

Conversation

mayankasthana
Copy link
Contributor

@mayankasthana mayankasthana commented Oct 9, 2018

@mayankasthana mayankasthana requested a review from a team as a code owner October 9, 2018 18:22
@the-knights-who-say-ni
Copy link

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)
Copy link
Member

@tirkarthi tirkarthi Oct 9, 2018

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.

@tirkarthi
Copy link
Member

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 pitrou added the type-feature A feature request or enhancement label Oct 9, 2018
Copy link
Member

@pitrou pitrou left a 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):
Copy link
Member

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.
Copy link
Member

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

@@ -3,6 +3,7 @@
import mimetypes
import sys
import unittest
import pathlib
Copy link
Member

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?

@bedevere-bot
Copy link

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 have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@tirkarthi
Copy link
Member

Doc can be updated at https://docs.python.org/3/library/mimetypes.html#mimetypes.guess_type . I think Changed in version 3.8: Accepts a path-like object. can be used similar to doc at https://docs.python.org/3/library/gzip.html#gzip.GzipFile . I will let the reviewers to decide on the exact wording to be used.

@pitrou
Copy link
Member

pitrou commented Oct 9, 2018

@tirkarthi You're right, I forgot about the necessary doc changes.

@mayankasthana
Copy link
Contributor Author

I have made the requested changes; please review again.

Also, I have signed the CLA, might take a little for the status to update.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@pitrou: please review the changes made to this pull request.

Copy link
Member

@pitrou pitrou left a 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.


.. 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`).
Copy link
Member

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.
Copy link
Member

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.

@bedevere-bot
Copy link

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 have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@mayankasthana
Copy link
Contributor Author

I hadn't realized that I had changed the API. Changed it back. Also added a test when named arguments are used.
Also made the doc changes.

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@pitrou: please review the changes made to this pull request.

@pitrou
Copy link
Member

pitrou commented Oct 10, 2018

Thanks for polishing this! The PR is good to go now.

@pitrou pitrou merged commit 7e18dee into python:master Oct 10, 2018
@mayankasthana
Copy link
Contributor Author

Thanks @pitrou and @tirkarthi.

@tirkarthi
Copy link
Member

@mayankasthana You're welcome. Looking forward to more contributions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants