Dynamically create python functions with a proper signature.
BSD-3-CLAUSE License
Bot releases are hidden (Show)
Published by smarie over 5 years ago
add_signature_parameters
now correctly inserts parameters in the right order when they are prepended (using first=
). Fixed #29.
See documentation page for details.
Published by smarie over 5 years ago
Function creation API:
handler
into impl
for clarity. Fixes #27.addsource
and addhandler
arguments as add_source
and add_impl
respectively, for consistency@wraps
or create_wrapper
, because that's probably what you want to do (= reuse not only the signature but also all metadata). Fixes #26.__qualname__
attributefunc_impl
in create_function
and @with_signature
, and are the ones from the wrapped function in create_wrapper
and @wraps
as intuitively expected. Fixes #28.Wrappers:
@wraps
and create_wrapper
now offer a new_sig
argument. In that case the __wrapped__
attribute is not set. Fixes #25.@wraps
and create_wrapper
now correctly preserve the __dict__
and other metadata from the wrapped item. Fixes #24
See documentation page for details.
Published by smarie over 5 years ago
When a non-representable default value was used in the signature to generate, the code failed with a SyntaxError
. This case is now correctly handled, by storing the corresponding variable in the generated function's context. Fixes #23.
See documentation page for details.
Published by smarie over 5 years ago
Now providing a @wraps
, equivalent of functools.wraps
; and a create_wrapper
equivalent of functools.update_wrapper
. Fixes #21
@with_signature
now does not override the __name__
when signature is provided as a function. Fixes #22
add_signature_parameters
now accepts that parameters are provided as single elements (not necessarily iterables)
Updated documentation
See documentation page for details.
Published by smarie over 5 years ago
None
can be used as the desired signature of @with_signature
. This indicated that the user does not want to create a new function but only wants to update the metadata. Fixes #20.
See documentation page for details.
Published by smarie over 5 years ago
Fixed isgeneratorfunction
for old python versions, see decorator#63.
Python<3.3-specific function body is now not loaded at all if not needed.
See documentation page for details.
Published by smarie over 5 years ago
inject_as_first_arg
was missing from @with_signature
, added it. Fixed #18.
See documentation page for details.
Published by smarie over 5 years ago
Now create_function
and @with_signature
create the same kind of function than the handler. So if it is a generator, a generator-based coroutine, or an async coroutine, the generated function will adapt. Fixes #6.
See documentation page for details.
Published by smarie over 5 years ago
Now a string signature can be provided to @with_signature
without problem. Fixed #17.
See documentation page for details.
Published by smarie over 5 years ago
See documentation page for details.
Published by smarie over 5 years ago
@with_signature
:
create_function
. Fixed #12.None
as the new func_signature
to declare that the signature is identical to the decorated function. This can be handy to just change the docstring or module name of a function for example. Fixes #15
create_function
and @with_signature
:
modulename
parameter to override the module name. Fixes #14
__call_handler__
). New addhandler
parameter (default: True) controls this behaviour. Fixes #16
Misc:
add_signature_parameters
.See documentation page for details.
Published by smarie over 5 years ago
New helper function remove_signature_parameters
.
Fixed issue with @with_signature
when argument is a Signature
. Fixes #11
See documentation page for details.
Published by smarie over 5 years ago
New decorator @with_signature
to change the signature of a callable. Fixes #3
create_function
now accepts that a function be passed as a signature template. Fixes #10
See documentation page for details.
Published by smarie over 5 years ago
Functions can now be created from a Signature
object, in addition to string signatures. This unlocks many useful use cases, among easily creating function wrappers. Note: the inner function that provides this feature is get_signature_from_string
. Fixes #8
Improved design by getting rid of the regular expression parser to check parameters definition. This assumes that the compiler will correctly raise exceptions when a string signature is not correct, and that inspect.signature
or funcsigs.signature
works correctly at detecting all the parameter kinds and annotations on the resulting function. It seems like a fair assumption... Fixes #9.
See documentation page for details.
Published by smarie over 5 years ago
create_function
:
create_function
does not require users to prepend "def "
to the signature anymore. Fixed #5
Return annotations are now supported. Fixes #4.
Type hint as comments are supported but the generated function loses the annotations because inspect.signature
loses the annotation too in that case. Fixes #7
Variable-length arguments such as *args
and **kwargs
are now properly handled. Fixes #2
Handler functions can now receive the dynamically created function as first argument, by using create_function(func_signature, func_handler, inject_as_first_arg=True)
. Fixes #1
Renamed _call_
into _call_handler_
in the generated code.
Misc:
pytest-cases
dependency for tests.See documentation page for details.