Skip to content

CPython implementation of intersections #9

Open
@tomasr8

Description

@tomasr8

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions