qapi: add must_match helper

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>
This commit is contained in:
John Snow 2021-05-19 14:39:45 -04:00 committed by Markus Armbruster
parent 43b1be65f0
commit e0e8a0ac2e
3 changed files with 16 additions and 11 deletions

View file

@ -8,11 +8,11 @@ This is the main entry point for generating C code from the QAPI schema.
"""
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 @@ from .visit import gen_visit
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