CarliJoy / intersection_examples

Python Typing Intersection examples
MIT License
21 stars 2 forks source link

Intersection: The Ordered Layers Formulation #48

Open mark-todd opened 2 months ago

mark-todd commented 2 months ago

Summary

I'm starting this thread as a fresh start on the Intersection PEP. We've come across various difficulties in formulating the Intersection PEP, and it's been a long road of discussion - I'm keen for all that hard work not to go to waste.

What's the idea?

This PEP will build a lot on the ideas of the "Ordered Intersection" that's been discussed a lot over the past few weeks, but conceptually is perhaps slightly different. I'm hoping that perhaps these differences will offer solutions to some of the issues we've been facing. The main concepts:

  1. Intersection can be thought of a series of structural layers - Intersection[A,B] is A layered on top of B. There will be more details on exactly what I mean by layering to follow.
  2. For all non-structural types, the intersection must be an instance of all intersection components. They must appear in the same order in the intersection as in the MRO.

What isn't part of this PEP

Other considerations

What is layering?

The concept of layering is inspired by the way a lot of photo editing software works. Layers are built up - where features overlap the highest priority layer appears in the final image. In this analogy each class/type is effectively a layer, and the stack of layers is the intersection. A layer does not need to a concrete class and can be a protocol. It only represents the exposed method and attribute types of the class/protocol.

This might be quite controversial, but it seems to me so far that the two concepts at the core of the idea deal with a lot of the discussed edge cases. The PEP will put quite a lot of emphasis on the "How to teach" section as these concepts will be at the very heart of the idea.