On Fri, Aug 14, 2020 at 4:38 PM Caleb Donovick <donovick@cs.stanford.edu> wrote:

My own personal use for this would be for generating anonymous protocols and dataclasses:

class T(Protocol):
    x: int
    y: str

# with some abuse of notation obviously these would generate unique types
assert T == Struct[x=int, y=str]

# similarly 
@dataclass
class S:
   x: int
   y: str

assert S == Struct[x=int, y=str]

I often want to create such types “on the fly” without needing to put a name on them.

Now as I don’t need mixed keyword / positional arguments I can achieve this with:

# K = dict
Struct[K(x=int, y=str)]

But that costs 3 more keystrokes and is certainly less beautiful.

While I would not personally use this I think a real killer app would be slicing named axis, as the slice syntax is exclusive to geitem and hence can not leverage the dict trick.

To me, the main weakness here is that you couldn't move forward with this unless you also got the various static type checkers on board. But I don't think those care much about this use case (an inline notation for what you can already do with a class definition and annotations). And without static checking this isn't going to be very popular.

If and when we have `__getitem__` with keyword args we can start thinking about how to best leverage it in type annotations -- I would assume that describing axes of objects like numpy arrays would be the first use case.

--
--Guido van Rossum (python.org/~guido)