[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 01/12] qapi/commands: assert arg_type is not None
From: |
John Snow |
Subject: |
[PATCH v2 01/12] qapi/commands: assert arg_type is not None |
Date: |
Wed, 16 Dec 2020 20:59:16 -0500 |
when boxed is true, expr.py asserts that we must have
arguments. Ultimately, this should mean that if boxed is True, that
arg_type should be defined. Mypy cannot infer this, and does not support
'stateful' type inference, e.g.:
```
if x:
assert y is not None
...
if x:
y.etc()
```
does not work, because mypy does not statefully remember the conditional
assertion in the second block. Help mypy out by creating a new local
that it can track more easily.
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/commands.py | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py
index 50978090b44..71744f48a35 100644
--- a/scripts/qapi/commands.py
+++ b/scripts/qapi/commands.py
@@ -126,6 +126,9 @@ def gen_marshal(name: str,
boxed: bool,
ret_type: Optional[QAPISchemaType]) -> str:
have_args = boxed or (arg_type and not arg_type.is_empty())
+ if have_args:
+ assert arg_type is not None
+ arg_type_c_name = arg_type.c_name()
ret = mcgen('''
@@ -147,7 +150,7 @@ def gen_marshal(name: str,
ret += mcgen('''
%(c_name)s arg = {0};
''',
- c_name=arg_type.c_name())
+ c_name=arg_type_c_name)
ret += mcgen('''
@@ -163,7 +166,7 @@ def gen_marshal(name: str,
ok = visit_check_struct(v, errp);
}
''',
- c_arg_type=arg_type.c_name())
+ c_arg_type=arg_type_c_name)
else:
ret += mcgen('''
ok = visit_check_struct(v, errp);
@@ -193,7 +196,7 @@ def gen_marshal(name: str,
ret += mcgen('''
visit_type_%(c_arg_type)s_members(v, &arg, NULL);
''',
- c_arg_type=arg_type.c_name())
+ c_arg_type=arg_type_c_name)
ret += mcgen('''
visit_end_struct(v, NULL);
--
2.26.2
- [PATCH v2 00/12] qapi: static typing conversion, pt1.5, John Snow, 2020/12/16
- [PATCH v2 01/12] qapi/commands: assert arg_type is not None,
John Snow <=
- [PATCH v2 02/12] qapi/events: fix visit_event typing, John Snow, 2020/12/16
- [PATCH v2 03/12] qapi/main: handle theoretical None-return from re.match(), John Snow, 2020/12/16
- [PATCH v2 10/12] tests/qapi-schema: Add quotes to module name in test output, John Snow, 2020/12/16
- [PATCH v2 04/12] qapi/gen: assert that _start_if is not None in _wrap_ifcond, John Snow, 2020/12/16
- [PATCH v2 08/12] qapi/gen: write _genc/_genh access shims, John Snow, 2020/12/16
- [PATCH v2 12/12] qapi: enable strict-optional checks, John Snow, 2020/12/16
- [PATCH v2 07/12] qapi/schema: make QAPISourceInfo mandatory, John Snow, 2020/12/16
- [PATCH v2 05/12] qapi/gen: use './builtin' for the built-in module name, John Snow, 2020/12/16
- [PATCH v2 09/12] qapi/gen: move write method to QAPIGenC, make fname a str, John Snow, 2020/12/16
- [PATCH v2 06/12] qapi/source: Add builtin null-object sentinel, John Snow, 2020/12/16