Jason Lowe-Power has submitted this change. (
https://gem5-review.googlesource.com/c/public/gem5/+/42644 )
Change subject: python: Improve type annotations in pystats
..
python: Improve type annotations in pystats
This fixes some errors and warning when running mypy.
`gem5/src/python/m5/ext> mypy pystats`
There is one error that is ignored, which is a bug in mypy. See
https://github.com/python/mypy/issues/6040
Change-Id: I18b648c059da12bd30d612f0e265930b976f22b4
Signed-off-by: Jason Lowe-Power
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/42644
Reviewed-by: Andreas Sandberg
Maintainer: Bobby R. Bruce
Tested-by: kokoro
---
M src/python/m5/ext/pystats/group.py
M src/python/m5/ext/pystats/jsonloader.py
M src/python/m5/ext/pystats/statistic.py
3 files changed, 31 insertions(+), 19 deletions(-)
Approvals:
Andreas Sandberg: Looks good to me, approved
Bobby R. Bruce: Looks good to me, approved
kokoro: Regressions pass
diff --git a/src/python/m5/ext/pystats/group.py
b/src/python/m5/ext/pystats/group.py
index 22d11b2..cc9fcd3 100644
--- a/src/python/m5/ext/pystats/group.py
+++ b/src/python/m5/ext/pystats/group.py
@@ -25,7 +25,8 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import re
-from typing import Callable, Dict, Iterator, List, Optional, Pattern, Union
+from typing import Callable, Dict, Iterator, List, Mapping, Optional,
Pattern,\
+ Union
from .jsonserializable import JsonSerializable
from .statistic import Scalar, Statistic
@@ -118,8 +119,10 @@
precompiled regex or a string in regex format
"""
if isinstance(regex, str):
-regex = re.compile(regex)
-yield from self.children(lambda _name: regex.search(_name))
+pattern = re.compile(regex)
+else:
+pattern = regex
+yield from self.children(lambda _name: bool(pattern.search(_name)))
class Vector(Group):
"""
@@ -129,7 +132,7 @@
accordance to decisions made in relation to
https://gem5.atlassian.net/browse/GEM5-867.
"""
-def __init__(self, scalar_map: Dict[str,Scalar]):
+def __init__(self, scalar_map: Mapping[str,Scalar]):
super(Vector, self).__init__(
type="Vector",
time_conversion=None,
diff --git a/src/python/m5/ext/pystats/jsonloader.py
b/src/python/m5/ext/pystats/jsonloader.py
index a8261f3..35873e4 100644
--- a/src/python/m5/ext/pystats/jsonloader.py
+++ b/src/python/m5/ext/pystats/jsonloader.py
@@ -24,11 +24,12 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+from json.decoder import JSONDecodeError
from .simstat import SimStat
-from .statistic import Scalar, Distribution, Accumulator
+from .statistic import Scalar, Distribution, Accumulator, Statistic
from .group import Group, Vector
import json
-from typing import IO
+from typing import IO, Union
class JsonLoader(json.JSONDecoder):
"""
@@ -46,9 +47,11 @@
"""
def __init__(self):
-json.JSONDecoder.__init__(self, object_hook=self.__json_to_simstat)
+super(JsonLoader, self).__init__(self,
+object_hook=self.__json_to_simstat
+)
-def __json_to_simstat(self, d: dict) -> SimStat:
+def __json_to_simstat(self, d: dict) -> Union[SimStat,Statistic,Group]:
if 'type' in d:
if d['type'] == 'Scalar':
d.pop('type', None)
@@ -69,6 +72,11 @@
d.pop('type', None)
d.pop('time_conversion', None)
return Vector(d)
+
+else:
+raise ValueError(
+f"SimStat object has invalid type {d['type']}"
+)
else:
return SimStat(**d)
diff --git a/src/python/m5/ext/pystats/statistic.py
b/src/python/m5/ext/pystats/statistic.py
index 00d479d..15490a0 100644
--- a/src/python/m5/ext/pystats/statistic.py
+++ b/src/python/m5/ext/pystats/statistic.py
@@ -25,7 +25,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from abc import ABC
-from typing import Any, Optional, Union, List
+from typing import Any, Iterable, Optional, Union, List
from .jsonserializable import JsonSerializable
from .storagetype import StorageType
@@ -76,13 +76,13 @@
"""
value: List[Union[int,float]]
-def __init__(self, value: List[Union[int,float]],
+def __init__(self, value: Iterable[Union[int,float]],
type: Optional[str] = None,
unit: Optional[str] = None,
description: Optional[str] = None,
datatype: Optional[StorageType] = None):
super(BaseScalarVector, self).__init__(
- value=value,
+