New submission from Ken Jin <kenjin4...@gmail.com>:
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! ---------- components: Interpreter Core messages: 378475 nosy: kj priority: normal severity: normal status: open title: GenericAlias accepts indexing/subscripting, causing confusion type: behavior versions: Python 3.10, Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue42010> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com