This is a prototype implementation of PEP 827 (Type Manipulation) in mypy.
The PEP introduces type-level computation operators for introspecting and constructing types.
Most of the main features are prototyped, and this should be suitable for experimentation, but it it not yet production quality or ready to be a PR yet. (This prototype has been AI assisted, and at least some slop has made it in that will need to be fixed; it might benefit from a full history rewrite, also.)
For the original mypy README, see REAL_README.md.
- Type operators:
IsAssignable,IsEquivalent,Bool,GetArg,GetArgs,GetMember,GetMemberType,Members,Attrs,FromUnion,Length,Slice,Concat,Uppercase,Lowercase,Capitalize,Uncapitalize,RaiseError - Conditional types:
true_type if BoolType else false_type - Type-level comprehensions:
*[T for x in Iter[...]]with filtering - Dot notation:
member.name,member.type,param.type, etc. - Boolean operators:
and,or,noton type booleans - Data types:
Member[name, type, quals, init, definer],Param[name, type, quals] - Extended callables:
Callable[Params[Param[...], ...], RetType] - Object construction:
NewProtocol[*Members],NewTypedDict[*Members] - Class modification:
UpdateClass[*Members]as return type of decorators /__init_subclass__ - InitField: Keyword argument capture with literal type inference
- Callable introspection:
GetArg[SomeCallable, Callable, Literal[0]]returnsParamtypes - Incremental mode
-
GetSpecialAttr[T, Attr]— extract__name__,__module__,__qualname__ -
GenericCallable[Vs, lambda <vs>: Ty]— generic callable types with lambda binding -
Overloaded[*Callables]— overloaded function type construction -
any(comprehension)/all(comprehension)— quantification over type booleans -
classmethod/staticmethodrepresentation in type-level computation -
Any attempt to make it perform well
-
Fine-grained mode is currently broken
mypy/typelevel.py— All type operator evaluation logicmypy/typeanal.py— Desugaring of conditional types, comprehensions, dot notation, extended callablesmypy/typeshed/stdlib/_typeshed/typemap.pyi— Stub declarations for all operators and data typestest-data/unit/check-typelevel-*.test— Test suite
- Evaluating
NewProtocolcreates a new anonymousTypeInfothat doesn't go into any symbol tables. ThatTypeInfohangs on to theNewProtocolinvocation that created it, and when we serializeInstances that refer to it, we serialize theNewProtocolinvocation instead. This allows us to avoid needing to serialize the anonymousTypeInfos.