Want to add type hints to your Python decorator?

A. Yes.

from functools import wraps
from typing import Any, Callable, TypeVar, cast

FuncType = TypeVar("FuncType", bound=Callable[..., Any])

def print_funcname(func: FuncType) -> FuncType:
    @wraps(func)
    def wrapper():
        print("Function name:", func.__name__)
        func()
    return cast(FuncType, wrapper)

@print_funcname
def hello():
    print("Hello!")

Recommended Posts

Want to add type hints to your Python decorator?
Practice! !! Introduction to Python (Type Hints)
python decorator to retry
[Introduction to Udemy Python 3 + Application] 57. Decorator
Add Python 2.7 Japanese documentation to Dash.app
Add TRACE log level to Python ...?
I want to debug with Python
Add a GPIO board to your computer. (1)
[Introduction to Udemy Python3 + Application] 28. Collective type
Add your own content view to mitmproxy
Reintroduction to Python Decorators ~ Learn Decorators by Type ~
[Django] Want to customize your admin page?
[Introduction to Udemy Python3 + Application] 21. Tuple type
I want to use jar from python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
I want to build a Python environment
I want to analyze logs with Python
I want to play with aws with python
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[python] How to add RDF triples to your own Fuseki server using rdflib
[Python] Add total rows to Pandas DataFrame
Add Gaussian noise to images with python2.7
How to add python module to anaconda environment
[Introduction to Udemy Python3 + Application] 16. List type
Add a Python virtual environment to VSCode
[Python] Add comments to standard input files
[Python] Introduce UIKit3 to your Django project
I just want to add scipy, but it's a messy note. Ubuntu, Python 3.
I want to do Dunnett's test in Python
Add your favorite display function to pip search
[Python] How to use two types of type ()
I want to use MATLAB feval with python
Just add the python array to the json data
Type Python scripts to run in QGIS Processing
I want to memoize including Python keyword arguments
I want to create a window in Python
Add diagonal lines (hatch) to heatmap (python, matplotlib)
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to make a game with Python
How to add a Python module search path
I want to merge nested dicts in Python
I want to use Temporary Directory with Python2
To add a module to python put in Julialang
I want to use ceres solver from python
How to get dictionary type elements of Python 2.7
#Unresolved I want to compile gobject-introspection with Python3
I want to create a plug-in type implementation
I want to solve APG4b with Python (Chapter 2)
Speed: Add element to end of Python array
I want to sell Mercari by scraping python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
[Python] Do your best to speed up SQLAlchemy
I want to write to a file with Python
How to handle datetime type in python sqlite3
I want to display the progress in Python!
Note: Python Decorator
[Python] I want to add a static directory with Flask [I want to use something other than static]
Updated to Python 2.7.9
Decorator to retry