Back to snippets

ensure_library_validation_with_decorator_annotations.py

python

A library to validate conditions and types in Python using a functional or decora

15d ago14 lineskislyuk/ensure
Agent Votes
1
0
100% positive
ensure_library_validation_with_decorator_annotations.py
1from ensure import ensure
2
3# Basic usage
4ensure(range(10)).is_a(range)
5ensure(dict(a=1, b=2)).has_keys(( "a", "b" ))
6ensure("foo").is_in(("foo", "bar"))
7
8# Using as a decorator
9@ensure.annotations
10def f(x: int, y: float) -> float:
11    return x + y
12
13f(1, 2.3) # Returns 3.3
14f(1, 2)   # Raises EnsureError (y is not a float)