Skip to content

Commit

Permalink
Support subtypes of basic types when encoding (#428)
Browse files Browse the repository at this point in the history
The current implementation of `TomlEncoder` doesn't support subtypes of the supported basic types. For example:

```py
from enum import StrEnum
import toml

class ValidStrings(StrEnum):
    FOO = "foo"

assert isinstance(ValidStrings.FOO, str) # OK
data = {"foo": ValidStrings.FOO}
print(toml.dumps(data))
```

Output:
```
foo = [ "f", "o", "o",]
```

This pull request changes the implementation to use `isinstance` instead of `get(type(...))` to play nice with inheritence.

Fixes #418
  • Loading branch information
abrahammurciano authored Sep 20, 2023
1 parent 5706d31 commit 7ffcfa3
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion toml/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def dump_inline_table(self, section):

def dump_value(self, v):
# Lookup function corresponding to v's type
dump_fn = self.dump_funcs.get(type(v))
dump_fn = next(f for t, f in self.dump_funcs.items() if isinstance(v, t), None)
if dump_fn is None and hasattr(v, '__iter__'):
dump_fn = self.dump_funcs[list]
# Evaluate function (if it exists) else return v
Expand Down

0 comments on commit 7ffcfa3

Please sign in to comment.