python / python/typing

Making functions/methods subscriptable

Open
#1,410 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked python-ideas discussion, then review PEP 612 and issue #946 to understand the stated limitations. The proposal involves subscriptable FunctionType and related types, descriptor binding, and likely type-checker special cases; done would require an agreed design and corresponding type-checker support.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.