From nobody Tue Feb 10 14:29:34 2026 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621534419; cv=none; d=zohomail.com; s=zohoarc; b=OWBv+lc8MHfpqYpiKxAgwFhdL2AL4H8GCu5ee9xY1WZnp86nEUJChZp31uG7B3yy6xaslqSDo7/AbPgi0a+QtytoUhGwzsxoJGx4lKY68x+IJlnnAJ5pmYIxnKfz3xRrwnq82/5Xj55AmZpSfhIoL3H5CBhKRLrmFvCnwoKh1cQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621534419; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hwtjx5OUopMjJQeTcMhySgii1zl3rOiyfCnfBv7eSNE=; b=NLh6T4Op0qrNMYXVxpRgxLbOEakkoARP9K0YBw0OI6aTg9j+cv5WWmaKAz8u2S4rF46TwFAKp1EWKo++fS7v20GlqwKaatJbiyp+Dh2VdlTVX10MGHPwrvuHVnSoZJchsm3q7eIEdGnZT7EjWZCnb/GQ3KVPUD3C3vS2uD1jwlY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621534419297920.690381657377; Thu, 20 May 2021 11:13:39 -0700 (PDT) Received: from localhost ([::1]:53958 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ljnAu-0007l4-9B for importer2@patchew.org; Thu, 20 May 2021 14:13:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56200) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljmrJ-0002oZ-5B for qemu-devel@nongnu.org; Thu, 20 May 2021 13:53:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:24278) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljmr1-0006aZ-3f for qemu-devel@nongnu.org; Thu, 20 May 2021 13:53:16 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-442-K3pIQRg-M6OYPpd0inoLvA-1; Thu, 20 May 2021 13:53:00 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 748C2107ACC7; Thu, 20 May 2021 17:52:59 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-114-17.ams2.redhat.com [10.36.114.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3E28236D5; Thu, 20 May 2021 17:52:59 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 5BE081132CB1; Thu, 20 May 2021 19:52:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621533182; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hwtjx5OUopMjJQeTcMhySgii1zl3rOiyfCnfBv7eSNE=; b=R2kfXyNYwS0zyvogIy7kdkoRDthNgAqI5UnO+jKM5oiPBF7SCoFoUPW39PxL9JsFfThwI4 GvSFrOL1cCy6WkF/7jSAjsL2zNGMtWid1S8KlXGFO1iWIyYqX5N5QFANbYbqv2+1Xvp6TL kSS2n65dmiphyAeR9kgeRfKV1oQiLac= X-MC-Unique: K3pIQRg-M6OYPpd0inoLvA-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PULL 11/15] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard Date: Thu, 20 May 2021 19:52:52 +0200 Message-Id: <20210520175256.1119684-12-armbru@redhat.com> In-Reply-To: <20210520175256.1119684-1-armbru@redhat.com> References: <20210520175256.1119684-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=armbru@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=armbru@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.39, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, John Snow Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: John Snow TypeGuards wont exist in Python proper until 3.10. Ah well. We can hack up our own by declaring this function to return the type we claim it checks for and using this to safely downcast object -> List[str]. In so doing, I bring this function under _pragma so it can use the 'info' object in its closure. Having done this, _pragma also now no longer needs to take a 'self' parameter, so drop it. To help with line-length, and with the context evident from its new scope, rename the function to the shorter check_list_str(). Signed-off-by: John Snow Message-Id: <20210519183951.3946870-12-jsnow@redhat.com> Reviewed-by: Markus Armbruster Signed-off-by: Markus Armbruster --- scripts/qapi/parser.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py index 9f980f7513..8a58e1228f 100644 --- a/scripts/qapi/parser.py +++ b/scripts/qapi/parser.py @@ -17,6 +17,7 @@ from collections import OrderedDict import os import re +from typing import List =20 from .common import must_match from .error import QAPISemError, QAPISourceError @@ -154,28 +155,29 @@ def _include(include, info, incl_fname, previously_in= cluded): ) from err =20 @staticmethod - def _check_pragma_list_of_str(name, value, info): - if (not isinstance(value, list) - or any([not isinstance(elt, str) for elt in value])): - raise QAPISemError( - info, - "pragma %s must be a list of strings" % name) + def _pragma(name, value, info): + + def check_list_str(name, value) -> List[str]: + if (not isinstance(value, list) or + any([not isinstance(elt, str) for elt in value])): + raise QAPISemError( + info, + "pragma %s must be a list of strings" % name) + return value + + pragma =3D info.pragma =20 - def _pragma(self, name, value, info): if name =3D=3D 'doc-required': if not isinstance(value, bool): raise QAPISemError(info, "pragma 'doc-required' must be boolean") - info.pragma.doc_required =3D value + pragma.doc_required =3D value elif name =3D=3D 'command-name-exceptions': - self._check_pragma_list_of_str(name, value, info) - info.pragma.command_name_exceptions =3D value + pragma.command_name_exceptions =3D check_list_str(name, value) elif name =3D=3D 'command-returns-exceptions': - self._check_pragma_list_of_str(name, value, info) - info.pragma.command_returns_exceptions =3D value + pragma.command_returns_exceptions =3D check_list_str(name, val= ue) elif name =3D=3D 'member-name-exceptions': - self._check_pragma_list_of_str(name, value, info) - info.pragma.member_name_exceptions =3D value + pragma.member_name_exceptions =3D check_list_str(name, value) else: raise QAPISemError(info, "unknown pragma '%s'" % name) =20 --=20 2.26.3