typing icon indicating copy to clipboard operation
typing copied to clipboard

Making functions/methods subscriptable

Open Gobot1234 opened this issue 2 years ago • 0 comments

I'd like to make both instances and the types of functions and methods (including builtin methods) subscriptable.

Part of this proposal has been discussed already on python-ideas (https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463) but I thought it might also be worthwhile making FunctionType etc. subscriptable and work in a similar way to Callable just with extra descriptor behaviour that binds it into a MethodType.

class Foo:
	bar: FunctionType[[Self, int], str]
	# equivalent to 
	def bar(self, arg1, /) -> str: ...

This is different from Callable as if FunctionType was replaced with Callable, bar would always require a self argument.

This unfortunately probably requires special casing in type checkers as it isn't quite currently possible with PEP 612 (ParamSpec + Concatenate) (see #946) unless the PEP is updated

Gobot1234 avatar Jun 09 '23 09:06 Gobot1234