Repository: amaranth-lang/amaranth
Pull request #1166 (2024-02-28 09:00:45)

lib.memory: Add missing __eq__ to signature types.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1166?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 87.34%. Comparing base [(`c6bc9b4`)](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/c6bc9b47ef4d29f1d1fa340c3e0048deccd56ae0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) to head [(`ad59420`)](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1166?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1166      +/-   ##
==========================================
- Coverage   87.61%   87.34%   -0.28%     
==========================================
  Files          43       43              
  Lines        8695     8699       +4     
  Branches     2058     2055       -3     
==========================================
- Hits         7618     7598      -20     
- Misses        887      915      +28     
+ Partials      190      186       -4     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1166?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).


Patch:
diff --git a/amaranth/lib/memory.py b/amaranth/lib/memory.py
index a1a69f2..9cd4687 100644
--- a/amaranth/lib/memory.py
+++ b/amaranth/lib/memory.py
@@ -110,6 +110,12 @@ class WritePort:
         def granularity(self):
             return self._granularity
 
+        def __eq__(self, other):
+            return (type(self) is type(other) and
+                    self.addr_width == other.addr_width and
+                    self.shape == other.shape and
+                    self.granularity == other.granularity)
+
         def __repr__(self):
             granularity = f", granularity={self.granularity}" if self.granularity is not None else ""
             return f"WritePort.Signature(addr_width={self.addr_width}, shape={self.shape}{granularity})"
@@ -213,6 +219,11 @@ class ReadPort:
         def shape(self):
             return self._shape
 
+        def __eq__(self, other):
+            return (type(self) is type(other) and
+                    self.addr_width == other.addr_width and
+                    self.shape == other.shape)
+
         def __repr__(self):
             return f"ReadPort.Signature(addr_width={self.addr_width}, shape={self.shape})"
 


Test patch:
diff --git a/tests/test_lib_memory.py b/tests/test_lib_memory.py
index 917097f..eb7512b 100644
--- a/tests/test_lib_memory.py
+++ b/tests/test_lib_memory.py
@@ -83,6 +83,16 @@ class WritePortTestCase(FHDLTestCase):
                 "^Granularity must divide data array length$"):
             WritePort.Signature(addr_width=2, shape=ArrayLayout(8, 8), granularity=3)
 
+    def test_signature_eq(self):
+        sig = WritePort.Signature(addr_width=2, shape=8)
+        self.assertEqual(sig, WritePort.Signature(addr_width=2, shape=8))
+        self.assertNotEqual(sig, WritePort.Signature(addr_width=2, shape=7))
+        self.assertNotEqual(sig, WritePort.Signature(addr_width=1, shape=8))
+        self.assertNotEqual(sig, WritePort.Signature(addr_width=2, shape=8, granularity=8))
+        sig = WritePort.Signature(addr_width=2, shape=8, granularity=4)
+        self.assertEqual(sig, WritePort.Signature(addr_width=2, shape=8, granularity=4))
+        self.assertNotEqual(sig, WritePort.Signature(addr_width=2, shape=8, granularity=8))
+
     def test_constructor(self):
         signature = WritePort.Signature(shape=MyStruct, addr_width=4)
         port = WritePort(signature, memory=None, domain="sync")
@@ -171,6 +181,13 @@ class ReadPortTestCase(FHDLTestCase):
                 "^`addr_width` must be a non-negative int, not -2$"):
             ReadPort.Signature(addr_width=-2, shape=8)
 
+    def test_signature_eq(self):
+        sig = ReadPort.Signature(addr_width=2, shape=8)
+        self.assertEqual(sig, ReadPort.Signature(addr_width=2, shape=8))
+        self.assertNotEqual(sig, ReadPort.Signature(addr_width=2, shape=7))
+        self.assertNotEqual(sig, ReadPort.Signature(addr_width=1, shape=8))
+        self.assertNotEqual(sig, WritePort.Signature(addr_width=2, shape=8))
+
     def test_constructor(self):
         signature = ReadPort.Signature(shape=MyStruct, addr_width=4)
         port = ReadPort(signature, memory=None, domain="sync")