From 7b275cdd69d5e6af0b95f4e6440d4664fe1c3674 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Wed, 8 Sep 2021 06:54:24 +0200 Subject: qapi: Fix a botched type annotation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mypy is unhappy: $ mypy --config-file=scripts/qapi/mypy.ini `git-ls-files scripts/qapi/\*py` scripts/qapi/common.py:208: error: Function is missing a return type annotation scripts/qapi/common.py:227: error: Returning Any from function declared to return "str" Messed up in commit ccea6a8637 "qapi: Factor common recursion out of cgen_ifcond(), docgen_ifcond()". Tidy up. Signed-off-by: Markus Armbruster Message-Id: <20210908045428.2689093-2-armbru@redhat.com> Reviewed-by: Marc-André Lureau --- scripts/qapi/common.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'scripts/qapi/common.py') diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 5f8f76e5b2..c4d11b9637 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -205,7 +205,8 @@ def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]], cond_fmt: str, not_fmt: str, all_operator: str, any_operator: str) -> str: - def do_gen(ifcond: Union[str, Dict[str, Any]], need_parens: bool): + def do_gen(ifcond: Union[str, Dict[str, Any]], + need_parens: bool) -> str: if isinstance(ifcond, str): return cond_fmt % ifcond assert isinstance(ifcond, dict) and len(ifcond) == 1 -- cgit 1.4.1 From 916fca17c7445c17d4be37610c80c1f68784ef28 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Wed, 8 Sep 2021 06:54:25 +0200 Subject: qapi: Drop Indentation.__bool__() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Intentation.__bool__() is not worth its keep: it has just one user, which can just as well check .__str__() instead. Signed-off-by: Markus Armbruster Message-Id: <20210908045428.2689093-3-armbru@redhat.com> Reviewed-by: Marc-André Lureau --- scripts/qapi/common.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'scripts/qapi/common.py') diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index c4d11b9637..1d62c27fb7 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -142,10 +142,6 @@ class Indentation: """Return the current indentation as a string of spaces.""" return ' ' * self._level - def __bool__(self) -> bool: - """True when there is a non-zero indentation.""" - return bool(self._level) - def increase(self, amount: int = 4) -> None: """Increase the indentation level by ``amount``, default 4.""" self._level += amount @@ -169,8 +165,9 @@ def cgen(code: str, **kwds: object) -> str: Obey `indent`, and strip `EATSPACE`. """ raw = code % kwds - if indent: - raw = re.sub(r'^(?!(#|$))', str(indent), raw, flags=re.MULTILINE) + pfx = str(indent) + if pfx: + raw = re.sub(r'^(?!(#|$))', pfx, raw, flags=re.MULTILINE) return re.sub(re.escape(EATSPACE) + r' *', '', raw) -- cgit 1.4.1 From e2ff14a5740c2fe3714a56221792b6d74bc64c08 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Wed, 8 Sep 2021 06:54:26 +0200 Subject: qapi: Bury some unused code in class Indentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit .__int__() has never been used. Drop it. .decrease() raises ArithmeticError when asked to decrease indentation level below zero. Nothing catches it. It's a programming error. Dumb down to assert. Signed-off-by: Markus Armbruster Message-Id: <20210908045428.2689093-4-armbru@redhat.com> Reviewed-by: Marc-André Lureau --- scripts/qapi/common.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'scripts/qapi/common.py') diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 1d62c27fb7..489273574a 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -132,9 +132,6 @@ class Indentation: def __init__(self, initial: int = 0) -> None: self._level = initial - def __int__(self) -> int: - return self._level - def __repr__(self) -> str: return "{}({:d})".format(type(self).__name__, self._level) @@ -148,9 +145,7 @@ class Indentation: def decrease(self, amount: int = 4) -> None: """Decrease the indentation level by ``amount``, default 4.""" - if self._level < amount: - raise ArithmeticError( - f"Can't remove {amount:d} spaces from {self!r}") + assert amount <= self._level self._level -= amount -- cgit 1.4.1