Skip to content

Composable failure raiser objects #43

@smarie

Description

@smarie

A failure raiser currently consists in

  • some validation callable
  • a failure type (default Failure)
  • an optional overridden help message

Besides we have composition operators (and, or, etc.)

So we could imagine to provide equivalent objects, directly composable with the python operators :

PositiveInt = InstanceOf(int) & GreaterThan(0)

Advantages:

  • we could compose the type hints automatically (to check if for example pychamr follows the type hints of __and__
  • compacity and reuse

Companion needs/features:

  • if we go down that road, we would need an easy way to define a failure raiser. It could be by blending it with the existing compact syntax, for example
IntMultipleOfTwo = InstanceOf(int) & (lambda x: x % 2 == 0, 'x should be a multiple of 2', CustomType)
  • factories could maybe be defined as classes "à la checktypes" ? As :
class MultipleOf(...):
    params = 'nb', 
    base_type = None
    checker = lambda x, nb: x % nb == 0
    help_msg = "x should be multiple of {nb}"

Notes:

  • we should probably not wish these objects to be classes (like in checktypes). That is maybe more elegant conceptualy but would definitely be less efficient in terms of python (metaclasses, overriding instance_check etc.)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions