typelets.funcs¶
Typing useful for defining and calling functions.
Added in version 1.0.
Module Attributes
Standard type for referencing a callable's parameters. |
|
Standard type for referencing a function's return value. |
|
Standard type for the owner of a method. |
|
A type indicating a dictionary used for keyword arguments. |
Functions
|
Decorator for typing a function signature based on another function. |
|
Decorator for typing a function signature based on another method. |
Classes
|
Base class for defining a signature based on a function. |
|
Base class for defining a signature based on a method. |
|
A representation of a method on a class. |
|
Defines a signature for inheriting all parameters from a function. |
|
Defines a signature for inheriting all parameters from a method. |
- typelets.funcs.TParams = ~TParams[source]¶
Standard type for referencing a callable’s parameters.
Added in version 1.1.
- class typelets.funcs.TReturn_co[source]¶
Standard type for referencing a function’s return value.
Added in version 1.1.
alias of TypeVar(‘TReturn_co’, covariant=True)
- class typelets.funcs.TOwner[source]¶
Standard type for the owner of a method.
This would generally be a class.
alias of TypeVar(‘TOwner’, bound=
object)
- typelets.funcs.KwargsDict¶
A type indicating a dictionary used for keyword arguments.
Added in version 1.0.
- class typelets.funcs.MethodDirective(*args, **kwargs)[source]¶
Bases:
Protocol[TOwner,TParams,TReturn_co]A representation of a method on a class.
This works as a directive for converting an unbound method to a bound method. It can be returned in place of a
Callable[]to ensure proper typing when returning a type for a method.Added in version 1.1.
- __get__(instance: None, owner: type[TOwner]) Callable[Concatenate[TOwner, TParams], TReturn_co][source]¶
- __get__(instance: TOwner, owner: type[TOwner]) Callable[TParams, TReturn_co]
- __init__(*args, **kwargs)¶
- class typelets.funcs.BaseParamsFromFunc(func: Callable[TParams, Any])[source]¶
-
Base class for defining a signature based on a function.
This is built to be used with the
type_func_params_as()ortype_method_params_as()decorators. It references another function, classmethod, or staticmethod, providing access to itsParamSpecand allows its use in a__call__()method.Subclasses must implement
__call__(), taking in the parameters needed by the decorated function, and returnsAnyspecifically.Note
There are some things to be aware of when writing your
__call__()method:Because this is defined in a class, it must contain
self, even if your decorated function does not. This won’t affect the final type of your function.typing.Anymust always be returned from the signature, regardless of the referenced or decorated functions.If your decorated function uses
*,/, those must be present.You must include both
TParams.argsandTParams.kwargsat the end of the signature, even if the wrapped function doesn’t use either*argsor**kwargs.This is necessary for the type checkers.
Using
/to indicate positional-only arguments may trigger a type error when decorating a class. This should not affect the resulting type.We recommend using
# type: ignoreon the decorator line in this situation.
If your decorated function simply takes
*argsand/or**kwargsand passes them to the referenced without taking any additional parameters, you can use:Added in version 1.1.
Examples
A simple function with*argsand**kwargs.¶from typelets.funcs import BaseParamsFromFunc, TParams # This would provide typing for any of the following functions. # # def my_function( # name: str, # age: int, # *args, # **kwargs, # ) -> str: # # def my_function( # name: str, # *args, # age: int, # **kwargs, # ) -> str: # # def my_function( # name: str, # age: int, # *args, # ) -> str: # # def my_function( # name: str, # age: int, # **kwargs, # ) -> str: # # def my_function( # *, # name: str, # age: int, # **kwargs, # ) -> str: class MyFunctionParamsFrom(BaseParamsFromFunc[TParams]): def __call__( self, name: str, age: int, *args: TParams.args, **kwargs: TParams.kwargs, ): ...
- class typelets.funcs.BaseParamsFromMethod(func: Callable[Concatenate[TOwner, TParams], Any])[source]¶
Bases:
Generic[TParams,TOwner]Base class for defining a signature based on a method.
This is built to be used with the
type_func_params_as()ortype_method_params_as()decorators. It references an unbound method on a class, providing access to itsParamSpecand allows its use in a__call__()method.Subclasses must implement
__call__(), taking in the parameters needed by the decorated function, and returnsAnyspecifically.Note
There are some things to be aware of when writing your
__call__()method:Because this is defined in a class, it must contain
self, even if your decorated function does not. This won’t affect the final type of your function.typing.Anymust always be returned from the signature, regardless of the referenced or decorated functions.If your decorated function uses
*,/, those must be present.You must include both
TParams.argsandTParams.kwargsat the end of the signature, even if the wrapped function doesn’t use either*argsor**kwargs.This is necessary for the type checkers.
Using
/to indicate positional-only arguments may trigger a type error when decorating a class. This should not affect the resulting type.We recommend using
# type: ignoreon the decorator line in this situation.
If your decorated function simply takes
*argsand/or**kwargsand passes them to the referenced without taking any additional parameters, you can use:Added in version 1.1.
Examples
A simple function with*argsand**kwargs.¶from typelets.funcs import BaseParamsFromMethod, TOwner, TParams # This would provide typing for any of the following functions. # # def my_function( # self, # name: str, # age: int, # *args, # **kwargs, # ) -> str: # # def my_function( # self, # name: str, # *args, # age: int, # **kwargs, # ) -> str: # # def my_function( # self, # name: str, # age: int, # *args, # ) -> str: # # def my_function( # self, # name: str, # age: int, # **kwargs, # ) -> str: # # def my_function( # self, # *, # name: str, # age: int, # **kwargs, # ) -> str: class MyFunctionParamsFrom(BaseParamsFromMethod[TParams, TOwner]): def __call__( self, name: str, age: int, *args: TParams.args, **kwargs: TParams.kwargs, ): ...
- class typelets.funcs.OnlyParamsFromFunc(func: Callable[TParams, Any])[source]¶
Bases:
BaseParamsFromFunc[TParams]Defines a signature for inheriting all parameters from a function.
This is used whenever you have a function or method that takes in a
*argsand/or**kwargs(but no other parameters) and passes them to another function.The
*argsand/or**kwargswill inherit the signature and types from the referenced function.This can be used with either
type_func_params_as()ortype_method_params_as().Example
from typelets.funcs import (OnlyParamsFromFunc, type_func_params_as, type_method_params_as) def wrapped_func( a: int, b: str, /, c: int, *, d: bool, ) -> int: return a + c # Apply to a function: @type_func_params_as(OnlyParamsFromFunc(wrapped_func)) def my_wrapper_func(*args, **kwargs) -> str: return str(wrapped_func(*args, **kwargs)) # Or apply to a method: class MyClass: @type_method_params_as(OnlyParamsFromFunc(wrapped_func)) def my_wrapper_func(self, *args, **kwargs) -> str: return str(wrapped_func(*args, **kwargs))
Added in version 1.1.
- class typelets.funcs.OnlyParamsFromMethod(func: Callable[Concatenate[TOwner, TParams], Any])[source]¶
Bases:
BaseParamsFromMethod[TParams,TOwner]Defines a signature for inheriting all parameters from a method.
This is used whenever you have a function or method that takes in a
*argsand/or**kwargs(but no other parameters) and passes them to another method.The
*argsand/or**kwargswill inherit the signature and types from the referenced function.This can be used with either
type_func_params_as()ortype_method_params_as().Example
from typelets.funcs import (OnlyParamsFromMethod, type_func_params_as, type_method_params_as) class OtherClass: def wrapped_func( self, a: int, b: str, /, c: int, *, d: bool, ) -> int: return a + c # Apply to a function: @type_func_params_as(OnlyParamsFromMethod(OtherClass.wrapped_func)) def my_wrapper_func(*args, **kwargs) -> str: return str(OtherClass().wrapped_func(*args, **kwargs)) # Or apply to a method: class MyClass: @type_method_params_as(OnlyParamsFromMethod( OtherClass.wrapped_func )) def my_wrapper_func(self, *args, **kwargs) -> str: return str(OtherClass().wrapped_func(*args, **kwargs))
Added in version 1.1.
- typelets.funcs.type_func_params_as(params_from: Callable[TParams, Any], *, args_name: str | None = 'args', kwargs_name: str | None = 'kwargs') Callable[[Callable[TParams, TReturn_co]], Callable[TParams, TReturn_co]][source]¶
Decorator for typing a function signature based on another function.
This can be used with standard functions, classmethods, and staticmethods to type
*argsand/or**kwargsbased on the arguments defined in a referenced function.It’s built for functions that take in arguments for the purpose of passing to another function or method, which normally would mean either duplicating the arguments in the signature or losing out on typing altogether.
This works as a decorator that takes a function signature definition (an instance of a
BaseParamsFromFuncsubclass) or method signature (an instance of aBaseParamsFromMethod), and retypes the decorated function using that signature. The definition is able to use theParamSpecof a referenced function.You can craft your own definition by creating a subclass of either of these base classes. See the documentation for examples.
For simple cases, you can use the built-in
OnlyParamsFromFuncorOnlyParamsFromMethod.There is minimal impact to function setup, and no impact when calling the function at runtime. The decorated function’s
__annotations__and__signature__will be patched at decoration time. There are no costs to calling the function, and no changes to function behavior.Warning
This may affect docstrings in Visual Studio Code or in other editors using PyLance, due to the way that docstrings are inferred from the first-provided callable. See https://github.com/microsoft/pylance-release/issues/5840.
Added in version 1.1.
- Parameters:
params_from (
callable) –The callable function signature to base parameters off of.
This is generally going to be an instance of a
BaseParamsFromFuncsubclass.args_name (
str, optional) –Optional name of the
*argsargument.By default, the name will be auto-detected if
"args"isn’t the correct name. Consumers can set this to a name explicitly, or set it toNoneto avoid looking for this argument.kwargs_name (
str, optional) –Optional name of the
*kwargsargument.By default, the name will be auto-detected if
"kwargs"isn’t the correct name. Consumers can set this to a name explicitly, or set it toNoneto avoid looking for this argument.
Examples
Type a function based fully on a function¶from typelets.funcs import (OnlyParamsFromFunc, type_func_params_as, type_method_params_as) def wrapped_func( a: int, b: str, /, c: int, *, d: bool, ) -> int: return a + c @type_func_params_as(OnlyParamsFromFunc(wrapped_func)) def my_wrapper_func(*args, **kwargs) -> str: return str(wrapped_func(*args, **kwargs))
Type a function based fully on a method¶from typelets.funcs import (OnlyParamsFromMethod, type_method_params_as) class MyClass: def wrapped_func( self, a: int, b: str, /, c: int, *, d: bool, ) -> int: return a + c @type_func_params_as(OnlyParamsFromMethod(MyClass.wrapped_func)) def my_wrapper_func(*args, **kwargs) -> str: return str(wrapped_func(*args, **kwargs))
Type a method using a custom signature¶from typing import Any from typelets.funcs import (BaseParamsFromFunc, TParams, type_method_params_as) class MyParamsFrom(BaseParamsFromFunc[TParams]): def __call__( self, x: int, y: bool = True, *args: TParams.args, **kwargs: TParams.kwargs, ) -> Any: ... def wrapped_func( a: int, b: str, /, c: int, *, d: bool, ) -> int: return a + c @type_func_params_as(MyParamsFrom(wrapped_func)) def my_wrapper_func( x: int, y: bool = True, *args, **kwargs, ) -> str: return str(wrapped_func(*args, **kwargs))
- typelets.funcs.type_method_params_as(params_from: Callable[TParams, Any], *, args_name: str | None = 'args', kwargs_name: str | None = 'kwargs') Callable[[Callable[Concatenate[TOwner, TParams], TReturn_co]], MethodDirective[TOwner, TParams, TReturn_co]][source]¶
Decorator for typing a function signature based on another method.
This can be used with unbound methods on classes to type
*argsand/or**kwargsbased on the arguments defined in a referenced function.It’s built for methods that take in arguments for the purpose of passing to another function or method, which normally would mean either duplicating the arguments in the signature or losing out on typing altogether.
This works as a decorator that takes a function signature definition (an instance of a
BaseParamsFromFuncsubclass) or method signature (an instance of aBaseParamsFromMethod), and retypes the decorated method using that signature. The definition is able to use theParamSpecof a referenced function.Both the unbound method and bound method will have the correct signature.
You can craft your own definition by creating a subclass of either of these base classes. See the documentation for examples.
For simple cases, you can use the built-in
OnlyParamsFromFuncorOnlyParamsFromMethod.There is minimal impact to function setup, and no impact when calling the function at runtime. The decorated function’s
__annotations__and__signature__will be patched at decoration time. There are no costs to calling the function, and no changes to function behavior.Warning
This may affect docstrings in Visual Studio Code or in other editors using PyLance, due to the way that docstrings are inferred from the first-provided callable. See https://github.com/microsoft/pylance-release/issues/5840.
Added in version 1.1.
- Parameters:
params_from (
callable) –The callable function signature to base parameters off of.
This is generally going to be an instance of a
BaseParamsFromFuncsubclass.args_name (
str, optional) –Optional name of the
*argsargument.By default, the name will be auto-detected if
"args"isn’t the correct name. Consumers can set this to a name explicitly, or set it toNoneto avoid looking for this argument.kwargs_name (
str, optional) –Optional name of the
*kwargsargument.By default, the name will be auto-detected if
"kwargs"isn’t the correct name. Consumers can set this to a name explicitly, or set it toNoneto avoid looking for this argument.
Examples
Type a method based fully on a function¶from typelets.funcs import (OnlyParamsFromFunc, type_method_params_as) def wrapped_func( a: int, b: str, /, c: int, *, d: bool, ) -> int: return a + c class MyClass: @type_method_params_as(OnlyParamsFromFunc(wrapped_func)) def my_wrapper_func(self, *args, **kwargs) -> str: return str(wrapped_func(*args, **kwargs))
Type a method based fully on a method¶from typelets.funcs import (OnlyParamsFromMethod, type_method_params_as) class MyClass: def wrapped_func( self, a: int, b: str, /, c: int, *, d: bool, ) -> int: return a + c @type_method_params_as(OnlyParamsFromMethod(wrapped_func)) def my_wrapper_func(self, *args, **kwargs) -> str: return str(self.wrapped_func(*args, **kwargs))
Type a method using a custom signature¶from typing import Any from typelets.funcs import (BaseParamsFromMethod, TParams, TOwner, type_method_params_as) class MyParamsFrom(BaseParamsFromMethod[TParams, TOwner]): def __call__( self, x: int, y: bool = True, *args: TParams.args, **kwargs: TParams.kwargs, ) -> Any: ... class MyClass: def wrapped_func( self, a: int, b: str, /, c: int, *, d: bool, ) -> int: return a + c @type_method_params_as(MyParamsFrom(wrapped_func)) def my_wrapper_func( self, x: int, y: bool = True, *args, **kwargs, ) -> str: return str(self.wrapped_func(*args, **kwargs))