# Making functions subscriptable at runtime

**URL:** <https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463>\
**Category:** Ideas\
**Tags:** typing, builtins\
**Created:** [May 3, 2023, 8:21pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463 "2023-05-03T20:21:40Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [May 18, 2023, 6:10pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/21 "2023-05-18T18:10:23Z")

</div>

> [@ambv](#):
>
> if we made a `CallableLike[some_func]` construct to solve the problem above, we would put possibly irrelevant details about the function signature into the required callable type; for example: what if we don’t care about the names of the positional arguments?

This is offtopic here, but I think that it is better to make it a callable rather than a subscroptable: `callable_type(some_func)`. Then it could be used as a decorator:

```python
@callable_type
def MyCallableType(a: int, /, b:str, *, c: float = 0) -> bool:
    ...

```

If we don’t care about the names of the positional arguments, we should make them positional-only.

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [May 18, 2023, 6:24pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/22 "2023-05-18T18:24:18Z")

</div>

That proposal is made obsolete by PEP 695.

---

<div class="post-metadata">

**Author:** ![simonrouse9461](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/simonrouse9461/32/12433_2.png) [@simonrouse9461](https://discuss.python.org/u/simonrouse9461)\
**Post date:** [June 9, 2023, 7:43am UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/23 "2023-06-09T07:43:30Z")

</div>

Just curious, why is this still being discussed? Hasn’t PEP 695 already been approved for being added in Python 3.12?

---

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [June 9, 2023, 7:56am UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/24 "2023-06-09T07:56:22Z")

</div>

> [@simonrouse9461](#):
>
> Just curious, why is this still being discussed?

Because PEP 695 does not propose making functions subscriptable at runtime

---

<div class="post-metadata">

**Author:** ![Gobot1234](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gobot1234/32/7165_2.png) [@Gobot1234](https://discuss.python.org/u/Gobot1234)\
**Post date:** [June 23, 2023, 3:16pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/25 "2023-06-23T15:16:46Z")

</div>

I’ve written up the first draft of this idea as a PEP. Any thoughts and feedback would be greatly appreciated (as would a sponsor 😉)

> <https://gist.github.com/Gobot1234/71e4d948b02c7a6f04cf484455c6d971>

Thanks.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [June 23, 2023, 3:48pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/26 "2023-06-23T15:48:41Z")

</div>

I will sponsor the PEP.

---

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [June 23, 2023, 3:55pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/27 "2023-06-23T15:55:11Z")

</div>

> [@Gobot1234](#):
>
> I’ve written up the first draft of this idea as a PEP. Any thoughts and feedback would be greatly appreciated (as would a sponsor 😉)

That looks pretty promising! I find the examples in the “Motivation” section pretty convincing. A few thoughts right off the bat:

- You use the term “bidirectional inference” in the second sentence, which is quite type-theory-jargon-y. It would be good to use less jargony language _or_ explain what the jargon means _or_ link to a definition of the term. (Mypy also more often refers to this concept as “type context” in their documentation/codebase.)
- You refer to `Unknown` several times, which is a pyright-specific concept. It would be better to say something like “type checker cannot infer the type of `T`” instead of “revealed type is `list[Unknown]`” – then it can apply to all type checkers equally
- You say “This proposal also opens the door for runtime reflection of type parameters similar to Rust, C++ and Scala.” Discussing those concepts would be beyond the scope of the PEP, I think, but it might be good to link to an explanation of what you’re referring to here.

> [@Jelle](#):
>
> I will sponsor the PEP.

I’m also happy to sponsor 🙂

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [June 23, 2023, 4:02pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/28 "2023-06-23T16:02:43Z")

</div>

I would also happily sponsor.

I note that Mojo is adopting this syntax as well.

---

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [June 23, 2023, 4:29pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/29 "2023-06-23T16:29:11Z")

</div>

Another piece of feedback: this is one of the motivating examples in the PEP:

```py
def make_list[T](*args: T) -> list[T]: ...
reveal_type(make_list()) # ??

```

It might be worth noting explicitly that type checkers should be able to infer the type here if an explicit annotation is given as the “expected type” for the returned object, e.g.:

```py
def make_list[T](*args: T) -> list[T]: ...
x: list[int] = make_list()
reveal_type(x) # list[int]

```

There are drawbacks to this solution: it forces users to split expressions into multiple parts, making code unnecessarily verbose. But it would be good to explicitly state in the PEP what the current solution is, and why it’s insufficient; situations where it won’t suffice, etc.

---

<div class="post-metadata">

**Author:** ![Gobot1234](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gobot1234/32/7165_2.png) [@Gobot1234](https://discuss.python.org/u/Gobot1234)\
**Post date:** [June 23, 2023, 5:14pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/30 "2023-06-23T17:14:50Z")

</div>

Thanks for all the pointers Alex

---

<div class="post-metadata">

**Author:** ![Gobot1234](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gobot1234/32/7165_2.png) [@Gobot1234](https://discuss.python.org/u/Gobot1234)\
**Post date:** [June 23, 2023, 5:52pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/31 "2023-06-23T17:52:36Z")

</div>

Thank you Jelle, Alex and Guido for offering to sponsor the PEP, you’re really spoiling me for choice after consulting random.choice I’m happy to say I’m picking Guido to sponsor the PEP.

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [June 23, 2023, 6:44pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/32 "2023-06-23T18:44:56Z")

</div>

Okay. I hope that Jelle an Alex will continue to provide high quality detailed feedback on the PEP! Do you know the next steps?

---

<div class="post-metadata">

**Author:** ![Gobot1234](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gobot1234/32/7165_2.png) [@Gobot1234](https://discuss.python.org/u/Gobot1234)\
**Post date:** [June 23, 2023, 6:57pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/33 "2023-06-23T18:57:06Z")

</div>

Rewrite the PEP in rst add the appropriate headings and make a PR to python/peps?

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [June 23, 2023, 8:23pm UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/34 "2023-06-23T20:23:51Z")

</div>

Yup that’s it.

---

<div class="post-metadata">

**Author:** ![srittau](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/srittau/32/237_2.png) [@srittau](https://discuss.python.org/u/srittau)\
**Post date:** [June 20, 2024, 10:25am UTC](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/35 "2024-06-20T10:25:34Z")

</div>

This would also be required to type `functools.wraps` correctly. See [functools.wraps does not work on class methods · Issue #10653 · python/typeshed · GitHub](https://github.com/python/typeshed/issues/10653). Basically, instead of copying the whole signature – like we do for functions – for methods we’d not want to copy the `self` argument. This would need an overload for `MethodType`.

[Previous page](https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463.md?page=1)
