Skip to content
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

Subclass of dict is not recognized since 0.9 #57

Open
pwwang opened this issue Dec 6, 2022 · 5 comments
Open

Subclass of dict is not recognized since 0.9 #57

pwwang opened this issue Dec 6, 2022 · 5 comments

Comments

@pwwang
Copy link
Contributor

pwwang commented Dec 6, 2022

Python 3.9.5 (default, Jun  4 2021, 12:28:51) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import rtoml                                                

In [2]: rtoml.VERSION                                               
Out[2]: '0.8.0'

In [3]: class MyDict(dict): ...                                     

In [4]: d = MyDict(a=1)                                             

In [5]: rtoml.dumps(d)                                              
Out[5]: 'a = 1\n'
Python 3.9.5 (default, Jun  4 2021, 12:28:51) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import rtoml                                                                     

In [2]: rtoml.__version__                                                                
Out[2]: '0.9.0'

In [3]: class MyDict(dict): ...                                                          

In [4]: d = MyDict(a=1)                                                                  

In [5]: d                                                                                
Out[5]: {'a': 1}

In [6]: rtoml.dumps(d)                                                                   
---------------------------------------------------------------------------
TomlSerializationError                    Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 rtoml.dumps(d)

File ~/miniconda3/lib/python3.9/site-packages/rtoml/__init__.py:49, in dumps(obj, pretty)
     46 else:
     47     serialize = _rtoml.serialize
---> 49 return serialize(obj)

TomlSerializationError: {'a': 1} (MyDict) is not serializable to TOML
@samuelcolvin
Copy link
Owner

Makes sense we need to try the next tp_base like here.

Shouldn't be too hard to fix, would you like to have a try.

pwwang added a commit to pwwang/python-simpleconf that referenced this issue Feb 7, 2023
@pwwang
Copy link
Contributor Author

pwwang commented Jun 25, 2024

This still prevents me from upgrading rtoml to 0.11 for some of my projects.

Originally comments with the PR:

Can we add something to make it work at the python end?

For example, at dump(obj, ...) or dumps(obj, ...), can we do:

def dump(obj, ...):
    if issubclass(type(obj), dict):
        obj = dict(obj)
    # continue logic

@samuelcolvin
Copy link
Owner

well the problem is it needs to be recursive.

You could add your own method to do this. By the way, it's better to use use type(x) in a lookup, then work through the __mro__, like we did in pydantic v1.

@pwwang
Copy link
Contributor Author

pwwang commented Jun 25, 2024

well the problem is it needs to be recursive.

That's a good point. Maybe I'll just wrap it myself.

Feel free to close.

@samuelcolvin
Copy link
Owner

Ultimate solution is pydantic/pydantic-core#1339, I'll leave this open for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants