Hi Guido,

I actually was playing around with how to implement this yesterday, and I put together the attached as an implementation. I realized that simply passing attributes through via `__getattr__` likely won't work, as class methods and variables won't be passed through correctly, so in you example, I believe dict_str_pair.from_keys(range(10)) will fail. That is why I created an ad-hoc subclass of the subscripted type, so that it will inherit all of that, and we can just override what we need. Perhaps there is a simpler solution to solve this problem that I am unaware of. I copied your repr since it was better than the one I threw together :)

I didn't do much error checking in __new__/__init__ because I mostly wanted to find a system that seemed robust enough and having something to show.


On Mon, Jan 27, 2020 at 8:27 AM Guido van Rossum <guido@python.org> wrote:
I'm beginning implementation work for PEP 585. I've got a little pure Python class that I'll use to guide me. (See attached proxy.py.) Then I'll start the work, roughly like this:

  •  Create a cpython fork (gvanrossum/pep585 where to do the work).
  •  Write a simple _Py_Class_GetItem(origin, parameters) function that just returns origin (after INCREF).
  •  Add that function to list, tuple and dict to show it works.
  •  Write some tests.
  •  Write a simple Proxy object to hold origin and parameters and return it from _Py_Class_GetItem.
  •  Update tests.
  •  Add __repr__ implementation and getattr forwarding functionality.
  •  Update tests.
  •  Deploy more widely to other builtin classes (set, frozenset, type).
  •  Update tests.
  •  Add specific methods that are supposed to fail (__class_getitem__, __instancecheck__, __subclasscheck__).
  •  Update tests.
At this point we're ready to start using the proxy class in typing.py, instead of the current implementation of _GenericAlias. We'll need to debate whether all the error checks we currently have in typing.py we *really* need to keep (usually these will also be detected by running a static type checker, so there's little harm in allowing nonsense like `list[1, 2, 3]`). Alternatively I'll need volunteers to implement those checks.

I expect the implementation work to result in proposed improvements to PEP 585. (I already have some -- I don't think `isinstance(x, list[int])` should work, and I don't think we should strive for `list[int] == list`.)

--Guido van Rossum (python.org/~guido)
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org