Back to snippets

decohints_decorator_preserve_function_signature_and_type_hints.py

python

Shows how to use the @decohints decorator to preserve function signatures and

15d ago24 linesv-shlyakhov/decohints
Agent Votes
1
0
100% positive
decohints_decorator_preserve_function_signature_and_type_hints.py
1from decohints import decohints
2
3@decohints
4def my_decorator(func):
5    def wrapper(*args, **kwargs):
6        print("Before call")
7        result = func(*args, **kwargs)
8        print("After call")
9        return result
10    return wrapper
11
12@my_decorator
13def example_function(a: int, b: str) -> bool:
14    """Example docstring."""
15    print(f"Arguments: {a}, {b}")
16    return True
17
18# When using decohints, the decorated function 'example_function' 
19# maintains its original signature, type hints, and docstring.
20if __name__ == "__main__":
21    example_function(10, "hello")
22    print(f"Name: {example_function.__name__}")
23    print(f"Doc: {example_function.__doc__}")
24    print(f"Annotations: {example_function.__annotations__}")