Skip to content

bpo-34926: Make mimetypes.guess_type accept os.PathLike objects - #9777

Merged
pitrou merged 11 commits into
python:masterfrom
mayankasthana:bpo-34926
Oct 10, 2018
Merged

pitrou merged 11 commits into
python:masterfrom
mayankasthana:bpo-34926

Conversation

@mayankasthana

@mayankasthana mayankasthana commented Oct 9, 2018 •

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

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!

Comment thread Lib/test/test_mimetypes.py Outdated

expected = self.db.guess_type(filename)

eq(self.db.guess_type(filepath), expected)

@tirkarthi tirkarthi Oct 9, 2018 •

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.

Please use self.assertEqual instead of using alias to be consistent with rest of the tests.

@tirkarthi

Copy link
Copy Markdown
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

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

Comment thread Lib/mimetypes.py Outdated
but non-standard types.
"""
url = url_or_path
if isinstance(url_or_path, os.PathLike):

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.

You probably don't need the conditional here, url = os.fspath(url) also works on strings.

Comment thread 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
Copy Markdown
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).

Comment thread Lib/test/test_mimetypes.py Outdated
import mimetypes
import sys
import unittest
import pathlib

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.

Can you please keep imports in alphebatical order?

@bedevere-bot

Copy link
Copy Markdown

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
Copy Markdown
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

pitrou commented Oct 9, 2018

Copy link
Copy Markdown
Member

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

@mayankasthana

Copy link
Copy Markdown
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
Copy Markdown

Thanks for making the requested changes!

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

@pitrou pitrou 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 for doing the changes. I have two more nits below.

Comment thread Doc/library/mimetypes.rst Outdated

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
Copy Markdown
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`.

Comment thread 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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown

Thanks for making the requested changes!

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

@pitrou

pitrou commented Oct 10, 2018

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor Author

Thanks @pitrou and @tirkarthi.

@tirkarthi

Copy link
Copy Markdown
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