Skip to content

Commit d57a1e7

Browse files
gipertclaude
andcommitted
fix(vis): only strip Quantity for plain (non-pint) format specs
The previous fix stripped magnitudes from every Quantity in leg_dat, but the default-spec branch in WaveformBrowser.__init__ substitutes '0.3gP~' for entries without an explicit format. Stripping there made the format call ValueError out because '0.3gP~' is invalid for plain floats. Parse each legend format string and only strip Quantities whose spec has no pint formatter character (~, P, H, L, C, D). Default '0.3gP~' and explicit pint specs keep the Quantity; user-provided plain specs like '0.1f' get the magnitude so pint's DeprecationWarning does not fire. Also add tests/test_units.py to lock in the application-registry sharing contract (importing dspeed must not replace pint's app registry, and consumer-created quantities must interoperate with dspeed.units.unit_registry quantities). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 7f07406 commit d57a1e7

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/dspeed/vis/waveform_browser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,14 @@ def draw_current(self, clear: bool = True) -> None:
549549
leg_labels.append(form)
550550
else:
551551
for form in self.legend_format:
552+
plain_names = {
553+
name
554+
for _, name, spec, _ in string.Formatter().parse(form)
555+
if name and spec and not (set(spec) & set("~PHLCD"))
556+
}
552557
for leg_dat in leg_cycle:
553558
leg_dat = {
554-
k: v.m if isinstance(v, Quantity) else v
559+
k: v.m if k in plain_names and isinstance(v, Quantity) else v
555560
for k, v in leg_dat.items()
556561
}
557562
leg_labels.append(form.format(**leg_dat))

tests/test_units.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pint
2+
3+
4+
def test_shares_application_registry():
5+
# Importing dspeed.units must not replace the application registry, and
6+
# quantities created via the bare pint API must interoperate with dspeed's
7+
# registry (regression for cross-registry errors in consumer code).
8+
app_reg_before = pint.get_application_registry()
9+
from dspeed.units import unit_registry as ureg
10+
11+
assert pint.get_application_registry() is app_reg_before
12+
13+
consumer_q = pint.Quantity(1, "us")
14+
dspeed_q = ureg.Quantity(1000, "ns")
15+
assert (consumer_q + dspeed_q).to("us").magnitude == 2

0 commit comments

Comments
 (0)