[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 09/22] qapi: add match_nofail helper
From: |
Markus Armbruster |
Subject: |
Re: [PATCH 09/22] qapi: add match_nofail helper |
Date: |
Sun, 25 Apr 2021 09:54:24 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) |
John Snow <jsnow@redhat.com> writes:
> Mypy cannot generally understand that these regex functions cannot
> possibly fail. Add a _nofail helper that clarifies this for mypy.
Convention wants a blank line here.
> Signed-off-by: John Snow <jsnow@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 cbd3fd81d36..d38c1746767 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 match_nofail(pattern: str, string: str) -> Match[str]:
> + match = re.match(pattern, string)
> + assert match is not None
> + return match
Name it must_match()? You choose.
I wish we could have more stating typing with less notational overhead,
but no free lunch...
[...]
- Re: [PATCH 03/22] qapi/source: Remove line number from QAPISourceInfo initializer, (continued)
- [PATCH 04/22] qapi/parser: factor parsing routine into method, John Snow, 2021/04/21
- [PATCH 08/22] qapi/parser: Use @staticmethod where appropriate, John Snow, 2021/04/21
- [PATCH 06/22] qapi/parser: assert get_expr returns object in outer loop, John Snow, 2021/04/21
- [PATCH 09/22] qapi: add match_nofail helper, John Snow, 2021/04/21
- Re: [PATCH 09/22] qapi: add match_nofail helper,
Markus Armbruster <=
- [PATCH 05/22] qapi/parser: Assert lexer value is a string, John Snow, 2021/04/21
[PATCH 07/22] qapi/parser: assert object keys are strings, John Snow, 2021/04/21