[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 11/15] qapi/parser: Rework _check_pragma_list_of_str as a Type
From: |
John Snow |
Subject: |
[PATCH v3 11/15] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard |
Date: |
Wed, 19 May 2021 14:39:47 -0400 |
TypeGuards wont exist in Python proper until 3.10. Ah well. We can hack
up our own by declaring this function to return the type we claim it
checks for and using this to safely downcast object -> List[str].
In so doing, I bring this function under _pragma so it can use the
'info' object in its closure. Having done this, _pragma also now no
longer needs to take a 'self' parameter, so drop it.
To help with line-length, and with the context evident from its new
scope, rename the function to the shorter check_list_str().
Signed-off-by: John Snow <jsnow@redhat.com>
---
I left (name, value) as args to avoid creating a fully magic "macro",
because this looked simply too weird:
info.pragma.foobar = check_list_str()
and it looked more reasonable as:
info.pragma.foobar = check_list_str(name, value)
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/qapi/parser.py | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 9f980f75139..8a58e1228f0 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -17,6 +17,7 @@
from collections import OrderedDict
import os
import re
+from typing import List
from .common import must_match
from .error import QAPISemError, QAPISourceError
@@ -154,28 +155,29 @@ def _include(include, info, incl_fname,
previously_included):
) from err
@staticmethod
- def _check_pragma_list_of_str(name, value, info):
- if (not isinstance(value, list)
- or any([not isinstance(elt, str) for elt in value])):
- raise QAPISemError(
- info,
- "pragma %s must be a list of strings" % name)
+ def _pragma(name, value, info):
+
+ def check_list_str(name, value) -> List[str]:
+ if (not isinstance(value, list) or
+ any([not isinstance(elt, str) for elt in value])):
+ raise QAPISemError(
+ info,
+ "pragma %s must be a list of strings" % name)
+ return value
+
+ pragma = info.pragma
- def _pragma(self, name, value, info):
if name == 'doc-required':
if not isinstance(value, bool):
raise QAPISemError(info,
"pragma 'doc-required' must be boolean")
- info.pragma.doc_required = value
+ pragma.doc_required = value
elif name == 'command-name-exceptions':
- self._check_pragma_list_of_str(name, value, info)
- info.pragma.command_name_exceptions = value
+ pragma.command_name_exceptions = check_list_str(name, value)
elif name == 'command-returns-exceptions':
- self._check_pragma_list_of_str(name, value, info)
- info.pragma.command_returns_exceptions = value
+ pragma.command_returns_exceptions = check_list_str(name, value)
elif name == 'member-name-exceptions':
- self._check_pragma_list_of_str(name, value, info)
- info.pragma.member_name_exceptions = value
+ pragma.member_name_exceptions = check_list_str(name, value)
else:
raise QAPISemError(info, "unknown pragma '%s'" % name)
--
2.30.2
- [PATCH v3 00/15] qapi: static typing conversion, pt5a, John Snow, 2021/05/19
- [PATCH v3 05/15] qapi/parser: Assert lexer value is a string, John Snow, 2021/05/19
- [PATCH v3 01/15] qapi/parser: Don't try to handle file errors, John Snow, 2021/05/19
- [PATCH v3 07/15] qapi/parser: assert object keys are strings, John Snow, 2021/05/19
- [PATCH v3 15/15] qapi/parser: add docstrings, John Snow, 2021/05/19
- [PATCH v3 02/15] qapi: Add test for nonexistent schema file, John Snow, 2021/05/19
- [PATCH v3 04/15] qapi/parser: factor parsing routine into method, John Snow, 2021/05/19
- [PATCH v3 03/15] qapi/source: Remove line number from QAPISourceInfo initializer, John Snow, 2021/05/19
- [PATCH v3 11/15] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard,
John Snow <=
- [PATCH v3 10/15] qapi/parser: Fix token membership tests when token can be None, John Snow, 2021/05/19
- [PATCH v3 14/15] qapi/parser: allow 'ch' variable name, John Snow, 2021/05/19
- [PATCH v3 09/15] qapi: add must_match helper, John Snow, 2021/05/19
- [PATCH v3 08/15] qapi/parser: Use @staticmethod where appropriate, John Snow, 2021/05/19
- [PATCH v3 06/15] qapi/parser: enforce all top-level expressions must be dict in _parse(), John Snow, 2021/05/19
- [PATCH v3 12/15] qapi/parser: add type hint annotations, John Snow, 2021/05/19
- [PATCH v3 13/15] qapi/parser: Remove superfluous list comprehension, John Snow, 2021/05/19
- Re: [PATCH v3 00/15] qapi: static typing conversion, pt5a, Markus Armbruster, 2021/05/20