I’m trying to perform good input validity checking on my python code, but I also want it to be succinct. That is, the solution I don’t want to go with is this one:
def some_func(int_arg, str_arg, other_arg):
try:
int_arg = int(int_arg)
except TypeError, ValueError
logging.error("int_arg must respond to int()")
raise TypeError
try:
if str_arg is not None:
str_arg = str(str_arg)
except TypeError
logging.error("Okay, I'm pretty sure this isn't possible, bad example")
raise TypeError
if other_arg not in (VALUE1, VALUE2, VALUE3):
logging.error("other arg must be VALUE1, VALUE2, or VALUE3")
raise TypeError
This is just too much code and to much space to spend on just checking 3 arguments.
My current approach is this:
def some_func(int_arg, str_arg, other_arg):
try:
int_arg = int(int_arg) #int_arg must be an integer
str_arg is None or str_arg = str(str_arg) #str_arg is optional, but must be a string if provided
assert other_arg in (VALUE1, VALUE2, VALUE3)
catch TypeError, ValueError, AssertionError:
logging.error("Bad arguments given to some_func")
throw TypeError
I lose the specificity of my log message, but this is much more succinct and honestly more readable in my opinion.
One thing specifically I’m wondering about is the use of the assert statement. I’ve read that it’s discouraged to use assertions as a way of checking input-validity, but I was wondering if this was a legitimate way to use it.
If not, is there a similar way to perform this check (or do this validation in general) that’s still pretty succinct?
You could invent a decorator that would validate the arguments for you.
This is how the syntax could look like:
This is a naive implementation of the validation decorator factory.
Yes, there’s a bit of function nesting here, but it all makes sense. Let me explain:
validate(narg, converter)to be a function that takes some settings and returns a specific decorator (decorate) which behaves according to these settingsdecorateis then used to decorate a given function (func) that takes some positional arguments by creating a new functionfuncDecoratedthat takes the same arguments asfunc(*args) and is written in terms of input functionfuncas well as the initial settingsnarg,conv.The actual validation happens inside funcDecorated, which…
convdoes),funcwith the altered argument list.To do this for several arguments, we apply
validatemultiple times with different arguments. (It’s possible to rewrite it to only decorate once, but it looks more clear this way IMO.)See it in action: http://ideone.com/vjgIS
Note that
convcan act either…Note that this implementation doesn’t handle keyword arguments.