You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve help() by making typing.overload() information accessible at runtime #89263
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-09-04.18:03:28.637>labels= ['3.11']
title='Improve help() by making typing.overload() information accessible at runtime'updated_at=<Date2022-03-28.22:36:21.459>user='https://github.com/rhettinger'
We could make my proposed overload registry more reusable by putting it in a different module, probably functools. (Another candidate is inspect, but inspect.py imports functools.py, so that would make it difficult to use the registry for functools.singledispatch.)
We could then bill it as a "variant registry", with an API like this:
@overload could then call register_variant() to register each overload, and code that wants a list of overloads (pydoc, inspect.signature, runtime type checkers) could call get_variants().
get_key_for_callable() essentially does f"{callable.__qualname__}.{callable.__name__}", but returns None for objects it can't handle. It will also support at least classmethods and staticmethods.
The latest plan sounds good to me. I have some Thoughts on the proposed API, but it will be easier to express those as part of a PR review. Looking forward to seeing the PR!
After #31716 is merged, I'd like to have a stab at writing a PR to have overloads shown in the output of help(). For now, it will probably be easiest to do that without tinkering with inspect.signature(), and instead only making changes to pydoc -- I have a few ideas of how to do that.
I was just playing around with the new get_overloads function, and it appears that it doesn't work if for functions defined in the interactive shell. This seems unfortunate -- it certainly took me by surprise.
This is because all functions defined in the interactive shell have the same line-number set on their code object, and the new overloads registry uses the co_firstlineno attribute of the code attribute to register overloads.
I initially thought that this would cause problems for using get_overloads to improve the output of help(). Maybe it's not so much of a problem, though, since most people won't be calling help() on functions defined in the interactive shell -- they'll be calling it on functions that they import from within the interactive shell. Moreover, using @overload for a function defined in the interactive shell is pretty weird, since a type checker isn't much use to you in the interactive shell.
It's a little unfortunate because it makes it harder for people to experiment with get_overloads() in the REPL. But I agree that it's not a big problem for people using help() to introspect library functions.
Since @overload is a static typing feature, why would you be typing it in the REPL in the first place?
If you read about get_overloads() in the What's New and want to try it out for yourself, you might write some overloads in the REPL to see it in action. Looks like that's essentially what happened to Alex.
Since @overload is a static typing feature, why would you be typing it in the REPL in the first place?
Yeah, this is why I think it might actuallly not be that much of a big deal, as I said above.
If you read about get_overloads() in the What's New and want to try it out for yourself, you might write some overloads in the REPL to see it in action. Looks like that's essentially what happened to Alex.
Yup. Experimenting with stuff in the REPL is generally a part of my process when I write patches for CPython. I was tinkering with modifying pydoc to use get_overloads, and was trying things out in the REPL. I was surprised when things didn't work as expected, and it took me a minute or two to figure out that the reason for the unexpected behaviour was that I was in the interactive shell.
Activity
JelleZijlstra commentedon Mar 5, 2022
We could make my proposed overload registry more reusable by putting it in a different module, probably functools. (Another candidate is inspect, but inspect.py imports functools.py, so that would make it difficult to use the registry for functools.singledispatch.)
We could then bill it as a "variant registry", with an API like this:
@overload could then call register_variant() to register each overload, and code that wants a list of overloads (pydoc, inspect.signature, runtime type checkers) could call get_variants().
get_key_for_callable() essentially does f"{callable.__qualname__}.{callable.__name__}", but returns None for objects it can't handle. It will also support at least classmethods and staticmethods.
I will prepare a PR implementing this idea.
AlexWaygood commentedon Mar 5, 2022
The latest plan sounds good to me. I have some Thoughts on the proposed API, but it will be easier to express those as part of a PR review. Looking forward to seeing the PR!
gvanrossum commentedon Mar 28, 2022
Looks like there may be a new plan where we solve a smaller problem (overloads) in the context of typing only.
AlexWaygood commentedon Apr 16, 2022
After #31716 is merged, I'd like to have a stab at writing a PR to have overloads shown in the output of
help(). For now, it will probably be easiest to do that without tinkering withinspect.signature(), and instead only making changes topydoc-- I have a few ideas of how to do that.gh-89263: Add typing.get_overloads (GH-31716)
AlexWaygood commentedon Apr 17, 2022
I was just playing around with the new
get_overloadsfunction, and it appears that it doesn't work if for functions defined in the interactive shell. This seems unfortunate -- it certainly took me by surprise.AlexWaygood commentedon Apr 17, 2022
This is because all functions defined in the interactive shell have the same line-number set on their code object, and the new overloads registry uses the
co_firstlinenoattribute of the code attribute to register overloads.I initially thought that this would cause problems for using
get_overloadsto improve the output ofhelp(). Maybe it's not so much of a problem, though, since most people won't be callinghelp()on functions defined in the interactive shell -- they'll be calling it on functions that they import from within the interactive shell. Moreover, using@overloadfor a function defined in the interactive shell is pretty weird, since a type checker isn't much use to you in the interactive shell.JelleZijlstra commentedon Apr 17, 2022
It's a little unfortunate because it makes it harder for people to experiment with
get_overloads()in the REPL. But I agree that it's not a big problem for people usinghelp()to introspect library functions.gvanrossum commentedon Apr 17, 2022
Since
@overloadis a static typing feature, why would you be typing it in the REPL in the first place?JelleZijlstra commentedon Apr 17, 2022
If you read about
get_overloads()in the What's New and want to try it out for yourself, you might write some overloads in the REPL to see it in action. Looks like that's essentially what happened to Alex.AlexWaygood commentedon Apr 17, 2022
Yeah, this is why I think it might actuallly not be that much of a big deal, as I said above.
Yup. Experimenting with stuff in the REPL is generally a part of my process when I write patches for CPython. I was tinkering with modifying
pydocto useget_overloads, and was trying things out in the REPL. I was surprised when things didn't work as expected, and it took me a minute or two to figure out that the reason for the unexpected behaviour was that I was in the interactive shell.py-typing-extensions: Update to 4.3.0
furkanonder commentedon May 9, 2023
@JelleZijlstra The issue seems to be solved. We can close the issue.
JelleZijlstra commentedon May 9, 2023
We added
typing.get_overloads, but I don't think we ended up makinghelp()look at this information yet.py-typing-extensions: Update to 4.3.0
py-typing-extensions: Update to 4.3.0
py-typing-extensions: Update to 4.3.0