[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 13/16] qapi/expr.py: Modify check_keys to accept any Iterable
From: |
John Snow |
Subject: |
[PATCH v2 13/16] qapi/expr.py: Modify check_keys to accept any Iterable |
Date: |
Mon, 26 Oct 2020 17:36:34 -0400 |
This is a very minor adjustment.
a + b is list-specific behavior, but we can accept a wider variety of
types in a more pythonic fashion if we avoid that behavior.
Typing it this way allows callers to use things like dict.keys() and
other iterables that are not their own discrete lists.
Including it just as a statement of practice if nothing else: It's nice
to use the least-specific type possible as function input and use the
most-specific type for returns.
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Cleber Rosa <crosa@redhat.com>
---
scripts/qapi/expr.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py
index 8ad82ca98864..883aa1781599 100644
--- a/scripts/qapi/expr.py
+++ b/scripts/qapi/expr.py
@@ -133,8 +133,8 @@ def check_defn_name_str(name: str, info: QAPISourceInfo,
meta: str) -> None:
def check_keys(value: _JSObject,
info: QAPISourceInfo,
source: str,
- required: List[str],
- optional: List[str]) -> None:
+ required: Iterable[str] = (),
+ optional: Iterable[str] = ()) -> None:
"""
Ensures an object has a specific set of keys. [Const]
@@ -155,7 +155,7 @@ def pprint(elems: Iterable[str]) -> str:
"%s misses key%s %s"
% (source, 's' if len(missing) > 1 else '',
pprint(missing)))
- allowed = set(required + optional)
+ allowed = set(required) | set(optional)
unknown = set(value) - allowed
if unknown:
raise QAPISemError(
--
2.26.2
- [PATCH v2 05/16] qapi/expr.py: move string check upwards in check_type, (continued)
- [PATCH v2 05/16] qapi/expr.py: move string check upwards in check_type, John Snow, 2020/10/26
- [PATCH v2 06/16] qapi/expr.py: Check type of 'data' member, John Snow, 2020/10/26
- [PATCH v2 01/16] qapi/expr.py: Remove 'info' argument from nested check_if_str, John Snow, 2020/10/26
- [PATCH v2 07/16] qapi/expr.py: Add casts in a few select cases, John Snow, 2020/10/26
- [PATCH v2 08/16] qapi/expr.py: add type hint annotations, John Snow, 2020/10/26
- [PATCH v2 09/16] qapi/expr.py: rewrite check_if, John Snow, 2020/10/26
- [PATCH v2 15/16] qapi/expr.py: move related checks inside check_xxx functions, John Snow, 2020/10/26
- [PATCH v2 10/16] qapi/expr.py: Remove single-letter variable, John Snow, 2020/10/26
- [PATCH v2 11/16] qapi/expr.py: enable pylint checks, John Snow, 2020/10/26
- [PATCH v2 12/16] qapi/expr.py: Add docstrings, John Snow, 2020/10/26
- [PATCH v2 13/16] qapi/expr.py: Modify check_keys to accept any Iterable,
John Snow <=
- [PATCH v2 14/16] qapi/expr.py: Use tuples instead of lists for static data, John Snow, 2020/10/26
- [PATCH v2 16/16] qapi/expr.py: Use an expression checker dispatch table, John Snow, 2020/10/26