Title: Implement PEP 560: Core support for typing module and generic types
Type: enhancement Stage: patch review
Components: Interpreter Core, Library (Lib) Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: levkivskyi Nosy List: gvanrossum, levkivskyi, serhiy.storchaka, xgdomingo
Priority: normal Keywords: patch

Created on 2017-12-05 20:36 by levkivskyi, last changed 2017-12-14 12:22 by serhiy.storchaka.

Pull Requests
URL Status Linked Edit
PR 4732 open levkivskyi, 2017-12-05 20:48
Messages (7)
msg307682 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2017-12-05 20:38
As discussed before, there will be two PRs. One for the core components, and the second one (large) for typing updates. I will open the first PR shortly.
msg308092 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-12 08:21
As for __class_getitem__, why not implement type.__getitem__ instead of hacking PyObject_GetItem()?
msg308101 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-12 09:49
For reference: PEP 560.
msg308121 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2017-12-12 12:23
> As for __class_getitem__, why not implement type.__getitem__ instead of hacking PyObject_GetItem()?

This question was raised by Mark Shannon on python-dev. Actually, my initial implementation did exactly this, but I didn't like it for two reasons:

1. dir(type) is already very long, I don't want to make it even longer.
2. Some users may be surprised that although ``type.__getitem__`` is defined, ``int[something]`` will rise "TypeError: type object is not subscriptable"

Mark however disappeared since then, so I don't know what is his current opinion. I am however not as sure about this now. If there are some arguments for this, then I can revert to the older approach (implementing ``type.__getitem__``).
msg308151 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2017-12-12 21:10
Guido, what is your preference for the implementation of ``__class_getitem__``: ``PyObject_GetItem`` (current one) or ``type.__getitem__`` (old one)? Can we just go ahead with the version you like and then re-consider if some objections will appear? I am asking because you are going on vacation soon and there is a second part of implementation -- changes in typing (and someone needs to review them :-)
msg308171 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2017-12-12 23:37
I like the current approach (PyObject_GetItem).  I agree with both of your reasons to prefer it.
msg308291 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-14 12:22
This adds an overhead to every indexing.

$ ./python -m perf timeit --compare-to=./python0 -s 'a = [1]' --duplicate=10000 'a[0]'
python0: ..................... 15.3 ns +- 0.5 ns
python: ..................... 16.1 ns +- 0.3 ns

Mean +- std dev: [python0] 15.3 ns +- 0.5 ns -> [python] 16.1 ns +- 0.3 ns: 1.05x slower (+5%)

$ ./python -m perf timeit --compare-to=./python0 -s 'd = {1: 2}' --duplicate=10000 'd[1]'
python0: ..................... 17.6 ns +- 0.6 ns
python: ..................... 18.4 ns +- 0.5 ns

Mean +- std dev: [python0] 17.6 ns +- 0.6 ns -> [python] 18.4 ns +- 0.5 ns: 1.05x slower (+5%)
Date User Action Args
2017-12-14 12:22:24serhiy.storchakasetmessages: + msg308291
2017-12-13 03:57:22xgdomingosetnosy: + xgdomingo
2017-12-12 23:37:15gvanrossumsetmessages: + msg308171
2017-12-12 21:10:56levkivskyisetmessages: + msg308151
2017-12-12 12:23:20levkivskyisetmessages: + msg308121
2017-12-12 09:49:21serhiy.storchakasetmessages: + msg308101
2017-12-12 08:21:54serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg308092
2017-12-05 20:48:27levkivskyisetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request4635
2017-12-05 20:38:12levkivskyisetnosy: + gvanrossum
messages: + msg307682
2017-12-05 20:36:48levkivskyicreate