freenode
Languages & Toolchains

PEP 718 prototype lands as decorator questions stall it

Guido van Rossum shipped a mypy trial of subscriptable generic functions, but Callable typing and runtime wrappers still block consensus.

PEP 718 would let programmers subscript generic functions the way they already subscript generic classes, binding type parameters at the call site instead of relying only on inference. A working mypy prototype now exists, yet the proposal is still hung up on how that feature should interact with decorators, Callable types, and common runtime wrappers.

Guido van Rossum, who has been driving the draft, posted a feature-flagged mypy implementation with a substantial test suite. One early finding was that mypy currently strips unused type parameters, so a function that declares a type variable it never uses is treated as non-generic and cannot be indexed. The runtime keeps those parameters. Carl Meyer and van Rossum favor treating syntactically declared type variables as real, matching introspection and aligning functions with classes; the prototype was adjusted accordingly.

The harder problem is what type checkers should allow to be subscripted. If only known function objects qualify, most decorated callables become ineligible, because decorators typically erase down to a plain Callable type. If any Callable may be subscripted, programs that type-check can still crash at runtime: functools.partial, lru_cache, and many user-defined callable objects are not subscriptable. Meyer called that outcome a footgun, especially when a dependency change turns a bare function into a partial and a subscript added purely for static checking then fails in production.

Van Rossum said he would try a stricter checker mode that limits subscripting to statically known generic function objects. Michael Shardmind argued the opposite direction at runtime: give ordinary functions a getitem that returns a generic alias, and teach wraps, partial, and similar helpers to forward or synthesize that behavior so introspection and decoration stay consistent.

Type-parameter defaults were another gap. Without them, functions would still lag classes. Van Rossum proposed a short spec clarification: defaults apply on unsubscripted calls when inference binds nothing; a subscript binds every parameter and skips inference.

Until the Callable-versus-function-object rule is settled in the PEP text, and at least one type checker carries a reviewed implementation, 718 remains a prototype rather than a finished change to the typing spec.