Skip to content

Commit 8ed2f11

Browse files
committed
fix: add max_value to decimal_point validation
Add max_value=10 to RangeValidationError for decimal_point validation in encode_price() and decode_price(). This provides complete range information (0-10) for better user guidance. Changes: - Added max_value=10 to both validation checks - Updated error messages to show complete range - Updated docstrings to document the 0-10 range - Changed from '>= 0' to 'between 0 and 10' validation Rationale: - Tests use decimal_point=5 - Financial precision typically needs <= 5 decimal places - 32-bit device storage supports up to 6 decimal places safely - Max of 10 is generous but prevents absurd values Addresses Copilot AI feedback about missing max_value attribute.
1 parent b4f9a07 commit 8ed2f11

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/nwp500/encoding.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ def encode_price(value: Real, decimal_point: int) -> int:
198198
199199
Args:
200200
value: Price value (float or Decimal)
201-
decimal_point: Number of decimal places (0-4 typically)
201+
decimal_point: Number of decimal places (0-10, typically 2-5)
202202
203203
Returns:
204204
Integer representation of the price
205205
206206
Raises:
207-
ValueError: If decimal_point is negative
207+
RangeValidationError: If decimal_point is not in range 0-10
208208
209209
Examples:
210210
>>> encode_price(12.34, 2)
@@ -216,12 +216,13 @@ def encode_price(value: Real, decimal_point: int) -> int:
216216
>>> encode_price(100, 0)
217217
100
218218
"""
219-
if decimal_point < 0:
219+
if not 0 <= decimal_point <= 10:
220220
raise RangeValidationError(
221-
"decimal_point must be >= 0",
221+
"decimal_point must be between 0 and 10",
222222
field="decimal_point",
223223
value=decimal_point,
224224
min_value=0,
225+
max_value=10,
225226
)
226227
scale = 10**decimal_point
227228
return int(round(float(value) * scale))
@@ -233,13 +234,13 @@ def decode_price(value: int, decimal_point: int) -> float:
233234
234235
Args:
235236
value: Integer price value from device
236-
decimal_point: Number of decimal places
237+
decimal_point: Number of decimal places (0-10, typically 2-5)
237238
238239
Returns:
239240
Floating-point price value
240241
241242
Raises:
242-
ValueError: If decimal_point is negative
243+
RangeValidationError: If decimal_point is not in range 0-10
243244
244245
Examples:
245246
>>> decode_price(1234, 2)
@@ -251,12 +252,13 @@ def decode_price(value: int, decimal_point: int) -> float:
251252
>>> decode_price(100, 0)
252253
100.0
253254
"""
254-
if decimal_point < 0:
255+
if not 0 <= decimal_point <= 10:
255256
raise RangeValidationError(
256-
"decimal_point must be >= 0",
257+
"decimal_point must be between 0 and 10",
257258
field="decimal_point",
258259
value=decimal_point,
259260
min_value=0,
261+
max_value=10,
260262
)
261263
scale = 10**decimal_point
262264
return value / scale if scale else float(value)

0 commit comments

Comments
 (0)