Julian / venvs

venvs creates virtualenvs
https://pypi.org/project/venvs/
MIT License
17 stars 12 forks source link

Subclasses of basic types aren't properly dumped #114

Closed abrahammurciano closed 1 year ago

abrahammurciano commented 1 year ago

If I have a custom class that's a subtype of say str, I'd expect it to be dumped the same way as a string. For example:

from enum import StrEnum
import toml

class ValidStrings(StrEnum):
    FOO = "foo"

data = {"foo": ValidStrings.FOO}
print(toml.dumps(data))

Output:

foo = [ "f", "o", "o",]

I've done some investigating and it seems to be because in TomlEncoder.dump_value it tries to determine the dump function like this:

dump_fn = self.dump_funcs.get(type(v))

Perhaps isinstance should be used instead to play nice with inheritence? I'd suggest this:

dump_fn = next(f for t, f in self.dump_funcs.items() if isinstance(v, t), None)

I'd be happy to open a pull request for this

abrahammurciano commented 1 year ago

This is awfully embarrassing... I seem to have opened this issue in the wrong repository... This was meant for toml