Repository: amaranth-lang/amaranth
Pull request #1168 (2024-02-28 11:44:00)

lib.io: Expose `Pin` path and name as attributes.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1168?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) Report
Attention: Patch coverage is `85.71429%` with `1 lines` in your changes are missing coverage. Please review.
> Project coverage is 87.28%. Comparing base [(`1dd2e61`)](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/1dd2e6150c4c3524a44a8d371802758699e19c4a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) to head [(`04becea`)](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1168?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).

| [Files](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1168?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) | Patch % | Lines |
|---|---|---|
| [amaranth/lib/io.py](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1168?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang#diff-YW1hcmFudGgvbGliL2lvLnB5) | 85.71% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1168?src=pr&el=tree&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    #1168      +/-   ##
==========================================
- Coverage   87.62%   87.28%   -0.34%     
==========================================
  Files          43       43              
  Lines        8702     8708       +6     
  Branches     2058     1869     -189     
==========================================
- Hits         7625     7601      -24     
- Misses        887      917      +30     
  Partials      190      190              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1168?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/io.py b/amaranth/lib/io.py
index 09058d2..880e5d7 100644
--- a/amaranth/lib/io.py
+++ b/amaranth/lib/io.py
@@ -1,6 +1,7 @@
 from .. import *
 from ..lib import wiring
 from ..lib.wiring import In, Out
+from .. import tracer
 
 
 __all__ = ["Pin"]
@@ -123,9 +124,14 @@ class Pin(wiring.PureInterface):
 
     def __init__(self, width, dir, *, xdr=0, name=None, path=None, src_loc_at=0):
         if name is not None:
-            if path is None:
+            if path is not None:
                 raise ValueError("Cannot pass both name and path")
             path = (name,)
+        if path is None:
+            name = tracer.get_var_name(depth=2 + src_loc_at, default="$pin")
+            path = (name,)
+        self.path = tuple(path)
+        self.name = path[-1]
         signature = Pin.Signature(width, dir, xdr=xdr)
         super().__init__(signature, path=path, src_loc_at=src_loc_at + 1)
 


Test patch:
diff --git a/tests/test_lib_io.py b/tests/test_lib_io.py
index 1c82b1f..0f35322 100644
--- a/tests/test_lib_io.py
+++ b/tests/test_lib_io.py
@@ -218,3 +218,14 @@ class PinTestCase(FHDLTestCase):
         self.assertEqual(pin.signature.width, 2)
         self.assertEqual(pin.signature.dir,   "io")
         self.assertEqual(pin.signature.xdr,   2)
+        self.assertEqual(pin.name, "pin")
+        self.assertEqual(pin.path, ("pin",))
+        self.assertEqual(pin.i0.name, "pin__i0")
+        pin = Pin(2, dir="io", xdr=2, name="testpin")
+        self.assertEqual(pin.name, "testpin")
+        self.assertEqual(pin.path, ("testpin",))
+        self.assertEqual(pin.i0.name, "testpin__i0")
+        pin = Pin(2, dir="io", xdr=2, path=["a", "b"])
+        self.assertEqual(pin.name, "b")
+        self.assertEqual(pin.path, ("a", "b"))
+        self.assertEqual(pin.i0.name, "a__b__i0")