UI text when hovering over a variable whose type is a callable Protocol #5356
aholmes
started this conversation in
Enhancement
Replies: 1 comment
-
Seems reasonable to me. Protocol derived Classes should be represented like a function in the UI, not a class. Or maybe at least show extra information considering they're protocols. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
References
With reference to Python 3.10 as a minimum version.
When type-hinting callables, we run into limitations and need workarounds. For example, a type I will refer to has the signature
(config: Config, request_context_args: dict[Any, Any] | None = None) -> RequestContext
.Please reference this code.
Using
Callable
:Using
Protocol
:Using either type as a paramter:
Using either type as a variable:
Real code examples can be found here and here.
The Problem
With
Callable
, we can almost represent this asCallable[[Config, dict[Any, Any] | None], RequestContext]
. We run into three issues:Callable
does not allow parameter namesCallable
does not allow default parameter valuesHowever, one advantage with
Callable
is in how Pylance displays information about it when hovering over the type:Common advice to work around the issues mentioned is to specify a
Protocol
class and the__call__
method. Indeed this does fix the issues, however, the UI experience is degraded.And in fact the UI experience when hovering over a parameter (specifically) whose type is the callable is less than ideal in both scenarios.
Interestingly, I just noticed that a variable whose type is
Callable
is not displayed the same way as a parameter. This is not true with theProtocol
workaround, which does display the same as when used as a parameter.The Desired Enhancement
When a variable or parameter is a
Protocol
type with__call__
defined, and when hovered over or otherwise displaying information about the variable or parameter, showing the full call signature would make its purpose and usage more obvious. Ideally this would look similar, if not identical, to when the variable or parameter is a function:Beta Was this translation helpful? Give feedback.
All reactions