mirror of https://github.com/xemu-project/xemu.git
qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard
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> Message-Id: <20210519183951.3946870-12-jsnow@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
parent
c256263f3d
commit
03386200b9
|
@ -17,6 +17,7 @@
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from .common import must_match
|
from .common import must_match
|
||||||
from .error import QAPISemError, QAPISourceError
|
from .error import QAPISemError, QAPISourceError
|
||||||
|
@ -154,28 +155,29 @@ class QAPISchemaParser:
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _check_pragma_list_of_str(name, value, info):
|
def _pragma(name, value, info):
|
||||||
if (not isinstance(value, list)
|
|
||||||
or any([not isinstance(elt, str) for elt in value])):
|
def check_list_str(name, value) -> List[str]:
|
||||||
raise QAPISemError(
|
if (not isinstance(value, list) or
|
||||||
info,
|
any([not isinstance(elt, str) for elt in value])):
|
||||||
"pragma %s must be a list of strings" % name)
|
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 name == 'doc-required':
|
||||||
if not isinstance(value, bool):
|
if not isinstance(value, bool):
|
||||||
raise QAPISemError(info,
|
raise QAPISemError(info,
|
||||||
"pragma 'doc-required' must be boolean")
|
"pragma 'doc-required' must be boolean")
|
||||||
info.pragma.doc_required = value
|
pragma.doc_required = value
|
||||||
elif name == 'command-name-exceptions':
|
elif name == 'command-name-exceptions':
|
||||||
self._check_pragma_list_of_str(name, value, info)
|
pragma.command_name_exceptions = check_list_str(name, value)
|
||||||
info.pragma.command_name_exceptions = value
|
|
||||||
elif name == 'command-returns-exceptions':
|
elif name == 'command-returns-exceptions':
|
||||||
self._check_pragma_list_of_str(name, value, info)
|
pragma.command_returns_exceptions = check_list_str(name, value)
|
||||||
info.pragma.command_returns_exceptions = value
|
|
||||||
elif name == 'member-name-exceptions':
|
elif name == 'member-name-exceptions':
|
||||||
self._check_pragma_list_of_str(name, value, info)
|
pragma.member_name_exceptions = check_list_str(name, value)
|
||||||
info.pragma.member_name_exceptions = value
|
|
||||||
else:
|
else:
|
||||||
raise QAPISemError(info, "unknown pragma '%s'" % name)
|
raise QAPISemError(info, "unknown pragma '%s'" % name)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue