>  I don't know how to interpret these examples. What's Protocol and where
does it come from? What's Struct? 

`Protocol` comes from `typing`

`Struct` is my own class which generates anonymous dataclasses and protocols as you gathered (unfortunately I currently have two versions one for building the protocol and one for building the dataclass but thats because of stupid engineering requirements). 



On Fri, Aug 14, 2020 at 11:14 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Fri, Aug 14, 2020 at 04:07:33PM -0700, Caleb Donovick 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
> typesassert T == Struct[x=int, y=str]

I don't know how to interpret these examples. What's Protocol and where
does it come from? What's Struct?

As I recall, one of the motivations for re-visiting PEP 472 is to allow
such keyword notation in type hints, so that we could write

    Struct[x=int, y=str]

in a type hint and have it mean a struct with fields x (an int) and y (a
str). I'm not sure whether that use in type hinting would allow the use
of this Struct to create anonymous classes. I suppose it would, but I'm
not expert enough on type hints to be sure.

But assuming the two uses are compatible, I must say that having the
same notation for type-hinting a struct and actually creating an
anonymous struct class would be desirable:

    def func(widget:Struct[x=int, y=str]) -> gadget:
        pass

    # Later:
    MyWidget = Struct[x=int, y=str]

    func(MyWidget(19, 'hello'))

I really like the look of that, and I think that having the Struct call
use the same subscript notation as the Struct type hint is a plus.


> 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.

This is one of the motivating use-cases of PEP 472.



--
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/HLXSO2KSOB62Z4RI7SA52DHWGCGECATC/
Code of Conduct: http://python.org/psf/codeofconduct/