-
-
Notifications
You must be signed in to change notification settings - Fork 108
Support Decimal and MutableMapping codecs, add a multiline-Array indent control
#294
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
base: master
Are you sure you want to change the base?
Support Decimal and MutableMapping codecs, add a multiline-Array indent control
#294
Conversation
|
Bah, going through rebase to master 🏄🏼 |
For `decimal.Decimal` we can just encode as a `String` and leave it up to the user how to decode the value on load. For `dict` it's quite handy to just render any type that quacks like a `MutableMapping` to a table. Resolves python-poetry#288 and python-poetry#289 but still needs tests!
Simply casts to `Decimal` then calls `item()` on it as normal for most basic types.
Ensure it both unwraps as a `String` (or `str`) and that the `api.decimal()` encoder routine does the same.
9823394 to
8e0239a
Compare
|
There hopefully i got all the latest changes in now B) |
Decimal and MutableMapping decode, add a multiline-Array indent controlDecimal and MutableMapping codecs, add a multiline-Array indent control
|
Also, if this patch is likely to never be accepted or needs to be changed to be accepted please let me know asap since we'd much rather not have to maintain our own fork :) |
|
a better way to support this and similar cases is to allow passing a custom encoder to item() function. i don't think we need round trip |
|
@frostming ah ok, I'm fine to change it to do that. (also sorry i missed your comment don't have my email hooked up rn for GH 😂)
can you show an example that might already exist for doing this btw in the code base and for which feature addition out of the issues I made does this apply? All?
Also can you clarify wym by that? |
|
You can convert a decimal to a float representation in TOML, but can't convert it back, and Decimal isn't listed as the data types defined by TOML spec, so it's not worth to expose a new api and item type for it. Instead, I'd propose something like this: import tomlkit
a = Decimal('34.56')
def my_encoder(o):
if isinstance(o, Decimal):
return tomlkit.float_(o)
raise ValueError(f"Invalid type {type(value)}")
item = tomlkit.item(a, encoder=my_encoder) # a Float object
v = {"a": Decimal("34.56")}
print(tomlkit.dumps(v, encoder=my_encoder)
# a = 34.56In this way users can convert custom types, not only Decimal, to TOML items in tomlkit. And it is clearer that users can't convert it back(parse anything as Decimal) |
| def multiline( | ||
| self, | ||
| multiline: bool, | ||
| indent: str = " "*4, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer to use int indent=4, to prevent passing arbitrary strings.
@frostming works for me. So I guess i'll just drop all the Any other quiffs and/or do you have any suggestions for the tests I still need to do? |
|
@frostming thanks for the follow up! i will hopefully get back to this PR this week. Will check out that landed PR and change our stuff to match first, then come back here and drop the unneeded history. Still kinda looking for a little guidance on how thorough the new tests for the multi-line arrays should be? |
In an attempt to resolve the feature request issues i submitted:
Decimalserialization? #288bidictinputs? #289Array#290Summary:
Array._multiline_indent: strand expose it through aindent: strkwarg to.multiline()..items.item()encoder to handledecimal.Decimaland match againstMutableMappinginstead ofdict.Possibly still todo?
DecimalandMutableMappingchanges?Arrayindent parameter: