[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v9 08/14] iotests: touch up log function signature
From: |
John Snow |
Subject: |
[PATCH v9 08/14] iotests: touch up log function signature |
Date: |
Tue, 24 Mar 2020 19:20:57 -0400 |
Representing nested, recursive data structures in mypy is notoriously
difficult; the best we can reliably do right now is denote the atom
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 per-invocation such
that the signature for filter=() requires that its callables take e.g. a
str and return a str.
Signed-off-by: John Snow <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 c93c6b4557..3a049ece5b 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -28,6 +28,7 @@
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
@@ -353,9 +354,16 @@ def _filter(_key, value):
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.21.1
- [PATCH v9 04/14] iotests: replace mutable list default args, (continued)
- [PATCH v9 04/14] iotests: replace mutable list default args, John Snow, 2020/03/24
- [PATCH v9 05/14] iotests: add pylintrc file, John Snow, 2020/03/24
- [PATCH v9 06/14] iotests: alphabetize standard imports, John Snow, 2020/03/24
- [PATCH v9 07/14] iotests: drop pre-Python 3.4 compatibility code, John Snow, 2020/03/24
- [PATCH v9 08/14] iotests: touch up log function signature,
John Snow <=
- [PATCH v9 10/14] iotests: add hmp helper with logging, John Snow, 2020/03/24
- [PATCH v9 12/14] iotest 258: use script_main, John Snow, 2020/03/24
- [PATCH v9 09/14] iotests: limit line length to 79 chars, John Snow, 2020/03/24