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 <[email protected]>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/42644
Reviewed-by: Andreas Sandberg <[email protected]>
Maintainer: Bobby R. Bruce <[email protected]>
Tested-by: kokoro <[email protected]>
---
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,
+                                           value=list(value),
                                            type=type,
                                            unit=unit,
                                            description=description,
@@ -104,7 +104,7 @@
         from statistics import mean as statistics_mean
         return statistics_mean(self.value)

-    def count(self) -> int:
+    def count(self) -> float:
         """
         Returns the count across all the bins.

@@ -114,7 +114,6 @@
             The sum of all bin values.
         """
         assert(self.value != None)
-        assert(isinstance(self.value, List))
         return sum(self.value)


@@ -128,7 +127,6 @@
     It is assumed each bucket is of equal size.
     """

-    value: List[int]
     min: Union[float, int]
     max: Union[float, int]
     num_bins: int
@@ -139,7 +137,7 @@
     overflow: Optional[int]
     logs: Optional[float]

-    def __init__(self, value: List[int],
+    def __init__(self, value: Iterable[int],
                  min: Union[float, int],
                  max: Union[float, int],
                  num_bins: int,
@@ -179,12 +177,12 @@
     A statistical type representing an accumulator.
     """

-    count: int
+    _count: int
     min: Union[int, float]
     max: Union[int, float]
     sum_squared: Optional[int]

-    def __init__(self, value: List[Union[int,float]],
+    def __init__(self, value: Iterable[Union[int,float]],
                  count: int,
                  min: Union[int, float],
                  max: Union[int, float],
@@ -200,7 +198,10 @@
                                      datatype=datatype,
                                     )

-        self.count = count
+        self._count = count
         self.min = min
         self.max = max
-        self.sum_squared = sum_squared
\ No newline at end of file
+        self.sum_squared = sum_squared
+
+    def count(self) -> int:
+        return self._count

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/42644
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I18b648c059da12bd30d612f0e265930b976f22b4
Gerrit-Change-Number: 42644
Gerrit-PatchSet: 6
Gerrit-Owner: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: Andreas Sandberg <[email protected]>
Gerrit-Reviewer: Bobby R. Bruce <[email protected]>
Gerrit-Reviewer: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: Jason Lowe-Power <[email protected]>
Gerrit-Reviewer: kokoro <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to