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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: guardtypes-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 7984c73e70e71a7e5bc267550941acc5c6930f2b42f4bf502379e72c354d2cac
MD5 7f532dadb2114ec7bcc7b33f22a22f6d
BLAKE2b-256 a9a3ae0078557617132fc3bfa19157782589f29a49e757acbbfd2cd81a260c35

See more details on using hashes here.

File details

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

File metadata

  • Download URL: guardtypes-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 026151d19e5af49a46dac2c1be25a30cc93192843f872db2953c68eb1ed08cc8
MD5 cf2e89169a3e5dd74976cbb7ad33d246
BLAKE2b-256 6ed52a62bfb40079188b193abc81647ee11c4fb297e38b42b52a43a69be9fc81

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