Skip to content

Latest commit

 

History

History
89 lines (73 loc) · 6.84 KB

File metadata and controls

89 lines (73 loc) · 6.84 KB

pedantic-python-decorators Coverage Status PyPI version Conda Version Last Commit Stars Open Issues Open PRs

This packages includes many decorators that will make you write cleaner Python code.

The @pedantic decorator - Type checking at runtime

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.

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].

Minimal example

from pedantic import pedantic


@pedantic
def get_sum_of(values: list[int | float]) -> int:
    return sum(values)

get_sum_of(values=[0, 1.2, 3, 5.4])  # this raises the following runtime error: 
# Type hint of return value is incorrect: Expected type <class 'int'> but 10.0 of type <class 'float'> was the return value which does not match.

The @validate decorator

As the name suggests, with @validate you are able to validate the values that are passed to the decorated function.

List of all decorators in this package

List of all mixins in this package

Dependencies

There are no hard dependencies. But if you want to use some advanced features you need to install the following packages:

  • multiprocess if you want to use the @in_subprocess decorator
  • flask if you want to you the request validators which are designed for Flask (see unit tests for examples):
    • FlaskParameter (abstract class)
    • FlaskJsonParameter
    • FlaskFormParameter
    • FlaskPathParameter
    • FlaskGetParameter
    • FlaskHeaderParameter
    • GenericFlaskDeserializer

Contributing

This project is based on poetry and taskfile. Tip: Run task validate before making commits.

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 project by calling a method:

from pedantic import enable_pedantic, disable_pedantic
enable_pedantic()

Issues with compiled Python code

This package is not compatible with compiled source code (e.g. with Nuitka). That's because it uses the inspect module from the standard library which will raise errors like OSError: could not get source code in case of compiled source code.

Don't forget to check out the documentation.