[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 13/16] qapi/expr.py: Modify check_keys to accept any Collectio
From: |
John Snow |
Subject: |
[PATCH v3 13/16] qapi/expr.py: Modify check_keys to accept any Collection |
Date: |
Mon, 22 Feb 2021 19:34:05 -0500 |
This is a minor adjustment that allows the 'required' and 'optional'
keys fields to take a default value of an empty, immutable sequence (the
empty tuple).
This reveals a quirk of this function, which is that "a + b" is
list-specific behavior. We can accept a wider variety of types if we
avoid that behavior. Using Collection allows us to accept things like
lists, tuples, sets, and so on.
(Iterable would also have worked, but Iterable also includes things like
generator expressions which are consumed upon iteration, which would
require a rewrite to make sure that each input was only traversed once.)
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/expr.py | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py
index 2b96bec722f..0b841f292d7 100644
--- a/scripts/qapi/expr.py
+++ b/scripts/qapi/expr.py
@@ -33,6 +33,7 @@
import re
from typing import (
+ Collection,
Iterable,
List,
MutableMapping,
@@ -133,8 +134,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: Collection[str] = (),
+ optional: Collection[str] = ()) -> None:
"""
Ensures an object has a specific set of keys. [Const]
@@ -155,7 +156,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.29.2
- [PATCH v3 10/16] qapi/expr.py: Remove single-letter variable, (continued)
- [PATCH v3 10/16] qapi/expr.py: Remove single-letter variable, John Snow, 2021/02/22
- [PATCH v3 11/16] qapi/expr.py: enable pylint checks, John Snow, 2021/02/22
- [PATCH v3 12/16] qapi/expr.py: Add docstrings, John Snow, 2021/02/22
- [PATCH v3 08/16] qapi/expr.py: add type hint annotations, John Snow, 2021/02/22
- Re: [PATCH v3 08/16] qapi/expr.py: add type hint annotations, Markus Armbruster, 2021/02/25
- [PATCH v3 13/16] qapi/expr.py: Modify check_keys to accept any Collection,
John Snow <=
- [PATCH v3 16/16] qapi/expr.py: Use an expression checker dispatch table, John Snow, 2021/02/22
- [PATCH v3 14/16] qapi/expr.py: Use tuples instead of lists for static data, John Snow, 2021/02/22
- [PATCH v3 15/16] qapi/expr.py: move related checks inside check_xxx functions, John Snow, 2021/02/22