Repository: amaranth-lang/amaranth
Pull request #892 (2023-09-01 05:33:33)

lib.wiring: make `flipped()` self-invertible

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/892?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) Report
> Merging [#892](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/892?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) (b9a1e55) into [main](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/5a17f94fdce8ee3b1416da45a34f191b8bcda2e1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) (5a17f94) will **decrease** coverage by `0.29%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##             main     #892      +/-   ##
==========================================
- Coverage   83.94%   83.65%   -0.29%     
==========================================
  Files          54       54              
  Lines        7610     7605       -5     
  Branches     1843     1601     -242     
==========================================
- Hits         6388     6362      -26     
- Misses       1025     1048      +23     
+ Partials      197      195       -2     
```


| [Files Changed](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/892?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) | Coverage Δ | |
|---|---|---|
| [amaranth/lib/wiring.py](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/892?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang#diff-YW1hcmFudGgvbGliL3dpcmluZy5weQ==) | `100.00% <100.00%> (ø)` | |

... and [6 files with indirect coverage changes](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/892/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang)

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang)


Patch:
diff --git a/amaranth/lib/wiring.py b/amaranth/lib/wiring.py
index cc1977b..c8a4287 100644
--- a/amaranth/lib/wiring.py
+++ b/amaranth/lib/wiring.py
@@ -552,7 +552,10 @@ class FlippedInterface:
 
 
 def flipped(interface):
-    return FlippedInterface(interface)
+    if type(interface) is FlippedInterface:
+        return interface._FlippedInterface__unflipped
+    else:
+        return FlippedInterface(interface)
 
 
 @final


Test patch:
diff --git a/tests/test_lib_wiring.py b/tests/test_lib_wiring.py
index 06e9bab..d33ac7e 100644
--- a/tests/test_lib_wiring.py
+++ b/tests/test_lib_wiring.py
@@ -530,6 +530,7 @@ class FlippedInterfaceTestCase(unittest.TestCase):
         self.assertEqual(tintf, flipped(intf))
         self.assertRegex(repr(tintf),
             r"^flipped\(<.+?\.Interface object at .+>\)$")
+        self.assertIs(flipped(tintf), intf)
 
     def test_getattr_setattr(self):
         class I(Interface):