Skip to main content

Some useful Python decorators for cleaner software development.

Project description

pedantic-python-decorators Build Status Coverage Status PyPI version Requirements Status

These decorators will make you write cleaner and well-documented Python code.

Getting Started

This package requires Python 3.6.1 or later. There are multiple options for installing this package.

Option 1: Installing with pip from Pypi

Run pip install pedantic.

Option 2: Installing with pip and git

  1. Install Git if you don't have it already.
  2. Run pip install git+https://github.com/LostInDarkMath/pedantic-python-decorators.git@master

Option 3: Offline installation using wheel

  1. Download the latest release here by clicking on pedantic-python-decorators-x.y.z-py-none-any.whl.
  2. Execute pip install pedantic-python-decorators-x.y.z-py3-none-any.whl.

Usage

Use from pedantic import pedantic, pedantic_class to import the pedantic decorators for example. Of course you could import whatever decorator you want to use as well. Don't forget to check out the documentation. Happy coding!

Minimal example

from typing import Union, List
from pedantic import pedantic, pedantic_class

@pedantic
def get_sum_of(values: List[Union[int, float]]) -> Union[int, float]:
    return sum(values)

@pedantic_class
class MyClass:
    def __init__(self, x: float, y: int) -> None:
        self.x = x
        self.y = y

    def print_sum(self) -> None:
        print(get_sum_of(values=[self.x, self.y]))

m = MyClass(x=3.14, y=2)
m.print_sum()

The @pedantic decorator

The @pedantic decorator does the following things:

  • The decorated function can only be called by using keyword arguments. Positional arguments are not accepted.
  • The decorated function must have Type annotations.
  • Each time the decorated function is called, pedantic checks that the passed arguments and the return value of the function matches the given type annotations. As a consequence, the arguments are also checked for None, because None is only a valid argument, if it is annotated via typing.Optional.
  • If the decorated function has a docstring which lists the arguments, the docstring is parsed and compared with the type annotations. In other words, pedantic ensures that the docstring is everytime up-to-date. Currently, only docstrings in the Google style are supported.

In a nutshell: @pedantic raises an PedanticException if one of the following happened:

  • The decorated function is called with positional arguments.
  • The function has no type annotation for their return type or one or more parameters do not have type annotations.
  • A type annotation is incorrect.
  • A type annotation misses type arguments, e.g. typing.List instead of typing.List[int].
  • The documented arguments do not match the argument list or their type annotations.

List of all decorators in this package

Dependencies

Outside the Python standard library, the following dependencies are used:

Contributing

Feel free to contribute by submitting a pull request :)

Acknowledgments

Risks and side effects

The usage of decorators may affect the performance of your application. For this reason, I would highly recommend you to disable the decorators if your code runs in a productive environment. You can disable pedantic by set an environment variable:

export ENABLE_PEDANTIC=0

You can also disable or enable the environment variables in your Python project by calling a method:

from pedantic import enable_pedantic, disable_pedantic
enable_pedantic()

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pedantic-1.2.2.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

pedantic-1.2.2-py3-none-any.whl (42.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page