Skip to content

Instantly share code, notes, and snippets.

@devTechi
Last active February 12, 2021 09:45
Show Gist options
  • Save devTechi/6e633ded72cc83637f34b1a3f4a96984 to your computer and use it in GitHub Desktop.
Save devTechi/6e633ded72cc83637f34b1a3f4a96984 to your computer and use it in GitHub Desktop.
I found the very usefull nullable or non-nullable arguments script on stackoverflow - https://stackoverflow.com/a/53639511. This guy also created a repository with his script: https://github.com/unitedsoftwork/python-not-none. I wanted to use it, but it didn't work with **kwargs. As I am using keyword arguments nearly every time I edited his scr…
def not_none(nullable_parameters=None):
# values given by real_decorator (see below)
def the_actual_test(f, expected_args_with_given, allowed_nullable_args):
has_none = False
bad_parameters = []
for key, value in expected_args_with_given.items():
if (value is None and nullable_parameters is None) or \
(value is None and key not in nullable_parameters):
bad_parameters.append(key)
has_none = True
if has_none:
raise ValueError("[Function '{}' of '{}'] - IMPORTANT: Parameters '{}' cannot be None. ".format(f.__name__, f.__module__, bad_parameters))
# here the code REALLY begins
def not_null_decorator(original_func):
import inspect
has_self = False
# f.__code__.co_varnames --> local variables (not only parameters), see: https://python-reference.readthedocs.io/en/latest/docs/code/varnames.html
# get declared arguments from ogirinal function
argspec = inspect.getargspec(original_func)
if 'self' in argspec.args:
argnames = argspec.args[1:] # no self
has_self = True
else:
argnames = argspec.args
args_dict = dict.fromkeys(argnames)
def get_args(*args, **kwargs):
for arg in args:
if arg in args_dict.keys():
args_dict[arg] = arg
for key, value in kwargs.items():
if key in args_dict.keys():
args_dict[key] = value
return args_dict
def wrapper_with_self(self, *args, **kwargs):
the_actual_test(original_func, get_args(*args, **kwargs), nullable_parameters)
return original_func(self, *args, **kwargs)
def wrapper(*args, **kwargs):
the_actual_test(original_func, get_args(*args, **kwargs), nullable_parameters)
return original_func(*args, **kwargs)
if has_self:
return wrapper_with_self
else:
return wrapper
return not_null_decorator
############################
# usage:
# from .nullable_decorator import not_none
#
# @not_none(nullable_parameters=["nullable_arg1", "nullable_arg2"])
# def some_function(self, nullable_arg1=None, nullable_arg2=None, non_nullable_arg1=None):
# pass
#
# @not_none()
# def some_other_function(self, non_nullable_arg1=None, non_nullable_arg2=None):
# pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment