Open
Description
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
- Intersection flattening (same as unions)
- fix some copy&paste errors - references to Union and UnionGenericAlias and some docstrings
- isinstance support
- Incorporate gh-105499: Merge typing.Union and types.UnionType python/cpython#105511 when it gets merged
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
Labels
No labels