Skip to content

Making functions/methods subscriptable #1410

Open
@Gobot1234

Description

@Gobot1234

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions