-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting currency instance from its code #17
Comments
@flaeppe You're correct, that's not supported yet, and I agree it should be! I did start tinkering with it a little bit on this branch: https://github.com/antonagestam/immoney/tree/feature/currency-registry I wanted to implement it in a somewhat generic way, so that it's not mandatory to import and use Implementing Pydantic parsing should be straight-forward, but serialization probably needs a hack in user code to map For now, if you'd like to work on any of the above, feel free to go ahead! But as stated, support for Pydantic 1.x might be ruthlessly removed once 2.x is out (I'm ok with adding it as a dependency if needed for now though). Also note that I think the serialized format for class Foo(BaseModel):
foo: str
value: Money Becomes: {
"foo": "foo",
"value": {"value": "23.40", "currency": "SEK"},
} |
For the requirements I have, right now, only support for I suppose, for the time being, one could also take a little bit more manual/verbose approach of doing something like below, for class Foo(BaseModel):
amount: Decimal
currency: Currency
@root_validator()
def validate_is_money(cls, values: dict[str, Any]) -> dicr[str, Any]:
try
Money(values.get("amount"), values.get("currency"))
except SomeException as exc:
raise ValidationError() from exc
return values
@cached_property
def money(self) -> Money:
return Money(self.amount, self.currency) (I suppose there could exist some corresponding variant using private model attributes as well) But I get the reasoning of being able to serialize/parse as a Anyways, some currency registry and currency supported as pydantic type would be great. |
@flaeppe I'll probably be able to get a basic currency registry out soon :) |
Looks great! |
If I'm not missing something, I don't think I can get a
Currency
instance from its code? Is that something that could be natively supported?Reasoning is that I'm looking in to support for a
Currency
type for apydantic
model field. So I also have a follow up question on if supportingpydantic
would be of interest?I could probably monkey patch the
Currency
type to supportpydantic
while also mapping up code -> Currency to get it working. But was curious if any of the above support would be interesting to package.The text was updated successfully, but these errors were encountered: