[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 08/24] iotests: touch up log function signature
From: |
Max Reitz |
Subject: |
[PULL 08/24] iotests: touch up log function signature |
Date: |
Tue, 5 May 2020 14:58:10 +0200 |
From: John Snow <address@hidden>
Representing nested, recursive data structures in mypy is notoriously
difficult; the best we can reliably do right now is denote the leaf
types as "Any" while describing the general shape of the data.
Regardless, this fully annotates the log() function.
Typing notes:
TypeVar is a Type variable that can optionally be constrained by a
sequence of possible types. This variable is bound to a specific type
per-invocation, like a Generic.
log() behaves as log<Msg>() now, where the incoming type informs the
signature it expects for any filter arguments passed in. If Msg is a
str, then filter should take and return a str.
Signed-off-by: John Snow <address@hidden>
Reviewed-by: Max Reitz <address@hidden>
Message-Id: <address@hidden>
Reviewed-by: Kevin Wolf <address@hidden>
Signed-off-by: Max Reitz <address@hidden>
---
tests/qemu-iotests/iotests.py | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index 374a8f6077..69f24223d2 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -28,6 +28,7 @@ import signal
import struct
import subprocess
import sys
+from typing import (Any, Callable, Dict, Iterable, List, Optional, TypeVar)
import unittest
# pylint: disable=import-error, wrong-import-position
@@ -354,9 +355,16 @@ def filter_qmp_imgfmt(qmsg):
return value
return filter_qmp(qmsg, _filter)
-def log(msg, filters=(), indent=None):
- '''Logs either a string message or a JSON serializable message (like QMP).
- If indent is provided, JSON serializable messages are pretty-printed.'''
+
+Msg = TypeVar('Msg', Dict[str, Any], List[Any], str)
+
+def log(msg: Msg,
+ filters: Iterable[Callable[[Msg], Msg]] = (),
+ indent: Optional[int] = None) -> None:
+ """
+ Logs either a string message or a JSON serializable message (like QMP).
+ If indent is provided, JSON serializable messages are pretty-printed.
+ """
for flt in filters:
msg = flt(msg)
if isinstance(msg, (dict, list)):
--
2.26.2
- [PULL 00/24] Block patches, Max Reitz, 2020/05/05
- [PULL 03/24] iotests: ignore import warnings from pylint, Max Reitz, 2020/05/05
- [PULL 02/24] iotests: don't use 'format' for drive_add, Max Reitz, 2020/05/05
- [PULL 04/24] iotests: replace mutable list default args, Max Reitz, 2020/05/05
- [PULL 06/24] iotests: alphabetize standard imports, Max Reitz, 2020/05/05
- [PULL 05/24] iotests: add pylintrc file, Max Reitz, 2020/05/05
- [PULL 07/24] iotests: drop pre-Python 3.4 compatibility code, Max Reitz, 2020/05/05
- [PULL 01/24] iotests: do a light delinting, Max Reitz, 2020/05/05
- [PULL 08/24] iotests: touch up log function signature,
Max Reitz <=
- [PULL 10/24] iotests: add hmp helper with logging, Max Reitz, 2020/05/05
- [PULL 12/24] iotest 258: use script_main, Max Reitz, 2020/05/05
- [PULL 11/24] iotests: add script_initialize, Max Reitz, 2020/05/05
- [PULL 13/24] iotests: Mark verify functions as private, Max Reitz, 2020/05/05
- [PULL 09/24] iotests: limit line length to 79 chars, Max Reitz, 2020/05/05
- [PULL 18/24] block: Comment cleanups, Max Reitz, 2020/05/05
- [PULL 14/24] iotests: use python logging for iotests.log(), Max Reitz, 2020/05/05
- [PULL 19/24] Fix iotest 153, Max Reitz, 2020/05/05
- [PULL 17/24] qcow2: Tweak comment about bitmaps vs. resize, Max Reitz, 2020/05/05
- [PULL 21/24] block/block-copy: alloc task on each iteration, Max Reitz, 2020/05/05