RussBaz / enforce

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

enforce ignore default values #70

Open yitai27 opened 6 years ago

yitai27 commented 6 years ago
from typing import NamedTuple
from enforce import runtime_validation

@runtime_validation
class ArpEntry(NamedTuple):
    mac: str
    ip: str
    iface : str = None
>>> ArpEntry(mac='00:11:22:33:44:55', ip='1.1.1.1', iface='asdf')
ArpEntry(mac='00:11:22:33:44:55', ip='1.1.1.1', iface='asdf')
>>> ArpEntry(mac='00:11:22:33:44:55', ip='1.1.1.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/junk/cortex/venv/lib/python3.6/site-packages/enforce/decorators.py", line 188, in __call__
    data = init_data(*args, **kwargs)
  File "/home/user/junk/cortex/venv/lib/python3.6/site-packages/wrapt/wrappers.py", line 523, in __call__
    args, kwargs)
  File "/home/user/junk/cortex/venv/lib/python3.6/site-packages/enforce/decorators.py", line 104, in universal
    _args, _kwargs, _ = enforcer.validate_inputs(parameters)
  File "/home/user/junk/cortex/venv/lib/python3.6/site-packages/enforce/enforcers.py", line 71, in validate_inputs
    binded_arguments = self.signature.bind(*args, **kwargs)
  File "/usr/lib/python3.6/inspect.py", line 2969, in bind
    return args[0]._bind(args[1:], kwargs)
  File "/usr/lib/python3.6/inspect.py", line 2940, in _bind
    format(arg=param_name)) from None
TypeError: missing a required argument: 'iface'

Without enforce, I can create ArpEntry with only mac and ip, but with enforce I get TypeError.