Skip to content
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

bpo-9372: Deprecate several __getitem__ methods #8609

Merged
merged 8 commits into from
Aug 11, 2018

Conversation

berkerpeksag
Copy link
Member

@berkerpeksag berkerpeksag commented Aug 1, 2018

The __getitem__ methods of DOMEventStream, FileInput
and FileWrapper classes ignore their 'index'
parameters and return the next item instead.

https://bugs.python.org/issue9372

The __getitem__ methods of DOMEventStream, FileInput
and FileWrapper classes ignore their 'index'
parameters and return the next item instead.
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Please add an entry in What's New.

@@ -169,6 +169,9 @@ available for subclassing as well:
.. deprecated-removed:: 3.6 3.8
The *bufsize* parameter.

.. deprecated:: 3.8
Support for :meth:`sequence protocol <__getitem__>` is deprecated.
Copy link
Member

Choose a reason for hiding this comment

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

Actually the sequence protocol never was supported. It requires __len__ and __reverse__, and better support of __getitem__.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. Do you have a suggestion for wording? Would something like "Support for __getitem__ method is deprecated." be suitable?

Copy link
Member

Choose a reason for hiding this comment

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

This LGTM.

r'Use iterator protocol instead'):
with FileInput(files=[t]) as fi:
with self.assertRaises(RuntimeError):
fi[1]
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to use fi[0] which works now and returns "line1\n".

Copy link
Member Author

Choose a reason for hiding this comment

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

I used fi[1] on purpose to show that __getitem__ doesn't work as expected (e.g. it would return 'line2\n' if it does)

Copy link
Member

Choose a reason for hiding this comment

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

This is tested in other test (test__getitem__invalid_key). We need to test that even using a "valid" key is deprecated.

@@ -138,6 +139,22 @@ def _ignore_deprecated_imports(ignore=True):
yield


def ignore_warnings(*, category):
Copy link
Member

Choose a reason for hiding this comment

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

Why it is a keyword-only parameter? For a single argument I expect a positional parameter.

Copy link
Member Author

Choose a reason for hiding this comment

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

There are many warning-related APIs both in the stdlib and test.support and it's easy to forget how to use them, so I just tried to make the API more strict, easy to remember, and avoid misusing of the function.

Lib/fileinput.py Outdated
@@ -259,6 +259,13 @@ def __next__(self):
# repeat with next file

def __getitem__(self, i):
import warnings
warnings.warn(
"FileInput's __getitem__ method is deprecated. "
Copy link
Member

Choose a reason for hiding this comment

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

The end user likely uses indexing instead of direct calling of __getitem__. What about "Indexing FileInput is deprecated."?

Copy link
Member Author

Choose a reason for hiding this comment

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

How about "Indexing of a FileInput object is deprecated." or "Support for indexing a FileInput object is deprecated."

@vadmium do you have a suggestion on wording of the deprecation message?

@berkerpeksag
Copy link
Member Author

I've addressed all of your review comments.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM, but @vadmium may suggest better wording.

@berkerpeksag berkerpeksag merged commit 84a13fb into python:master Aug 11, 2018
@berkerpeksag berkerpeksag deleted the 9372-getitem-deprecation branch August 11, 2018 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants