def __init__(self, request, name=None, cache=None): self.name = name if name is not None else request.__name__ self.request = request self.cache = cache self.validate_name() self.annotation = Type.get_type( self.request.__annotations__.get('return'))
def extra_arguments(self): extras = [] args_spec = inspect.getfullargspec(self.request) args = args_spec.args if self.env_arg in args: args.remove(self.env_arg) if self.session_arg in args: args.remove(self.session_arg) defaults = list(args_spec.defaults or []) for argument in args[::-1]: annotation = Type.get_type(args_spec.annotations.get(argument)) if defaults: arg = DefaultValueArgument(argument, annotation, defaults.pop()) else: arg = NoDefaultValueArgument(argument, annotation) extras.append(arg) return extras[::-1]
def __init__(self, value, annotation=None, cached=False): self._value = value self.cached = cached if annotation is None: annotation = Type() self.annotation = annotation
def testing_casting_to_invalid_type_passes_value_unchanged(value): t = Type.get_type(object) assert t.cast(value) == value
def test_create_sectet_type(): t = Type.get_type(SecretType) assert str(t) == 'secret' assert bool(t) == True assert isinstance(t, SecretType)
def test_create_unsupported_type(cls): t = Type.get_type(cls) assert isinstance(t, Type) assert str(t) == '' assert bool(t) == False
def test_create_datetime_type(): t = Type.get_type(datetime) assert isinstance(t, DatetimeType) assert str(t) == 'datetime' assert bool(t) == True
def test_create_boolean_typ(): t = Type.get_type(bool) assert isinstance(t, BooleanType) assert str(t) == 'bool' assert bool(t) == True
def test_create_primitive_type(cls): t = Type.get_type(cls) assert isinstance(t, PrimitiveType) assert str(t) == cls.__name__ assert bool(t) == True
def test_no_default_value_argument_without_type_to_string(): a = NoDefaultValueArgument('name', Type()) assert str(a) == 'name'
def test_no_default_value_argument_returns_given_value(): a = NoDefaultValueArgument('name', Type()) assert a.get_value({'name': 3.14}) == 3.14
def test_default_value_argument_without_type_to_string(): a = DefaultValueArgument('name', Type(), 3.14) assert str(a) == 'name [default: 3.14]'
def test_returns_given_result(): r = Result(3.14, Type()) assert r.value == 3.14
def test_format_none(): r = Result(None, Type(), False) assert escape_ansi(r) == 'None'