Skip to main content

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.

Release files for strictpy 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strictpy 1.1.0
File Size Uploaded
strictpy-1.1.0.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for strictpy 1.1.0
File Interpreter ABI Platform
strictpy-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / strictpy-1.1.0.tar.gz

Download URL strictpy-1.1.0.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c7bb57dd779d3a582eddeed0c5e58a2843b833828f20e7704f0ccb83f38c1878
BLAKE2b-256 checksum
How to use checksums
f78a1023bff7dc231910502b6612cf5e98006890414af014362fe3fceafb4634
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / strictpy-1.1.0-py3-none-any.whl

Download URL strictpy-1.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d76de38e31a544208447634b50f8c5d5d0b0644b0647c07afe927b6a91de7b1
BLAKE2b-256 checksum
How to use checksums
307a06d57a314de7b9be39ee31db4026a7fc30151241a746aafcf64253d42a80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page