domain.value_objects.money¶
src.domain.value_objects.money
¶
Immutable Money value object with Decimal precision.
Financial calculations require exact precision - floats introduce rounding errors that accumulate in financial systems. This module provides a Money value object using Python's Decimal type.
Error Handling
Arithmetic operations between different currencies raise CurrencyMismatchError (a ValueError subclass). This follows Python's convention for type-incompatible operations (e.g., str + int raises TypeError). This is compliant with our error handling architecture - see "Value Object Arithmetic Exceptions" section in docs/architecture/error-handling-architecture.md for rationale.
Reference
- docs/architecture/account-domain-model.md
- docs/architecture/error-handling-architecture.md
Usage
from decimal import Decimal from src.domain.value_objects import Money
balance = Money(Decimal("1000.00"), "USD") fee = Money(Decimal("9.99"), "USD") result = balance - fee # Money(990.01, USD)
Classes¶
CurrencyMismatchError
¶
Bases: ValueError
Raised when attempting operations on different currencies.
Source code in src/domain/value_objects/money.py
Functions¶
__init__
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
currency1
|
str
|
First currency code. |
required |
currency2
|
str
|
Second currency code. |
required |
Source code in src/domain/value_objects/money.py
Money
dataclass
¶
Immutable monetary value with currency.
Financial calculations require exact precision - floats introduce rounding errors that accumulate in financial systems. This class uses Python's Decimal for exact decimal arithmetic.
Attributes:
| Name | Type | Description |
|---|---|---|
amount |
Decimal
|
Decimal value (positive, negative, or zero). |
currency |
str
|
ISO 4217 currency code (e.g., "USD", "EUR"). |
Immutability
Frozen dataclass ensures Money cannot be modified after creation. All arithmetic operations return new Money instances.
Currency Safety
Operations between different currencies raise CurrencyMismatchError. This prevents accidental mixing of currencies without conversion.
Example
from decimal import Decimal balance = Money(Decimal("1000.00"), "USD") fee = Money(Decimal("9.99"), "USD") balance - fee Money(amount=Decimal('990.01'), currency='USD')
Warning
Always use string initialization for Decimal to avoid float precision:
Money(Decimal("0.1"), "USD") # Correct Money(Decimal(0.1), "USD") # Wrong - already imprecise!
Source code in src/domain/value_objects/money.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
Functions¶
__post_init__
¶
Validate money after initialization.
Raises:
| Type | Description |
|---|---|
ValueError
|
If amount is not a valid Decimal or currency is invalid. |
Source code in src/domain/value_objects/money.py
__add__
¶
Add two Money values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Money
|
Money to add. |
required |
Returns:
| Type | Description |
|---|---|
Money
|
New Money with sum of amounts. |
Raises:
| Type | Description |
|---|---|
CurrencyMismatchError
|
If currencies differ. |
TypeError
|
If other is not Money. |
Source code in src/domain/value_objects/money.py
__sub__
¶
Subtract two Money values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Money
|
Money to subtract. |
required |
Returns:
| Type | Description |
|---|---|
Money
|
New Money with difference of amounts. |
Raises:
| Type | Description |
|---|---|
CurrencyMismatchError
|
If currencies differ. |
TypeError
|
If other is not Money. |
Source code in src/domain/value_objects/money.py
__mul__
¶
Multiply Money by a scalar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scalar
|
Decimal | int | float
|
Number to multiply by. |
required |
Returns:
| Type | Description |
|---|---|
Money
|
New Money with scaled amount. |
Example
balance = Money(Decimal("100.00"), "USD") balance * 2 Money(amount=Decimal('200.00'), currency='USD')
Source code in src/domain/value_objects/money.py
__rmul__
¶
Right multiply (scalar * Money).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scalar
|
Decimal | int | float
|
Number to multiply by. |
required |
Returns:
| Type | Description |
|---|---|
Money
|
New Money with scaled amount. |
__neg__
¶
Negate the amount.
Returns:
| Type | Description |
|---|---|
Money
|
New Money with negated amount. |
Example
debt = Money(Decimal("100.00"), "USD") -debt Money(amount=Decimal('-100.00'), currency='USD')
Source code in src/domain/value_objects/money.py
__lt__
¶
Less than comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Money
|
Money to compare. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this amount is less than other. |
Raises:
| Type | Description |
|---|---|
CurrencyMismatchError
|
If currencies differ. |
Source code in src/domain/value_objects/money.py
__le__
¶
Less than or equal comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Money
|
Money to compare. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this amount is less than or equal to other. |
Raises:
| Type | Description |
|---|---|
CurrencyMismatchError
|
If currencies differ. |
Source code in src/domain/value_objects/money.py
__gt__
¶
Greater than comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Money
|
Money to compare. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this amount is greater than other. |
Raises:
| Type | Description |
|---|---|
CurrencyMismatchError
|
If currencies differ. |
Source code in src/domain/value_objects/money.py
__ge__
¶
Greater than or equal comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Money
|
Money to compare. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this amount is greater than or equal to other. |
Raises:
| Type | Description |
|---|---|
CurrencyMismatchError
|
If currencies differ. |
Source code in src/domain/value_objects/money.py
is_positive
¶
Check if amount is positive (> 0).
Returns:
| Type | Description |
|---|---|
bool
|
True if amount is greater than zero. |
is_negative
¶
Check if amount is negative (< 0).
Returns:
| Type | Description |
|---|---|
bool
|
True if amount is less than zero. |
is_zero
¶
zero
classmethod
¶
Create Money with zero amount.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
currency
|
str
|
Currency code (default: USD). |
'USD'
|
Returns:
| Type | Description |
|---|---|
Self
|
Money with zero amount in specified currency. |
Example
Money.zero("EUR") Money(amount=Decimal('0'), currency='EUR')
Source code in src/domain/value_objects/money.py
from_cents
classmethod
¶
Create Money from cents (or smallest unit).
Useful for APIs that return amounts in cents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cents
|
int
|
Amount in cents/smallest currency unit. |
required |
currency
|
str
|
Currency code (default: USD). |
'USD'
|
Returns:
| Type | Description |
|---|---|
Self
|
Money with converted amount. |
Example
Money.from_cents(12345, "USD") Money(amount=Decimal('123.45'), currency='USD')
Note
Assumes 100 cents per unit. For currencies with different subdivisions (e.g., JPY has no cents), use direct construction.
Source code in src/domain/value_objects/money.py
__repr__
¶
Return repr for debugging.
Returns:
| Type | Description |
|---|---|
str
|
Detailed string representation. |
__str__
¶
Return human-readable string.
Returns:
| Type | Description |
|---|---|
str
|
Formatted string like "1,234.56 USD". |
Source code in src/domain/value_objects/money.py
Functions¶
validate_currency
¶
Validate and normalize currency code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Currency code (case-insensitive). |
required |
Returns:
| Type | Description |
|---|---|
str
|
Uppercase ISO 4217 currency code. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If code is not a valid ISO 4217 currency. |
Example
validate_currency("usd") 'USD' validate_currency("XYZ") ValueError: Invalid currency code: XYZ