Prompt file imported from wilfredinni/dinero (
.github/prompts/tool.prompt.md). Copyright stays with the author.
Create a new currency conversion tool for the Dinero Python library that:
-
Implements a
convert()method for Dinero objects with the following signature:def convert(self, exchange_rate: str | float, currency: Currency) -> Dinero -
Enables conversion of Dinero objects to different currencies using:
- A specified exchange rate
- Target currency specification
- Built-in currency objects (e.g., USD, CLP)
-
Requirements:
- Accept both string and float inputs for exchange rates
- Validate exchange rates (must be positive non-zero numbers)
- Verify currency objects are valid
- Perform accurate mathematical calculations maintaining precision
- Return a new Dinero object in the target currency
-
Error Handling:
- Raise ValueError for invalid exchange rates (negative, zero, non-numeric)
- Raise TypeError for invalid currency objects
- Include descriptive error messages
Example Usage:
from dinero import Dinero
from dinero.currencies import USD, CLP
# Convert USD to CLP
usd_amount = Dinero(amount="100", currency=USD)
clp_amount = usd_amount.convert(exchange_rate="750", currency=CLP)
# Returns: Dinero(75000, CLP)
- Testing:
- Write unit tests for the
convert()method - Test with various exchange rates and currency combinations
- Ensure edge cases are covered (e.g., zero, negative rates)
- Validate that the returned Dinero object has the correct amount and currency
- Write unit tests for the