experimental type safety for python
the name refers to the herman melville short story: this code would 'prefer not to' run when unexpected types are passed around.
it's similar to the excellent typecheck module, but bartleby tries to:
- provide a more natural location for parameter definitions
- allow for optional parameters
- allow for procedural type checking (ex. ranges)
- allow for per parameter comments that ( with some work ) might be able to be sucked into doc strings
- extend the concept of call safety to the class member / assignment level
see the wiki for details
quick examples
@checkparams
def example( x=Type(str) ):
return x
example("hello") # success!
example(5) # assert: 'int' isn't type 'str'
example() # assert: missing required parameter x
class Example(typedobject):
name= Type(str, "default")
e= Example()
print e.name # prints *instance* variable: "default"
e.name= "bartleby" # success!
e.name= 5 # assert: 'int' isn't type 'str'