Repository: amaranth-lang/amaranth
Pull request #1196 (2024-03-11 19:45:24)

Some f-strings in utils.py are broken (missing the letter "f")
Without this patch, `exact_log2(3)` will print `"{n} is not a power of 2"` instead of `"3 is not a power of 2"` as intended

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1196?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 89.07%. Comparing base [(`bfe541a`)](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/bfe541a6d7f63b7a8402d86aa51e741c7d1c97bd?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) to head [(`b413522`)](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1196?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    #1196      +/-   ##
==========================================
- Coverage   89.42%   89.07%   -0.36%     
==========================================
  Files          43       43              
  Lines        9139     9139              
  Branches     2175     1948     -227     
==========================================
- Hits         8173     8141      -32     
- Misses        789      822      +33     
+ Partials      177      176       -1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1196?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).

whitequark: Actually, care to add a test?
whitequark: Also, your commit message should be formatted something like

    utils: F-strings are missing the letter "f"
mcclure: Did a forced push with a corrected summary plus a test.

I tested with `pdm install && pdm run test` and although I am almost totally certain the test is correct and passes, I'm not sure I ran the tests right because I got 10 failures in other areas. I think this test is right though because when I changed "not" to "nfot" in the test string the failure count went up to 11 :P
wanda-phi: > Did a forced push with a corrected summary plus a test.
> 
> I tested with `pdm install && pdm run test` and although I am almost totally certain the test is correct and passes, I'm not sure I ran the tests right because I got 10 failures in other areas. I think this test is right though because when I changed "not" to "nfot" in the test string the failure count went up to 11 :P

I don't actually see any test?
mcclure: Apologies, should have got that the first time.

Patch:
diff --git a/amaranth/utils.py b/amaranth/utils.py
index cb2e1a1..014ef40 100644
--- a/amaranth/utils.py
+++ b/amaranth/utils.py
@@ -13,7 +13,7 @@ def ceil_log2(n):
     """
     n = operator.index(n)
     if n < 0:
-        raise ValueError("{n} is negative")
+        raise ValueError(f"{n} is negative")
     if n == 0:
         return 0
     return (n - 1).bit_length()
@@ -26,7 +26,7 @@ def exact_log2(n):
     """
     n = operator.index(n)
     if n <= 0 or (n & (n - 1)):
-        raise ValueError("{n} is not a power of 2")
+        raise ValueError(f"{n} is not a power of 2")
     return (n - 1).bit_length()
 
 


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index feb5147..4e11f71 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -16,7 +16,7 @@ class Log2TestCase(unittest.TestCase):
         self.assertEqual(ceil_log2(9), 4)
         with self.assertRaises(TypeError):
             ceil_log2(1.5)
-        with self.assertRaises(ValueError):
+        with self.assertRaisesRegex(ValueError, r"^-1 is negative$"):
             ceil_log2(-1)
 
     def test_exact_log2(self):
@@ -25,7 +25,7 @@ class Log2TestCase(unittest.TestCase):
         self.assertEqual(exact_log2(4), 2)
         self.assertEqual(exact_log2(8), 3)
         for val in [-1, 0, 3, 5, 6, 7, 9]:
-            with self.assertRaises(ValueError):
+            with self.assertRaisesRegex(ValueError, (f"^{val} is not a power of 2$")):
                 exact_log2(val)
         with self.assertRaises(TypeError):
             exact_log2(1.5)