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
I'm sure that there are other use cases for this, but the main one that comes to mind is inferring the type of function parameters. It is common that you want to override a superclass method, for example, which may accept many parameters that you don't need in the override:
Unfortunately, most libraries, even if they provide type stubs, do not provide a TypedDict subclass that you can simply import in order to use Unpack[MyMethodKwargs], so the only way to not lose typing information is to redeclare all of the parameters.
If we could infer a ParamSpec type from the method type, and if we could infer the method type from the method object, then we could do something like:
typeParameters<Textends(...args: any)=>any>=Textends(...args: infer P)=>any ? P : never;
This would be a very cool feature as it could be used for many other patterns, enabling things like Params[T], ReturnType[T], KeyOf[T], ValueOf[T], etc.
Unfortunately, since expressions were not implemented for structural pattern matching, we can't directly mirror the syntax, but it could maybe look something like:
type Params[C: Callable[..., Any]] =matchC: PifcaseCallable[P, Any] elseNever
or:
type Params[C: Callable[..., Any]] matchC:
caseCallable[P, Any]: Pcase _: Never
The text was updated successfully, but these errors were encountered:
I'm sure that there are other use cases for this, but the main one that comes to mind is inferring the type of function parameters. It is common that you want to override a superclass method, for example, which may accept many parameters that you don't need in the override:
Unfortunately, most libraries, even if they provide type stubs, do not provide a TypedDict subclass that you can simply import in order to use
Unpack[MyMethodKwargs]
, so the only way to not lose typing information is to redeclare all of the parameters.If we could infer a ParamSpec type from the method type, and if we could infer the method type from the method object, then we could do something like:
I think that Python badly needs this, as
**kwargs
is so ubiquitous and one of the most notable places where type information is lost in my experience.The way that TypeScript implements this is by leveraging pattern matching within conditional types:
This would be a very cool feature as it could be used for many other patterns, enabling things like
Params[T]
,ReturnType[T]
,KeyOf[T]
,ValueOf[T]
, etc.Unfortunately, since expressions were not implemented for structural pattern matching, we can't directly mirror the syntax, but it could maybe look something like:
or:
The text was updated successfully, but these errors were encountered: