Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gri-gus/decohints
A decorator for decorators that allows you to see the parameters of a decorated function when using it in PyCharm.
https://github.com/gri-gus/decohints
decohints decorators decorators-python pycharm python python3
Last synced: 2 months ago
JSON representation
A decorator for decorators that allows you to see the parameters of a decorated function when using it in PyCharm.
- Host: GitHub
- URL: https://github.com/gri-gus/decohints
- Owner: gri-gus
- License: apache-2.0
- Created: 2022-04-16T11:05:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-03T08:34:35.000Z (over 1 year ago)
- Last Synced: 2024-09-27T06:03:33.201Z (3 months ago)
- Topics: decohints, decorators, decorators-python, pycharm, python, python3
- Language: Python
- Homepage:
- Size: 121 KB
- Stars: 37
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# decohints
A decorator for decorators that allows you to see the parameters of a decorated function when using it in PyCharm.
**PyPi**: https://pypi.org/project/decohints/
## Reasons for creation
Below is an example of a decorator with parameters without the use of `decohints`:
```python
from functools import wrapsdef decorator_with_params(aa=None, bb=None, cc=None):
def _decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
except Exception:
print("Error")
return
return resultreturn wrapper
return _decorator
@decorator_with_params()
def test(a: int, b: int) -> int:
return a + b
```If you type below `test()` in PyCharm and wait, it will show decorator wrapper parameter hints as `test` function
parameter hints:This is not convenient and can confuse developers, which is why this library was made.
## Installation
```shell
pip install decohints
```## Usage
> ✅ Works with all kinds of decorators \
> ⚠️ If your decorator is already wrapped in another decorator, then `decohints` should be on topTo use, you need to follow two simple steps:
1. Import the `decohints` decorator from the `decohints` library:
```python
from decohints import decohints
```2. Wrap your decorator with a `decohints` decorator:
```python
@decohints
def your_decorator():
...
```The following is an example of a decorator with parameters, with using `decohints`:
```python
from functools import wrapsfrom decohints import decohints
@decohints
def decorator_with_params(aa=None, bb=None, cc=None):
def _decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
except Exception:
print("Error")
return
return resultreturn wrapper
return _decorator
@decorator_with_params()
def test(a: int, b: int) -> int:
return a + b
```If you type below `test()` in PyCharm and wait, it will show `test` function parameter hints:
> ❕Examples of use with decorator class, class decorators, and more are found at here:
click## Alternatives
### Specifying the type of wrapper
> ✅ Works with all kinds of decorator functions
Specifying the type `wrapper: func` will have the same behavior as using `decohints`.
Example:
```python
from functools import wrapsdef decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
except Exception:
print("Error")
return
return resultwrapper: func
return wrapper@decorator
def test(a: int, b: int) -> int:
return a + b
```If you type below `test()` in PyCharm and wait, it will show `test` function parameter hints:
### Specifying an output type in a decorator with parameters
> ❗️This method only works in decorator functions with parameters
If you specify the `Callable` type from the `typing` module for the result of the decorator with parameters, then the
behavior will be the same as using `decohints`.Example:
```python
from functools import wraps
from typing import Callabledef decorator_with_params(aa=None, bb=None, cc=None) -> Callable:
def _decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
except Exception:
print("Error")
return
return resultreturn wrapper
return _decorator
@decorator_with_params()
def test(a: int, b: int) -> int:
return a + b
```If you type below `test()` in PyCharm and wait, it will show `test` function parameter hints: