From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id E3D4D5A026E for ; Wed, 31 May 2023 03:59:04 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4QWC9S2m1gz4x4L; Wed, 31 May 2023 11:58:52 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1685498332; bh=uPO/eAcnGXT+/lowA5E8kB6OZro2d2V1Q8zubaPuk48=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aw9K8HHuibI3IitbyxI3aVJHUju9B3Ni5SeakKs1AxT2N3arOc0m7Wrlx1cpms8Ui 8V+pGNbTw3t8wcOEY5z6G2kfMAGsNSmja2T0uxBEGfXeU6g36r/4nQfeOSUz2MAlgd ManiQOqVO54XQlovZHNH9IMOaIUICAC3Z78VJrTM= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v3 05/20] avocado/tasst: Type checking helpers Date: Wed, 31 May 2023 11:58:34 +1000 Message-Id: <20230531015849.3229596-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230531015849.3229596-1-david@gibson.dropbear.id.au> References: <20230531015849.3229596-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 7XN66TCGXQRTRBOX4M7KMHXRU7WEEHGC X-Message-ID-Hash: 7XN66TCGXQRTRBOX4M7KMHXRU7WEEHGC X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Cleber Rosa , jarichte@redhat.com, David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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 + + +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