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

John Snow posted 8 patches 3 months ago
[PATCH 1/8] python/qapi: correct re.Match type hints for 3.13
Posted by John Snow 3 months ago
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(-)

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
 
-- 
2.45.0
Re: [PATCH 1/8] python/qapi: correct re.Match type hints for 3.13
Posted by Philippe Mathieu-Daudé 3 months ago
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>