Skip to main content

A lightweight library for type checking and type casting annotated vars.

Project description


Annotyped is a simple library with utilities and decorators for type checking and type casting automatically on annotated functions at runtime.

pip install annotyped --user

The basics

# Simple typecheck
def add(a: int, b: int) -> int:
   return a + b

add(1, 2)      # 3
add(2, '10')   # Param 'b' requires type '<class 'int'>', found '<class 'str'>': '10'

# Simple typecast, annotyped has pythons built in types as standard.
# You can add custom ones (shown later)

def add(a: int, b: int) -> str:
   return a + b

add('10', '20')   # '30'
add(1, 2)         # '3'
add('1.1', 2)     # Param 'a' could not convert to '<class 'int'>' from '<class 'str'>': invalid literal for int() with base 10: '1.1'

Custom converters / casters


Convert from a tuple or a str into namedtuple.

import annotyped                                         
import math                                              
from collections import namedtuple                       

Position = namedtuple('Position', 'x, y')                

def position(pos):
   if isinstance(pos, str) and ',' in pos:
      pos = map(int, pos.split(','))
   return Position(*pos)

def diff(p1: position, p2: position):                    
   return math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2)

p1 = (10, 20)
p2 = '20,50'

print( diff(p1, p2) )

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

annotyped-0.2.0.linux-x86_64.tar.gz (4.5 kB view hashes)

Uploaded source

Built Distribution

annotyped-0.2.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page