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-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

guardtypes-0.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for guardtypes-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f9aa69e5f4cf5a8c0be41635acd0f6c868960f0f6e6cbfa3caf9ec237e729b8e
MD5 994e72218c22ecb3f4f17c711aab420f
BLAKE2b-256 c171e14ff3302714e4adda67c9abc3b211d58816011af6bbedc37971dae848c7

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for guardtypes-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a72d958056076faa5ef71137c9f7d79682add5c459b5d8fe4351f4d2ab21dc22
MD5 72dc15f13f1053f81421a8117329700d
BLAKE2b-256 fc3bd6622ee2a0f1031f336f8f7ee9f81b26801fa2e9ecd6688f3e51160e7656

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