Open MattRCole opened 1 year ago
ParamSpec
is the right way to annotate a function like not_a_decorator
in your example. Your instincts were spot on here. If you enable type checking (set "typeCheckingMode" to "basic"), you can see that pyright (the type checker upon which pylance is built) will validate the arguments passed to not_a_decorator
and report type violations if present.
What you're looking for here is a language server feature — the ability for the signature help provider to have an understanding of parameters that are provided by an earlier argument that is bound to a ParamSpec
. I don't recall anyone requesting this feature previously, but it's a reasonable enhancement request for pylance.
what does mean triage-needed?
what does mean triage-needed?
It means that the Pylance dev team needs to discuss how to address the issue.
jfyi, this seems to be working already:
Given:
class Base:
@classmethod
def create(
cls: Callable[P, T], *args: P.args, **kwargs: P.kwargs
) -> T:
return cls(*args, **kwargs)
class Derived(Base):
def __init__(self, foo: str):
self._foo = foo
I see the right completion on Derived.create
.
+1 to this feature. It would be great to have autocomplete working for ParamSpec
Question:
Let's say we have the following function:
How would we go about typing
not_a_decorator
so thatpylance
can dynamically tell us whatargs
andkwargs
are expected based on the givenfn
?Example:
Why do this?
Although this seems like an unusual usecase, it's not too far fetched. Take
functools.partial
: While the return value offunctools.partial
is very hard to type, it would be useful to have type hints for the correct parameters to pass topartial
based on the function that was passed in as the first argument.My attempted solutions
Typing the function directly
This fails to provide good parameter type hints:
Making a type alias and using
Concatenate
This also fails to provide good parameter type hints:
Using a Protocol
This was the final way I could think of to achieve this
Unfortunately, this also fails in the same manner:
Conclusion
It really feels like
ParamSpec
should allow this kind of dynamic type hinting, but it doesn't, or at least not in these three ways. Is there a different way to do this that I missed or is this not currently supported? If not, is it in-scope to support such a use-case ofParamSpec
?