Add some helper functions for typechecking parameters. We will be using these so that accidentally misusing some of the upcoming test helpers will result in more useful error messages. Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au> --- avocado/tasst/typing.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 avocado/tasst/typing.py diff --git a/avocado/tasst/typing.py b/avocado/tasst/typing.py new file mode 100644 index 0000000..2021b0b --- /dev/null +++ b/avocado/tasst/typing.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 + +# SPDX-License-Identifier: GPL-2.0-or-later +# +# tasst - Test A Simple Socket Transport +# library of test helpers for passt & pasta +# +# tasst/typing.py - Type checking and related helpers +# +# Copyright Red Hat +# Author: David Gibson <david(a)gibson.dropbear.id.au> + + +def typecheck(val, ty_): + if not isinstance(val, ty_): + raise TypeError("Expected {} instead of {}".format(ty_, type(val))) + return val + + +def typecheck_default(val, ty_, default): + if val is None: + return default + return typecheck(val, ty_) -- 2.40.1