decohints
decohints is a Python library that provides a decorator to help IDEs like PyCharm correctly display the parameter hints for functions wrapped by other decorators. It addresses a common pain point in Python development by preserving the original function's signature for better developer experience and type checking. The current version is 1.0.9, and it maintains an active release cadence.
Common errors
-
PyCharm/IDE does not show correct parameter hints (e.g., shows `*args, **kwargs` instead of actual parameters) for a function decorated by a custom decorator.
cause By default, Python decorators replace the decorated function with a wrapper, causing IDEs to lose the original function's signature for parameter hinting.fixApply the `@decohints` decorator to your custom decorator function. For instance, if you have `def my_decorator():`, change it to `@decohints\ndef my_decorator():`. -
Skipping analyzing 'decohints': module is installed, but missing library stubs or py.typed marker (Mypy error)
cause `decohints` versions prior to 1.0.5 did not properly signal to static type checkers that it provides type hints, leading Mypy to ignore its type information.fixUpgrade `decohints` to version 1.0.5 or later using `pip install --upgrade decohints`. This version includes the `py.typed` marker, enabling Mypy to correctly analyze the library.
Warnings
- gotcha When applying `decohints` to your own decorators, ensure that `@decohints` is the outermost decorator (i.e., placed directly above your decorator function definition) if your decorator is already wrapped in other decorators.
- gotcha Older versions of `decohints` (prior to 1.0.5) did not include the `py.typed` marker, which could lead to Mypy (or other static type checkers) skipping type analysis for the library or reporting 'missing library stubs' errors.
Install
-
pip install decohints
Imports
- decohints
from decohints import decohints
Quickstart
from functools import wraps
from decohints import decohints
@decohints
def my_simple_decorator(msg: str = "Hello"):
def _decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
print(f"Decorator received: {msg}")
return func(*args, **kwargs)
return wrapper
return _decorator
@my_simple_decorator(msg="Decorating this!")
def greet(name: str, age: int) -> str:
"""A function that greets someone."""
return f"Hi {name}, you are {age} years old."
result = greet("Alice", 30)
print(result)
# In PyCharm, typing `greet(` will now show `(name: str, age: int)` hints.