This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kj
Recipients kj
Date 2020-10-12.07:46:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1602488772.63.0.453170836227.issue42010@roundup.psfhosted.org>
In-reply-to
Content
While writing the docs for PEP 585's GenericAlias, I noticed that the following anti-pattern described in PEP 585 no longer throws an error when it should:

>>> l = list
>>> l[-1]

Whereas in versions of Python before 3.9, a "TypeError: 'type' object is not subscriptable" would have been thrown.

Quoting PEP 585 again: "Say, if a user is mistakenly passing a list type instead of a list object to a function, and that function is indexing the received object, the code would no longer raise an error."

Although the context of that statement isn't the same. I fully agree with its reasoning. This makes Python more confusing for beginners coming from other languages too:

// in c, makes a 10 element array 
int l[10];

# now in python, this is fine
l[10] 

This may give beginners the false impression that an empty 10 element list has been created.

I have created a PR and a test for this. The changed code blocks ints, floats, bools and complex from indexing for type objects. str is allowed since list['mytype'] seems analogous to typing.List[ForwardRef('mytype')]. Also, imo beginners are unlikely to accidentally write that code while intending to index a list.

Feedback is greatly appreciated, as I've not touched the C codebase in cpython before. Thanks!
History
Date User Action Args
2020-10-12 07:46:12kjsetrecipients: + kj
2020-10-12 07:46:12kjsetmessageid: <1602488772.63.0.453170836227.issue42010@roundup.psfhosted.org>
2020-10-12 07:46:12kjlinkissue42010 messages
2020-10-12 07:46:12kjcreate