Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPython implementation of intersections #9

Open
3 of 4 tasks
tomasr8 opened this issue Jul 25, 2023 · 8 comments
Open
3 of 4 tasks

CPython implementation of intersections #9

tomasr8 opened this issue Jul 25, 2023 · 8 comments

Comments

@tomasr8
Copy link
Collaborator

tomasr8 commented Jul 25, 2023

This is my stab at a reference implementation of the Intersection type in CPython:
https://github.com/tomasr8/cpython/tree/intersection-type
Contributions/improvements welcome!

Build instructions:

https://devguide.python.org/getting-started/setup-building/
TLDR:

git remote add tomasr8 https://github.com/tomasr8/cpython
git fetch tomasr8
git checkout --track tomasr8/intersection-type
make clean && make -s -j2
./python

TODOs

Example

from typing import Intersection

Intersection[int, str]
Intersection[int, float, int]  # == Intersection[int, float] (deduped)
Intersection[int, Intersection[float, str]]  # == Intersection[int, float, str] (flattened)
Intersection[int]  # == int

# overloaded __and__ for types
int & str
type(int & str)

class T: ...
T & int

class A: ...
class B: ...
class C(A, B): ...

a = A()
c = C()

isinstance(a, A & B)
isinstance(c, A & B)
@NeilGirdhar
Copy link
Collaborator

Why create the Intersection name at all only to deprecate it immediately? Union only exists because | wasn't available.

@CarliJoy
Copy link
Owner

CarliJoy commented Jul 28, 2023

Because we can backport Intersection with typing_extensions making it available also to older python versions.
The & will be only available with a newer Python version and nobody wants to way 2 Years for that...

Also some people will be forced to use older version for quite some time and we need backwards compatibility for them.

@NeilGirdhar
Copy link
Collaborator

Because we can backport Intersection with typing_extensions making it available also to older python versions.

Right, good point. BTW, you can still use & in annotations by using from __future__ import annotations, but I could see wanting to programmatically generate types.

@NeilGirdhar
Copy link
Collaborator

Also, Intersection should probably not be added to Python—just to typing-extensions.

@Gobot1234
Copy link

Also, Intersection should probably not be added to Python—just to typing-extensions.

Strongly disagree with this. typing_extensions is meant to be for backporting typing features, not having it in typing would create confusion.

@NeilGirdhar
Copy link
Collaborator

NeilGirdhar commented Jul 31, 2023

Strongly disagree with this. typing_extensions is meant to be for backporting typing features, not having it in typing would create confusion.

Good point. I guess as long as the linters push back, that's enough to discourage unnecessary use of it.

@JelleZijlstra
Copy link

Late to the party here, but note that there is precedent for adding something to typing that is immediately sort-of-deprecated: PEP 646 added typing.Unpack so that we could add typing_extensions.Unpack while also adding new syntax that made it unnecessary to ever use typing.Unpack. Analogously we could add a typing.Intersection while simultaneously adding a & operator that means you (almost) never have to use typing.Intersection directly.

@tomasr8
Copy link
Collaborator Author

tomasr8 commented Dec 14, 2023

Just an FYI, I rebased this on the latest CPython main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants