Configuration - RussBaz/enforce GitHub Wiki

Note, this is in-progress on the dev branch, and as a result many features are still being worked on.

There are two levels of configuration, the first being global configuration that applies to all decorators, and the second being decorator arguments that override global configuration.

Configuration Options

These are tentatively as follows.

  • recursion_depth
    • For iterables, how deep to recurse into nested structures.
    • -1 means unlimited recursion
    • 0 just checks that some iterable is of that type, for instance foo: List[str] is just checked that it's a list.
    • 1 checks that the item is of the corresponding type (as 0 does) and also that every element matches its signature
    • etc.
  • iterable_size
    • For iterables, how many items to check
    • 'none' means check no items (as with recursion depth, foo: List[str] with -1 just checks that it is of type List
    • 'first' means check the first item
    • 'all' means check every item
  • group
    • "Tag" decorators and then disable or enable groups of function checks.
  • enable
    • Turn on or off individual decorators or groups of decorators.

Global Configuration

So some notes with this.

Ideal behavior is the decorator being enabled or disabled based on when it's run.

Example:

enforce.config(enable=False)

Decorator Configuration

Example:

@runtime_validation(recursion_depth=0, iterable_size='first', group='best group', enable=True)
def foo(a: List[str]):
    pass

Example

        enforce.config(enable=False)
        enforce.set_group('foo', True)

        @runtime_validation(group='foo')
        def test1(a: typing.List[str]): return a

        @runtime_validation(group='foo')
        def test2(a: typing.List[str]): return a

        @runtime_validation(group='bar')
        def test3(a: typing.List[str]): return a

        with self.assertRaises(RuntimeTypeError):
            test1(5)

        with self.assertRaises(RuntimeTypeError):
            test2(5)

        test3(5)

        enforce.config(enable=True)