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.

from guardtypes import enforce


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

from guardtypes import enforce


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

    @staticmethod
    @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.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: guardtypes-1.0.2.tar.gz
  • Upload date:
  • Size: 5.5 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.2.tar.gz
Algorithm Hash digest
SHA256 76d49b1e084733dbfdf56f5a3b9efcf3441f93e52292337cf0b01b1e9c8663db
MD5 d036ee74a1addbd7d82c5ca0b5cd2229
BLAKE2b-256 8f3a8955adaf5c0b383f39ea4402fa2bb527fb9ef101db84497db67e3d1d0c00

See more details on using hashes here.

File details

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

File metadata

  • Download URL: guardtypes-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b54cdce8c894510e6677c9c83d767c2a765c2d61fbf976e8167b4985177ee09f
MD5 d3e5b1ac51808ba781bdb7069165a9ce
BLAKE2b-256 ae06f12d8cf86b39e3da0ceda853e9665ce9c87ab360bfaf3a5a6a6c4459ffe3

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