From nobody Sun Nov 16 16:46:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600815443; cv=none; d=zohomail.com; s=zohoarc; b=JZA4iUAQMtio+h0K9iuda1ftfSeLQimoCiqo+gPzT+nkqadL5keZso3AXxDarvR3dGhGee2QxU+I7ehBZ2T4v0XUYBiqx0NNbLaqTRWNYuh2REeEOIqn1QZiH5Bi88uxS/aEUhJd8nzQyfMKb3+XTiS8CtZwWdOG7ItH75zURJ4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600815443; 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=zYOmOo9imBbda8qkO2IraSrwFKE1gwRI/d1nAeUgSyw=; b=GdcnE7TaQ6sVxDEBaLzGxoh18Fmwq4aCc6LCQD5g6NKEDK1ll44bmrVbOUYiVKhTsRsaJ2RPnMjDjYCtb8JY/Vvx73YggmbI+HBL7xM55lcnyi4yyOM4pgPY/AQ4Sr9W3jB8s3KU+JbiAvGysrbdS/Y1xfFbzIab819ZQfc/zGo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail 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 1600815443946956.0334427247831; Tue, 22 Sep 2020 15:57:23 -0700 (PDT) Received: from localhost ([::1]:58080 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKrDu-0003AA-9r for importer@patchew.org; Tue, 22 Sep 2020 18:57:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58470) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKqti-0006JE-3k for qemu-devel@nongnu.org; Tue, 22 Sep 2020 18:36:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:40265) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKqtd-0007ot-Ll for qemu-devel@nongnu.org; Tue, 22 Sep 2020 18:36:29 -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-572-m6u-WVRzOamqY5d7gc5sxA-1; Tue, 22 Sep 2020 18:35:52 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A5816186DD27; Tue, 22 Sep 2020 22:35:51 +0000 (UTC) Received: from scv.redhat.com (ovpn-119-140.rdu2.redhat.com [10.10.119.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id D255760BF4; Tue, 22 Sep 2020 22:35:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600814184; 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=zYOmOo9imBbda8qkO2IraSrwFKE1gwRI/d1nAeUgSyw=; b=HGyAxBzkMFJ3YnupS5axc4ykTAH3osBlEigzjmUQT3afmgtPc0P+x2g9clvavQhokR8ZSR cmR23LJpNptYJu02dd5WjNktsiqcad7190Qhxd2av3FCoaaF472bRupoBLLlGYFkZXGnFN yBRzrDNlCWl0wCb/nU05OvBNVeys/Tw= X-MC-Unique: m6u-WVRzOamqY5d7gc5sxA-1 From: John Snow To: Markus Armbruster Subject: [PATCH 22/26] qapi/parser.py: add type hint annotations (QAPIDoc) Date: Tue, 22 Sep 2020 18:35:21 -0400 Message-Id: <20200922223525.4085762-23-jsnow@redhat.com> In-Reply-To: <20200922223525.4085762-1-jsnow@redhat.com> References: <20200922223525.4085762-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/22 02:07:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.455, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: Cleber Rosa , John Snow , qemu-devel@nongnu.org, Eduardo Habkost , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Annotations do not change runtime behavior. This commit *only* adds annotations. Signed-off-by: John Snow --- scripts/qapi/parser.py | 68 +++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py index f5f40ffa16..cdb64ffc22 100644 --- a/scripts/qapi/parser.py +++ b/scripts/qapi/parser.py @@ -23,6 +23,7 @@ NamedTuple, Optional, Set, + TYPE_CHECKING, Type, TypeVar, Union, @@ -32,6 +33,10 @@ from .pragma import PragmaError from .source import QAPISourceInfo =20 +if TYPE_CHECKING: + # pylint: disable=3Dcyclic-import + from .schema import QAPISchemaMember, QAPISchemaFeature + =20 _Value =3D Union[List[object], 'OrderedDict[str, object]', str, bool] # Necessary imprecision: mypy does not (yet?) support recursive types; @@ -405,43 +410,43 @@ class QAPIDoc: """ =20 class Section: - def __init__(self, name=3DNone): + def __init__(self, name: Optional[str] =3D None): # optional section name (argument/member or section name) self.name =3D name self.text =3D '' =20 - def append(self, line): + def append(self, line: str) -> None: self.text +=3D line.rstrip() + '\n' =20 class ArgSection(Section): - def __init__(self, name): + def __init__(self, name: Optional[str] =3D None): super().__init__(name) - self.member =3D None + self.member: Optional['QAPISchemaMember'] =3D None =20 - def connect(self, member): + def connect(self, member: 'QAPISchemaMember') -> None: self.member =3D member =20 - def __init__(self, info): + def __init__(self, info: QAPISourceInfo): self.info =3D info - self.symbol =3D None + self.symbol: Optional[str] =3D None self.body =3D QAPIDoc.Section() # dict mapping parameter name to ArgSection - self.args =3D OrderedDict() - self.features =3D OrderedDict() + self.args: 'OrderedDict[str, QAPIDoc.ArgSection]' =3D OrderedDict() + self.features: 'OrderedDict[str, QAPIDoc.ArgSection]' =3D OrderedD= ict() # a list of Section - self.sections =3D [] + self.sections: List[QAPIDoc.Section] =3D [] # the current section self._section =3D self.body self._append_line =3D self._append_body_line =20 - def has_section(self, name): + def has_section(self, name: str) -> bool: """Return True if we have a section with this name.""" for i in self.sections: if i.name =3D=3D name: return True return False =20 - def append(self, line): + def append(self, line: str) -> None: """ Parse a comment line and add it to the documentation. =20 @@ -462,18 +467,18 @@ def append(self, line): line =3D line[1:] self._append_line(line) =20 - def end_comment(self): + def end_comment(self) -> None: self._end_section() =20 @classmethod - def _is_section_tag(cls, name): + def _is_section_tag(cls, name: str) -> bool: return name in ('Returns:', 'Since:', # those are often singular or plural 'Note:', 'Notes:', 'Example:', 'Examples:', 'TODO:') =20 - def _append_body_line(self, line): + def _append_body_line(self, line: str) -> None: """ Process a line of documentation text in the body section. =20 @@ -513,7 +518,7 @@ def _append_body_line(self, line): # This is a free-form documentation block self._append_freeform(line.strip()) =20 - def _append_args_line(self, line): + def _append_args_line(self, line: str) -> None: """ Process a line of documentation text in an argument section. =20 @@ -546,7 +551,7 @@ def _append_args_line(self, line): =20 self._append_freeform(line.strip()) =20 - def _append_features_line(self, line): + def _append_features_line(self, line: str) -> None: name =3D line.split(' ', 1)[0] =20 if name.startswith('@') and name.endswith(':'): @@ -565,7 +570,7 @@ def _append_features_line(self, line): =20 self._append_freeform(line.strip()) =20 - def _append_various_line(self, line): + def _append_various_line(self, line: str) -> None: """ Process a line of documentation text in an additional section. =20 @@ -591,7 +596,10 @@ def _append_various_line(self, line): =20 self._append_freeform(line) =20 - def _start_symbol_section(self, symbols_dict, name): + def _start_symbol_section( + self, + symbols_dict: 'OrderedDict[str, QAPIDoc.ArgSection]', + name: str) -> None: # FIXME invalid names other than the empty string aren't flagged if not name: raise QAPIDocError("invalid parameter name") @@ -602,20 +610,20 @@ def _start_symbol_section(self, symbols_dict, name): self._section =3D QAPIDoc.ArgSection(name) symbols_dict[name] =3D self._section =20 - def _start_args_section(self, name): + def _start_args_section(self, name: str) -> None: self._start_symbol_section(self.args, name) =20 - def _start_features_section(self, name): + def _start_features_section(self, name: str) -> None: self._start_symbol_section(self.features, name) =20 - def _start_section(self, name=3DNone): + def _start_section(self, name: Optional[str] =3D None) -> None: if name in ('Returns', 'Since') and self.has_section(name): raise QAPIDocError("duplicated '%s' section" % name) self._end_section() self._section =3D QAPIDoc.Section(name) self.sections.append(self._section) =20 - def _end_section(self): + def _end_section(self) -> None: if self._section: text =3D self._section.text =3D self._section.text.strip() if self._section.name and (not text or text.isspace()): @@ -623,34 +631,34 @@ def _end_section(self): "empty doc section '%s'" % self._section.name) self._section =3D None =20 - def _append_freeform(self, line): + def _append_freeform(self, line: str) -> None: match =3D re.match(r'(@\S+:)', line) if match: raise QAPIDocError("'%s' not allowed in free-form documentatio= n" % match.group(1)) self._section.append(line) =20 - def connect_member(self, member): + def connect_member(self, member: 'QAPISchemaMember') -> None: if member.name not in self.args: # Undocumented TODO outlaw self.args[member.name] =3D QAPIDoc.ArgSection(member.name) self.args[member.name].connect(member) =20 - def connect_feature(self, feature): + def connect_feature(self, feature: 'QAPISchemaFeature') -> None: if feature.name not in self.features: raise QAPISemError(feature.info, "feature '%s' lacks documentation" % feature.name) self.features[feature.name].connect(feature) =20 - def check_expr(self, expr): + def check_expr(self, expr: 'OrderedDict[str, object]') -> None: if self.has_section('Returns') and 'command' not in expr: raise QAPISemError(self.info, "'Returns:' is only valid for commands") =20 - def check(self): - - def check_args_section(args): + def check(self) -> None: + def check_args_section( + args: 'OrderedDict[str, QAPIDoc.ArgSection]') -> None: bogus =3D [name for name, section in args.items() if not section.member] if bogus: --=20 2.26.2