Skip to content

bpo-9372: Deprecate several __getitem__ methods#8609

Merged
berkerpeksag merged 8 commits into
python:masterfrom
berkerpeksag:9372-getitem-deprecation
Aug 11, 2018
Merged

bpo-9372: Deprecate several __getitem__ methods#8609
berkerpeksag merged 8 commits into
python:masterfrom
berkerpeksag:9372-getitem-deprecation

Conversation

@berkerpeksag

@berkerpeksag berkerpeksag commented Aug 1, 2018

Copy link
Copy Markdown
Member

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.

@serhiy-storchaka serhiy-storchaka 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.

Please add an entry in What's New.

Comment thread Doc/library/fileinput.rst Outdated
The *bufsize* parameter.

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

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.

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

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

Choose a reason for hiding this comment

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

This LGTM.

Comment thread Lib/test/test_fileinput.py Outdated
r'Use iterator protocol instead'):
with FileInput(files=[t]) as fi:
with self.assertRaises(RuntimeError):
fi[1]

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 suggest to use fi[0] which works now and returns "line1\n".

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

yield


def ignore_warnings(*, category):

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.

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

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

Comment thread Lib/fileinput.py Outdated
def __getitem__(self, i):
import warnings
warnings.warn(
"FileInput's __getitem__ method is deprecated. "

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.

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

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

I've addressed all of your review comments.

@serhiy-storchaka serhiy-storchaka 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.

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