Code Monkey home page Code Monkey logo

decohints's Introduction

decohints

PyPI PyPI PyPI

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:

from functools import wraps


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 result

        return 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:

test() (*args, **kwargs)

This is not convenient and can confuse developers, which is why this library was made.

Installation

pip install decohints

Usage

✅ Works with all kinds of decorators
⚠️ If your decorator is already wrapped in another decorator, then decohints should be on top

To use, you need to follow two simple steps:

  1. Import the decohints decorator from the decohints library:
from decohints import decohints
  1. Wrap your decorator with a decohints decorator:
@decohints
def your_decorator():
    ...

The following is an example of a decorator with parameters, with using decohints:

from functools import wraps

from 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 result

        return 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:

test() (a: int, b: int)

❕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:

from functools import wraps


def decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            result = func(*args, **kwargs)
        except Exception:
            print("Error")
            return
        return result

    wrapper: 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:

test() (a: int, b: int)

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:

from functools import wraps
from typing import Callable


def 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 result

        return 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:

test() (a: int, b: int)

decohints's People

Contributors

gri-gus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ydf0509

decohints's Issues

PyCharm doesn't warning

from functools import wraps


from functools import wraps

from decohints import decohints


@decohints
def decorator_without_params(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            result = func(*args, **kwargs)
        except Exception:
            print("Error")
            return
        return result

    return wrapper


@decorator_without_params
def test(a: int, b: int) -> int:
    return a + b


test()

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.