[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 09/15] qapi: add must_match helper
From: |
Markus Armbruster |
Subject: |
[PULL 09/15] qapi: add must_match helper |
Date: |
Thu, 20 May 2021 19:52:50 +0200 |
From: John Snow <jsnow@redhat.com>
Mypy cannot generally understand that these regex functions cannot
possibly fail. Add a "must_match" helper that makes this clear for
mypy.
Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20210519183951.3946870-10-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
scripts/qapi/common.py | 8 +++++++-
scripts/qapi/main.py | 6 ++----
scripts/qapi/parser.py | 13 +++++++------
3 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index cbd3fd81d3..6ad1eeb61d 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -12,7 +12,7 @@
# See the COPYING file in the top-level directory.
import re
-from typing import Optional, Sequence
+from typing import Match, Optional, Sequence
#: Magic string that gets removed along with all space to its right.
@@ -210,3 +210,9 @@ def gen_endif(ifcond: Sequence[str]) -> str:
#endif /* %(cond)s */
''', cond=ifc)
return ret
+
+
+def must_match(pattern: str, string: str) -> Match[str]:
+ match = re.match(pattern, string)
+ assert match is not None
+ return match
diff --git a/scripts/qapi/main.py b/scripts/qapi/main.py
index 703e7ed1ed..f2ea6e0ce4 100644
--- a/scripts/qapi/main.py
+++ b/scripts/qapi/main.py
@@ -8,11 +8,11 @@
"""
import argparse
-import re
import sys
from typing import Optional
from .commands import gen_commands
+from .common import must_match
from .error import QAPIError
from .events import gen_events
from .introspect import gen_introspect
@@ -22,9 +22,7 @@
def invalid_prefix_char(prefix: str) -> Optional[str]:
- match = re.match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix)
- # match cannot be None, but mypy cannot infer that.
- assert match is not None
+ match = must_match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix)
if match.end() != len(prefix):
return prefix[match.end()]
return None
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index 7c71866195..48137d3fbe 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -18,6 +18,7 @@
import os
import re
+from .common import must_match
from .error import QAPISemError, QAPISourceError
from .source import QAPISourceInfo
@@ -238,8 +239,8 @@ def accept(self, skip_comment=True):
elif not self.tok.isspace():
# Show up to next structural, whitespace or quote
# character
- match = re.match('[^[\\]{}:,\\s\'"]+',
- self.src[self.cursor-1:])
+ match = must_match('[^[\\]{}:,\\s\'"]+',
+ self.src[self.cursor-1:])
raise QAPIParseError(self, "stray '%s'" % match.group(0))
def get_members(self):
@@ -369,7 +370,7 @@ def append(self, line):
# Strip leading spaces corresponding to the expected indent level
# Blank lines are always OK.
if line:
- indent = re.match(r'\s*', line).end()
+ indent = must_match(r'\s*', line).end()
if indent < self._indent:
raise QAPIParseError(
self._parser,
@@ -505,7 +506,7 @@ def _append_args_line(self, line):
# from line and replace it with spaces so that 'f' has the
# same index as it did in the original line and can be
# handled the same way we will handle following lines.
- indent = re.match(r'@\S*:\s*', line).end()
+ indent = must_match(r'@\S*:\s*', line).end()
line = line[indent:]
if not line:
# Line was just the "@arg:" header; following lines
@@ -540,7 +541,7 @@ def _append_features_line(self, line):
# from line and replace it with spaces so that 'f' has the
# same index as it did in the original line and can be
# handled the same way we will handle following lines.
- indent = re.match(r'@\S*:\s*', line).end()
+ indent = must_match(r'@\S*:\s*', line).end()
line = line[indent:]
if not line:
# Line was just the "@arg:" header; following lines
@@ -586,7 +587,7 @@ def _append_various_line(self, line):
# from line and replace it with spaces so that 'f' has the
# same index as it did in the original line and can be
# handled the same way we will handle following lines.
- indent = re.match(r'\S*:\s*', line).end()
+ indent = must_match(r'\S*:\s*', line).end()
line = line[indent:]
if not line:
# Line was just the "Section:" header; following lines
--
2.26.3
- [PULL 00/15] QAPI patches patches for 2021-05-20, Markus Armbruster, 2021/05/20
- [PULL 09/15] qapi: add must_match helper,
Markus Armbruster <=
- [PULL 10/15] qapi/parser: Fix token membership tests when token can be None, Markus Armbruster, 2021/05/20
- [PULL 06/15] qapi/parser: enforce all top-level expressions must be dict in _parse(), Markus Armbruster, 2021/05/20
- [PULL 11/15] qapi/parser: Rework _check_pragma_list_of_str as a TypeGuard, Markus Armbruster, 2021/05/20
- [PULL 01/15] qapi/parser: Don't try to handle file errors, Markus Armbruster, 2021/05/20
- [PULL 04/15] qapi/parser: factor parsing routine into method, Markus Armbruster, 2021/05/20
- [PULL 15/15] qapi/parser: add docstrings, Markus Armbruster, 2021/05/20
- [PULL 13/15] qapi/parser: Remove superfluous list comprehension, Markus Armbruster, 2021/05/20
- [PULL 07/15] qapi/parser: assert object keys are strings, Markus Armbruster, 2021/05/20
- [PULL 02/15] qapi: Add test for nonexistent schema file, Markus Armbruster, 2021/05/20
- [PULL 05/15] qapi/parser: Assert lexer value is a string, Markus Armbruster, 2021/05/20