From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org, Markus Armbruster <armbru@redhat.com>
Cc: "John Snow" <jsnow@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Eduardo Habkost" <ehabkost@redhat.com>,
"Cleber Rosa" <crosa@redhat.com>
Subject: [PATCH 25/37] qapi/gen.py: add notational type hints
Date: Tue, 15 Sep 2020 18:40:15 -0400 [thread overview]
Message-ID: <20200915224027.2529813-26-jsnow@redhat.com> (raw)
In-Reply-To: <20200915224027.2529813-1-jsnow@redhat.com>
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/gen.py | 105 +++++++++++++++++++++++---------------------
1 file changed, 56 insertions(+), 49 deletions(-)
diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py
index fc46813d1a..c87c84e7c9 100644
--- a/scripts/qapi/gen.py
+++ b/scripts/qapi/gen.py
@@ -15,6 +15,7 @@
import errno
import os
import re
+from typing import Dict, Generator, List, Optional, Tuple
from .common import (
c_fname,
@@ -24,32 +25,34 @@
guardstart,
mcgen,
)
-from .schema import QAPISchemaVisitor
+from .schema import (
+ QAPISchemaVisitor,
+)
+from .source import QAPISourceInfo
class QAPIGen:
-
- def __init__(self, fname):
+ def __init__(self, fname: Optional[str]):
self.fname = fname
self._preamble = ''
self._body = ''
- def preamble_add(self, text):
+ def preamble_add(self, text: str) -> None:
self._preamble += text
- def add(self, text):
+ def add(self, text: str) -> None:
self._body += text
- def get_content(self):
+ def get_content(self) -> str:
return self._top() + self._preamble + self._body + self._bottom()
- def _top(self):
+ def _top(self) -> str:
return ''
- def _bottom(self):
+ def _bottom(self) -> str:
return ''
- def write(self, output_dir):
+ def write(self, output_dir: str) -> None:
# Include paths starting with ../ are used to reuse modules of the main
# schema in specialised schemas. Don't overwrite the files that are
# already generated for the main schema.
@@ -74,7 +77,7 @@ def write(self, output_dir):
f.close()
-def _wrap_ifcond(ifcond, before, after):
+def _wrap_ifcond(ifcond: List[str], before: str, after: str) -> str:
if before == after:
return after # suppress empty #if ... #endif
@@ -91,40 +94,38 @@ def _wrap_ifcond(ifcond, before, after):
class QAPIGenCCode(QAPIGen):
-
- def __init__(self, fname):
+ def __init__(self, fname: Optional[str]):
super().__init__(fname)
- self._start_if = None
+ self._start_if: Optional[Tuple[List[str], str, str]] = None
- def start_if(self, ifcond):
+ def start_if(self, ifcond: List[str]) -> None:
assert self._start_if is None
self._start_if = (ifcond, self._body, self._preamble)
- def end_if(self):
+ def end_if(self) -> None:
assert self._start_if
self._wrap_ifcond()
self._start_if = None
- def _wrap_ifcond(self):
+ def _wrap_ifcond(self) -> None:
self._body = _wrap_ifcond(self._start_if[0],
self._start_if[1], self._body)
self._preamble = _wrap_ifcond(self._start_if[0],
self._start_if[2], self._preamble)
- def get_content(self):
+ def get_content(self) -> str:
assert self._start_if is None
return super().get_content()
class QAPIGenC(QAPIGenCCode):
-
- def __init__(self, fname, blurb, pydoc):
+ def __init__(self, fname: str, blurb: str, pydoc: str):
super().__init__(fname)
self._blurb = blurb
self._copyright = '\n * '.join(re.findall(r'^Copyright .*', pydoc,
re.MULTILINE))
- def _top(self):
+ def _top(self) -> str:
return mcgen('''
/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
@@ -140,7 +141,7 @@ def _top(self):
''',
blurb=self._blurb, copyright=self._copyright)
- def _bottom(self):
+ def _bottom(self) -> str:
return mcgen('''
/* Dummy declaration to prevent empty .o file */
@@ -150,16 +151,16 @@ def _bottom(self):
class QAPIGenH(QAPIGenC):
-
- def _top(self):
+ def _top(self) -> str:
return super()._top() + guardstart(self.fname)
- def _bottom(self):
+ def _bottom(self) -> str:
return guardend(self.fname)
@contextmanager
-def ifcontext(ifcond, *args):
+def ifcontext(ifcond: List[str],
+ *args: QAPIGenCCode) -> Generator[None, None, None]:
"""A 'with' statement context manager to wrap with start_if()/end_if()
*args: any number of QAPIGenCCode
@@ -185,15 +186,17 @@ def ifcontext(ifcond, *args):
class QAPIGenDoc(QAPIGen):
-
- def _top(self):
+ def _top(self) -> str:
return (super()._top()
+ '@c AUTOMATICALLY GENERATED, DO NOT MODIFY\n\n')
class QAPISchemaMonolithicCVisitor(QAPISchemaVisitor):
-
- def __init__(self, prefix, what, blurb, pydoc):
+ def __init__(self,
+ prefix: str,
+ what: str,
+ blurb: str,
+ pydoc: str):
self._prefix = prefix
self._what = what
self._genc = QAPIGenC(self._prefix + self._what + '.c',
@@ -201,38 +204,42 @@ def __init__(self, prefix, what, blurb, pydoc):
self._genh = QAPIGenH(self._prefix + self._what + '.h',
blurb, pydoc)
- def write(self, output_dir):
+ def write(self, output_dir: str) -> None:
self._genc.write(output_dir)
self._genh.write(output_dir)
class QAPISchemaModularCVisitor(QAPISchemaVisitor):
-
- def __init__(self, prefix, what, user_blurb, builtin_blurb, pydoc):
+ def __init__(self,
+ prefix: str,
+ what: str,
+ user_blurb: str,
+ builtin_blurb: Optional[str],
+ pydoc: str):
self._prefix = prefix
self._what = what
self._user_blurb = user_blurb
self._builtin_blurb = builtin_blurb
self._pydoc = pydoc
- self._genc = None
- self._genh = None
- self._module = {}
- self._main_module = None
+ self._genc: Optional[QAPIGenC] = None
+ self._genh: Optional[QAPIGenH] = None
+ self._module: Dict[Optional[str], Tuple[QAPIGenC, QAPIGenH]] = {}
+ self._main_module: Optional[str] = None
@staticmethod
- def _is_user_module(name):
+ def _is_user_module(name: Optional[str]) -> bool:
return name is not None and not name.startswith('./')
@staticmethod
- def _is_builtin_module(name):
+ def _is_builtin_module(name: Optional[str]) -> bool:
return not name
- def _module_dirname(self, what, name):
+ def _module_dirname(self, what: str, name: Optional[str]) -> str:
if self._is_user_module(name):
return os.path.dirname(name)
return ''
- def _module_basename(self, what, name):
+ def _module_basename(self, what: str, name: Optional[str]) -> str:
ret = '' if self._is_builtin_module(name) else self._prefix
if self._is_user_module(name):
basename = os.path.basename(name)
@@ -244,27 +251,27 @@ def _module_basename(self, what, name):
ret += re.sub(r'-', '-' + name + '-', what)
return ret
- def _module_filename(self, what, name):
+ def _module_filename(self, what: str, name: Optional[str]) -> str:
return os.path.join(self._module_dirname(what, name),
self._module_basename(what, name))
- def _add_module(self, name, blurb):
+ def _add_module(self, name: Optional[str], blurb: str) -> None:
basename = self._module_filename(self._what, name)
genc = QAPIGenC(basename + '.c', blurb, self._pydoc)
genh = QAPIGenH(basename + '.h', blurb, self._pydoc)
self._module[name] = (genc, genh)
self._genc, self._genh = self._module[name]
- def _add_user_module(self, name, blurb):
+ def _add_user_module(self, name: str, blurb: str) -> None:
assert self._is_user_module(name)
if self._main_module is None:
self._main_module = name
self._add_module(name, blurb)
- def _add_system_module(self, name, blurb):
+ def _add_system_module(self, name: Optional[str], blurb: str) -> None:
self._add_module(name and './' + name, blurb)
- def write(self, output_dir, opt_builtins=False):
+ def write(self, output_dir: str, opt_builtins: bool = False) -> None:
for name in self._module:
if self._is_builtin_module(name) and not opt_builtins:
continue
@@ -272,13 +279,13 @@ def write(self, output_dir, opt_builtins=False):
genc.write(output_dir)
genh.write(output_dir)
- def _begin_system_module(self, name):
+ def _begin_system_module(self, name: None) -> None:
pass
- def _begin_user_module(self, name):
+ def _begin_user_module(self, name: str) -> None:
pass
- def visit_module(self, name):
+ def visit_module(self, name: Optional[str]) -> None:
if name is None:
if self._builtin_blurb:
self._add_system_module(None, self._builtin_blurb)
@@ -292,7 +299,7 @@ def visit_module(self, name):
self._add_user_module(name, self._user_blurb)
self._begin_user_module(name)
- def visit_include(self, name, info):
+ def visit_include(self, name: str, info: QAPISourceInfo) -> None:
relname = os.path.relpath(self._module_filename(self._what, name),
os.path.dirname(self._genh.fname))
self._genh.preamble_add(mcgen('''
--
2.26.2
next prev parent reply other threads:[~2020-09-15 23:05 UTC|newest]
Thread overview: 98+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-15 22:39 [PATCH 00/37] qapi: static typing conversion, pt1 John Snow
2020-09-15 22:39 ` [PATCH 01/37] python: Require 3.6+ John Snow
2020-09-16 8:42 ` Markus Armbruster
2020-09-16 8:51 ` Daniel P. Berrangé
2020-09-15 22:39 ` [PATCH 02/37] [DO-NOT-MERGE] qapi: add debugging tools John Snow
2020-09-15 22:39 ` [PATCH 03/37] qapi-gen: Separate arg-parsing from generation John Snow
2020-09-15 22:39 ` [PATCH 04/37] qapi: move generator entrypoint into module John Snow
2020-09-16 11:54 ` Markus Armbruster
2020-09-16 14:24 ` John Snow
2020-09-17 7:46 ` Markus Armbruster
2020-09-15 22:39 ` [PATCH 05/37] qapi: Remove wildcard includes John Snow
2020-09-15 22:39 ` [PATCH 06/37] qapi: delint using flake8 John Snow
2020-09-16 12:12 ` Markus Armbruster
2020-09-16 14:29 ` John Snow
2020-09-17 7:54 ` Markus Armbruster
2020-09-17 16:57 ` John Snow
2020-09-18 10:33 ` Markus Armbruster
2020-09-18 18:13 ` John Snow
2020-09-21 7:31 ` Markus Armbruster
2020-09-21 14:50 ` John Snow
2020-09-15 22:39 ` [PATCH 07/37] qapi: add pylintrc John Snow
2020-09-16 12:30 ` Markus Armbruster
2020-09-16 14:37 ` John Snow
2020-09-17 7:58 ` Markus Armbruster
2020-09-17 17:06 ` John Snow
2020-09-15 22:39 ` [PATCH 08/37] qapi/common.py: Remove python compatibility workaround John Snow
2020-09-16 12:34 ` Markus Armbruster
2020-09-16 14:38 ` John Snow
2020-09-15 22:39 ` [PATCH 09/37] qapi/common.py: Add indent manager John Snow
2020-09-16 15:13 ` Markus Armbruster
2020-09-16 22:25 ` John Snow
2020-09-17 8:07 ` Markus Armbruster
2020-09-17 17:18 ` John Snow
2020-09-18 10:55 ` Markus Armbruster
2020-09-18 16:08 ` John Snow
2020-09-21 7:43 ` Markus Armbruster
2020-09-15 22:40 ` [PATCH 10/37] qapi/common.py: delint with pylint John Snow
2020-09-17 14:15 ` Markus Armbruster
2020-09-17 17:48 ` John Snow
2020-09-18 11:03 ` Markus Armbruster
2020-09-15 22:40 ` [PATCH 11/37] qapi/common.py: Replace one-letter 'c' variable John Snow
2020-09-17 14:17 ` Markus Armbruster
2020-09-17 17:51 ` John Snow
2020-09-15 22:40 ` [PATCH 12/37] qapi/common.py: check with pylint John Snow
2020-09-15 22:40 ` [PATCH 13/37] qapi/common.py: add notational type hints John Snow
2020-09-17 14:32 ` Markus Armbruster
2020-09-17 18:18 ` John Snow
2020-09-17 20:06 ` John Snow
2020-09-18 11:14 ` Markus Armbruster
2020-09-18 15:24 ` John Snow
2020-09-15 22:40 ` [PATCH 14/37] qapi/common.py: Move comments into docstrings John Snow
2020-09-17 14:37 ` Markus Armbruster
2020-09-17 18:44 ` John Snow
2020-09-17 19:14 ` Eduardo Habkost
2020-09-17 19:31 ` John Snow
2020-09-24 15:06 ` Markus Armbruster
2020-09-24 16:31 ` John Snow
2020-09-25 7:49 ` Markus Armbruster
2020-09-25 14:07 ` John Snow
2020-09-15 22:40 ` [PATCH 15/37] qapi/common.py: split build_params into new file John Snow
2020-09-17 14:42 ` Markus Armbruster
2020-09-17 18:53 ` John Snow
2020-09-17 19:40 ` John Snow
2020-09-15 22:40 ` [PATCH 16/37] qapi: establish mypy type-checking baseline John Snow
2020-09-18 11:55 ` Markus Armbruster
2020-09-18 14:27 ` John Snow
2020-09-21 8:05 ` Markus Armbruster
2020-09-21 14:41 ` John Snow
2020-09-25 1:18 ` Eduardo Habkost
2020-09-18 19:03 ` John Snow
2020-09-21 8:05 ` Markus Armbruster
2020-09-21 14:46 ` John Snow
2020-09-15 22:40 ` [PATCH 17/37] qapi/events.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 18/37] qapi/events.py: Move comments into docstrings John Snow
2020-09-15 22:40 ` [PATCH 19/37] qapi/commands.py: Don't re-bind to variable of different type John Snow
2020-09-15 22:40 ` [PATCH 20/37] qapi/commands.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 21/37] qapi/commands.py: enable checking with mypy John Snow
2020-09-15 22:40 ` [PATCH 22/37] qapi/source.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 23/37] qapi/source.py: delint with pylint John Snow
2020-09-15 22:40 ` [PATCH 24/37] qapi/gen.py: Fix edge-case of _is_user_module John Snow
2020-09-15 22:40 ` John Snow [this message]
2020-09-15 22:40 ` [PATCH 26/37] qapi/gen.py: Enable checking with mypy John Snow
2020-09-15 22:40 ` [PATCH 27/37] qapi/gen.py: Remove unused parameter John Snow
2020-09-15 22:40 ` [PATCH 28/37] qapi/gen.py: update write() to be more idiomatic John Snow
2020-09-15 22:40 ` [PATCH 29/37] qapi/gen.py: delint with pylint John Snow
2020-09-15 22:40 ` [PATCH 30/37] qapi/introspect.py: Add a typed 'extra' structure John Snow
2020-09-15 22:40 ` [PATCH 31/37] qapi/introspect.py: add _gen_features helper John Snow
2020-09-15 22:40 ` [PATCH 32/37] qapi/introspect.py: create a typed 'Node' data structure John Snow
2020-09-15 22:40 ` [PATCH 33/37] qapi/introspect.py: add notational type hints John Snow
2020-09-15 22:40 ` [PATCH 34/37] qapi/types.py: " John Snow
2020-09-15 22:40 ` [PATCH 35/37] qapi/types.py: remove one-letter variables John Snow
2020-09-15 22:40 ` [PATCH 36/37] qapi/visit.py: remove unused parameters from gen_visit_object John Snow
2020-09-15 22:40 ` [PATCH 37/37] qapi/visit.py: add notational type hints John Snow
2020-09-16 22:33 ` [PATCH 00/37] qapi: static typing conversion, pt1 John Snow
2020-09-17 20:22 ` John Snow
2020-09-18 7:50 ` Markus Armbruster
2020-09-18 13:07 ` Philippe Mathieu-Daudé
2020-09-18 14:30 ` John Snow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200915224027.2529813-26-jsnow@redhat.com \
--to=jsnow@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=armbru@redhat.com \
--cc=crosa@redhat.com \
--cc=ehabkost@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).