Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composable failure raiser objects #43

Open
smarie opened this issue Sep 14, 2019 · 0 comments
Open

Composable failure raiser objects #43

smarie opened this issue Sep 14, 2019 · 0 comments

Comments

@smarie
Copy link
Owner

smarie commented Sep 14, 2019

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.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant