RussBaz / enforce

Python 3.5+ runtime type checking for integration testing and data validation
543 stars 21 forks source link

Add Configuration #10

Closed TheDataLeek closed 7 years ago

TheDataLeek commented 8 years ago

It would be nice to be able to configure some basic settings globally, rather than having to set them on a per-function basis. This would extend #7 and #4.

Possible syntax is

# set recursion limit to 10
# do not disable
enforce.config(recursion_limit=10, disable=False)

This would allow to build programs where type checking can be enabled through command line arguments.

I'm not sure how the code to implement this would work, but I can start looking into it. It might not also hurt to have "tagged" functions so that you can configure groups of functions at a time. For example

import enforce
from enforce import runtime_validation

@runtime_validation(group='foo')
def foo1(a:str)->str:
    return a
@runtime_validation(group='foo')
def foo2(a:str)->str:
    return a
@runtime_validation(group='bar')
def bar(a:str)->str:
    return a

# Disable all "foo" group checks
enforce.config(disable=False)
enforce.group('foo', disable=True)

I'm open to suggestions on syntax, especially for the function group config.

TheDataLeek commented 8 years ago

Further detailed in the wiki page Configuration.

RussBaz commented 7 years ago

The updated configuration is pretty much there in the dev branch now.

I will post an update about the proposed syntax soon here.

RussBaz commented 7 years ago

This is a 'config' function definition: config(options: Dict, reset: bool=False) And this is a default configuration merged with an 'options' dictionary on each call: (None means leave the previous value unchanged)

default_options = {
    # Global enforce.py on/off switch
    'enabled': None,
    # Group related settings
    'groups': {
        # Dictionary of type {<name: str>: <status: bool>}
        # Sets the status of specified groups
        # Enable - True, disabled - False, do not change - None
        'set': {},
        # Sets the status of all groups to False before updating
        'disable_previous': False,
        # Sets the status of all groups to True before updating
        'enable_previous': False,
        # Deletes all the existing groups before updating
        'clear_previous': False,
        # Updating the default group status - default group is not affected by other settings
        'default': None
    },
    # Sets the type checking mode
    # Available options: 'invariant', 'covariant', 'contravariant', 'bivariant' and None
    'mode': None
    }

I think this general issue is now closed. Details and specifics should have their own issues.