def function(**trait_types): """ Factory function for type-checking decorators. """ for name, trait_type in trait_types.items(): if TraitType.is_trait_type_class(trait_type): trait_type = TraitType.instantiate_trait_type_class(trait_type) trait_types[name] = trait_type ##trait_types[name] = TraitType.instantiate_trait_type(trait_type) def decorator(fn): """ Decorator that produces a type-checked function. """ def wrapper(*args, **kw): """ Type-checking function wrapper. """ actual_args = _validate_args(fn, trait_types, args) actual_kw = _validate_kw(fn, trait_types, kw) return_value = fn(*actual_args, **actual_kw) return _validate_return_value(fn, trait_types, return_value) wrapper.__doc__ = fn.__doc__ return wrapper return decorator
def harvest_traits(cls, klass): """ Harvest any traits defined in the class' dictionary. Traits can be specified as either:- a) A class derived from 'TraitType' b) An instance of a class derived from 'TraitType' Internally we always store 'TraitType' *instances*, so if a class is found we will call it with no arguments. """ traits = {} for key, value in klass.__dict__.items(): # a) A class derived from 'TraitType' if TraitType.is_trait_type_class(value): traits[key] = TraitType.instantiate_trait_type_class(value) delattr(klass, key) # b) An instance of a class derived from 'TraitType' elif isinstance(value, TraitType): traits[key] = value delattr(klass, key) return traits
def __init__(self, item_type=Any, default_value=Undefined, allow_none=False, **metadata): """ Constructor. 'item_type' can be a trait type, a class/interface or the *name* of a class/interface in the form 'package.module:ClassOrInterface'. """ if default_value is Undefined: default_value = [] super(List, self).__init__(default_value, allow_none, **metadata) # If the item type is a 'TraitType' *class* then instantiate it. if TraitType.is_trait_type_class(item_type): item_type = TraitType.instantiate_trait_type_class(item_type) # For now we assume that if the item type is *not* a trait type, then # it is a class/interface or the *name* of a class/interface. Obviously # we should probably force that here! elif not isinstance(item_type, TraitType): item_type = Instance(item_type) self.item_type = item_type return
def Property(trait_type): """ Property trait type modifier. 'trait_type' can be:- 1) a trait type instance e.g. Property(Int(20)) 2), a trait type class e.g. Property(Int) 3) a class/interface e.g. from package.module import Foo Property(Foo) 4) the *name* of a class/interface e.g. Property('package.module:Foo') """ # If the trait type is a 'TraitType' *class* then instantiate it. if TraitType.is_trait_type_class(trait_type): trait_type = TraitType.instantiate_trait_type_class(trait_type) # For now we assume that if the trait type is *not* a trait type, then # it is a class/interface or the *name* of a class/interface that we can # use to make an 'Instance' trait type. Obviously we should probably force # that here! elif not isinstance(trait_type, TraitType): trait_type = Instance(trait_type) trait_type.getter = property_getter trait_type.setter = property_setter return trait_type