Skip to main content

Enforces type annotations at runtime

Project description

guardtypes

guardtypes is a Python library that enforces type annotations at runtime. It provides a decorator, enforce, that checks the types of function arguments and return values against their type hints, raising a TypeError if a mismatch is found.

Installation

To install guardtypes, use pip:

pip install guardtypes

Usage

Basic Example

The enforce decorator can be used to enforce type annotations on function arguments and return values.

import guardtypes


@guardtypes.enforce
def add(a: int, b: int) -> int:
    return a + b


# This will raise a TypeError because 'b' is not an int
add(1, '2')

Handling Local Context

The guardtypes decorator also handles local context, allowing you to use class types within the same function.

import guardtypes


class Cat:
    def __init__(self, name: str):
        self.name = name

    @staticmethod
    @guardtypes.enforce
    def create(name: str) -> 'Cat':
        return Cat(name)


# This will create an instance of Cat
cat = Cat.create("Whiskers")

# This will raise a TypeError because 'name' should be a str
cat = Cat.create(123)

License

This project is licensed under the MIT License.

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

guardtypes-1.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

guardtypes-1.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file guardtypes-1.0.1.tar.gz.

File metadata

  • Download URL: guardtypes-1.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for guardtypes-1.0.1.tar.gz
Algorithm Hash digest
SHA256 be07c6a60af5fada6df1656e7f7c596ff128a2e3ba54d5262fd3f26061fe8fa9
MD5 244db2b96febdfdb372316d16945ab47
BLAKE2b-256 fa55d9d5264186d2361e88d24b9210442cea034c11a96673fa5b3f297b640c34

See more details on using hashes here.

File details

Details for the file guardtypes-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: guardtypes-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for guardtypes-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd772d5dab881e1e9063e9ccb459728f6af05d47e3f0e71b86833d597a044a84
MD5 a3ab1921923f9148f1fb55c36e61d7ac
BLAKE2b-256 2de0d8c428c4834bff7e285f3c31a36d7778c2b69517e6a66e61dde109603c2f

See more details on using hashes here.

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