[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 13/22] qapi/parser: [RFC] overload the return type of get_expr
From: |
John Snow |
Subject: |
[PATCH 13/22] qapi/parser: [RFC] overload the return type of get_expr |
Date: |
Wed, 21 Apr 2021 23:07:11 -0400 |
Teach mypy that there are two possible return types here: either an
Expression, or ... something else.
Not a SLOC reduction, but it does remove an assertion. It also isn't
much safer than a cast, mypy has no insight into if overloads are true
or not. It's on the honor system.
I thought I'd demonstrate its use, though.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/parser.py | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index f2b57d5642a..cbdddc344e7 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -23,6 +23,7 @@
Optional,
Set,
Union,
+ overload,
)
from .common import match_nofail
@@ -95,8 +96,7 @@ def _parse(self) -> None:
self.docs.append(cur_doc)
continue
- expr = self.get_expr(False)
- assert isinstance(expr, dict) # Guaranteed when nested=False
+ expr = self.get_expr()
if 'include' in expr:
self.reject_expr_doc(cur_doc)
@@ -304,8 +304,15 @@ def get_values(self) -> List[object]:
raise QAPIParseError(self, "expected ',' or ']'")
self.accept()
+ @overload
+ # No nesting, must be an Expression.
+ def get_expr(self) -> Expression: ...
+
+ @overload
+ # Possibly nested, might be anything.
+ def get_expr(self, nested: bool) -> _ExprValue: ...
+
def get_expr(self, nested: bool = False) -> _ExprValue:
- # TODO: Teach mypy that nested=False means the retval is a Dict.
expr: _ExprValue
if self.tok != '{' and not nested:
raise QAPIParseError(self, "expected '{'")
--
2.30.2
- [PATCH 10/22] qapi/parser: Fix typing of token membership tests, (continued)
- [PATCH 14/22] qapi/parser: Remove superfluous list constructor, John Snow, 2021/04/21
- [PATCH 17/22] CHECKPOINT, John Snow, 2021/04/21
- [PATCH 19/22] qapi: [WIP] Add type ignores for qapidoc.py, John Snow, 2021/04/21
- [PATCH 20/22] qapi: [WIP] Import QAPIDoc from qapidoc Signed-off-by: John Snow <address@hidden>, John Snow, 2021/04/21
- [PATCH 22/22] qapi: [WIP] Enable linters on parser.py, John Snow, 2021/04/21
- [PATCH 21/22] qapi: [WIP] Add QAPIDocError, John Snow, 2021/04/21
- [PATCH 13/22] qapi/parser: [RFC] overload the return type of get_expr,
John Snow <=
- [PATCH 18/22] qapi: [WIP] Rip QAPIDoc out of parser.py, John Snow, 2021/04/21