Skip to main content

This is a simple decorator that will allow you to implement strict type checking in your everyday cPython functions based on the type hint of the function parameters.

Project description

Strictpy

Publish to PyPi Publish to Test PyPi

This library provides a simple decorator that allows you to enforce strict type checking in you everyday cPython functions based on the type hint of the function parameters.

What to expect from this decorator?

When you use this decorator in your function, you have to keep in mind a couple of things-

  1. This strictly requires type hinting your function parameters & return value
  2. If you don't provide type hinting, you'll see some custom exceptions being raised named TypeHintMissingError
  3. You can skip the return value strict check, if you want. But function parameters are always type checked
  4. If you pass type hinting but the hint don't match with the value type, you'll get a TypeMismatchError
  5. You must call your function with Keyword arguments, otherwise you'll get a PositionalArgumentsNotAllowedException

Each error/exception will have helpful message to help you identify what you need to do.

Installation

You can simply install the latest version with this command:

pip install strictpy

If you want any specific older version:

pip install strictpy==1.0.0

Usage

The usage is pretty simple and intuitive. We just need to have our functions decorated with @strict.

Let's start with simple example:

from strictpy import strict

@strict
def some_function(x: int, y: int) -> int:
    return x * y

some_function(x=5, y=6)

This will lead to execution of the function with no visible difference as all the arguments and the return value is type hinted. Also, the function is called with Keyword arguments, which is a must if you use @strict decorator.

Now, let's see an example what happens if type hints are missing:

from strictpy import strict

@strict
def some_function(x: int, y: int):
    return x * y

some_function(x=5, y=6)

If you run this code, you'll get the following exception because the return type hint is missing:

...
File "/Users/ishmam/PycharmProjects/strict-py/src/strictpy/helpers.py", line 14, in ensure_return_type_hint
    raise TypeHintMissingError("return type hint cannot be empty.")
strictpy.exceptions.TypeHintMissingError: return type hint cannot be empty.

Although, you can skip check for return type check like this:

from strictpy import strict

@strict(force_return_type_check=False)
def some_function(x: int, y: int):
    return x * y

some_function(x=5, y=6)

This will ignore the return value missing type hint.

In this last example you'll see the exception that is raised when the function is called with Positional arguments:

from strictpy import strict

@strict(force_return_type_check=False)
def some_function(x: int, y: int):
    return x * y

some_function(5, y=6) # x is positional

Raised exception:

File "/Users/ishmam/PycharmProjects/strict-py/src/strictpy/helpers.py", line 33, in ensure_keyword_only_arguments
    raise PositionalArgumentsNotAllowedException(
strictpy.exceptions.PositionalArgumentsNotAllowedException: Only keyword arguments are expected, 1 were passed as positional arguments.

Project details


Download files

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

Source Distribution

strictpy-1.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strictpy-1.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file strictpy-1.1.0.tar.gz.

File metadata

  • Download URL: strictpy-1.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for strictpy-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c7bb57dd779d3a582eddeed0c5e58a2843b833828f20e7704f0ccb83f38c1878
MD5 5ea5f21d65fe656f26f375a9e8f406bf
BLAKE2b-256 f78a1023bff7dc231910502b6612cf5e98006890414af014362fe3fceafb4634

See more details on using hashes here.

File details

Details for the file strictpy-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: strictpy-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for strictpy-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5d76de38e31a544208447634b50f8c5d5d0b0644b0647c07afe927b6a91de7b1
MD5 88abb66e87c7e8ebfa70772b1d31faaf
BLAKE2b-256 307a06d57a314de7b9be39ee31db4026a7fc30151241a746aafcf64253d42a80

See more details on using hashes here.

Supported by

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