diff mbox series

[1/8] python/qapi: correct re.Match type hints for 3.13

Message ID 20240820002318.1380276-2-jsnow@redhat.com
State New
Headers show
Series move qapi under python/qemu/ | expand

Commit Message

John Snow Aug. 20, 2024, 12:23 a.m. UTC
typing.Match was removed in Python 3.13, so we need to use re.Match
instead. However, Python 3.8 doesn't support using re.Match as a type
hint directly, so we need a conditional for now.

The import is written oddly so that "Match" is explicitly re-exported
for re-use by other modules. mypy will complain otherwise.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 scripts/qapi/common.py | 10 +++++++++-
 scripts/qapi/parser.py |  3 +--
 2 files changed, 10 insertions(+), 3 deletions(-)

Comments

Philippe Mathieu-Daudé Aug. 20, 2024, 10:19 a.m. UTC | #1
On 20/8/24 02:23, John Snow wrote:
> typing.Match was removed in Python 3.13, so we need to use re.Match
> instead. However, Python 3.8 doesn't support using re.Match as a type
> hint directly, so we need a conditional for now.
> 
> The import is written oddly so that "Match" is explicitly re-exported
> for re-use by other modules. mypy will complain otherwise.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>   scripts/qapi/common.py | 10 +++++++++-
>   scripts/qapi/parser.py |  3 +--
>   2 files changed, 10 insertions(+), 3 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
diff mbox series

Patch

diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index 737b059e629..444b3acf53a 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -12,16 +12,24 @@ 
 # See the COPYING file in the top-level directory.
 
 import re
+import sys
 from typing import (
     Any,
     Dict,
-    Match,
     Optional,
     Sequence,
     Union,
 )
 
 
+if sys.version_info < (3, 9):
+    # typing.Match was removed in 3.13,
+    # but it's still a necessity in 3.8.
+    from typing import \
+        Match as Match  # pylint: disable=useless-import-alias
+else:
+    Match = re.Match
+
 #: Magic string that gets removed along with all space to its right.
 EATSPACE = '\033EATSPACE.'
 POINTER_SUFFIX = ' *' + EATSPACE
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index adc85b5b394..9a42b119131 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -23,13 +23,12 @@ 
     Dict,
     List,
     Mapping,
-    Match,
     Optional,
     Set,
     Union,
 )
 
-from .common import must_match
+from .common import Match, must_match
 from .error import QAPISemError, QAPISourceError
 from .source import QAPISourceInfo