A lightweight library for type checking and type casting annotated vars.
Project description
annotyped
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
@annotyped.check
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)
@annotyped.cast
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
eg:
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)
@annotyped.cast
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
Built Distribution
Close
Hashes for annotyped-0.2.0.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4847bd8ca4bb7307ea7c330b90c837e9f784496578dcbff187fc051cfdfc06b1 |
|
MD5 | a0155af7a3777876989f1cfc6ab9cca7 |
|
BLAKE2b-256 | 7b0d9c0267a2930c3d47b633078a73178d556ee1c1a9098b8045bcd0c6c28c6d |
Close
Hashes for annotyped-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d81d9a29827e7a7226c39084f3a18ab57524a5979bd2d0d0969e16bfba55960 |
|
MD5 | a556e79aff39c80feb5bbf5f47ef5472 |
|
BLAKE2b-256 | c40e84a30b57fe8ef38d80cdec383f226edb0b20e13e9126fe2c7c64ba28ea59 |