On Tue, Sep 1, 2020 at 9:51 AM Guido van Rossum <guido@python.org> wrote:
On Tue, Sep 1, 2020 at 8:20 AM Christopher Barker <pythonchb@gmail.com> wrote:
Question: it strikes me that the use case of [] for type hints is logically quite different than for indexing. So do they need to use the same syntax / dunder?

I think that type hints are used in specific places, and thus the interpreter could know if a given [] was a type hint or an indexing operation, and thus could dispatch it differently.
If it's not technically possible to make the distinction, then it's a non-issue, but if it is, it may be worth considering.

Type hints are indeed dispatched differently, but this is done based on information that is only available at runtime. Since PEP 560, for `x[y]`, if no `__getitem__` method is found, and `x` is a type (class) object, and `x` has a class method `__class_getitem__`, that method is called. Extending this with keyword args is straightforward. Modifying the compiler to generate different bytecode for this case is essentially impossible.

Thanks -- then that answers my question -- no it's not technically possible. Oh well.


Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython