You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If more classes share the same base class the to_dict and from_dict functions get only generated for the class which first called to_dict or from dict. This is because all classes use the __dialect_to_dict_cache__ attribute from the base class. So the first execution of to_dict generates the function and put it into the __dialect_to_dict_cache__ from the base class. Every call of to_dict in a different class uses the fuction from the cache.
What I Did
In the following example the call entity2.to_dict() raises an exception because the to_dict function from Entity1 is used.
This can be seen by the function adress in the __dialect_to_dict_cache__ Dictionary.
{'dt1': '31/12/2021'}
Entity1 {<class '__main__.EthiopianDialect'>: <function to_dict at 0x7fa213792dd0>}
Entity2 {<class '__main__.EthiopianDialect'>: <function to_dict at 0x7fa213792dd0>}
Traceback (most recent call last):
File "/workspaces/ccn-api-server/scripts/demo/performance/issues/mashumaro_dialiect_issue.py", line 52, in <module>
print(entity2.to_dict(dialect=EthiopianDialect))
File "<string>", line 15, in to_dict
File "<string>", line 3, in to_dict
AttributeError: 'Entity2' object has no attribute 'dt1'. Did you mean: 'dt2'?
Possible solution
In mashamuro/core/meta/builder.py - CodeBuilder the hasattr function is used to check if dialect_to_dict_cache exists in this class. Bus hasattr also searches in the BaseClasses. So instead of hasattr one could check in cls.__dict__ for the existence. So only the actual class is checked.
defadd_to_dict(self) ->None:
self.reset()
dialects_feature=self.is_code_generation_option_enabled(ADD_DIALECT_SUPPORT)
ifdialects_feature:
# self.add_line("if not hasattr(cls, '__dialect_to_dict_cache__'):") # CHANGEDself.add_line("if not '__dialect_to_dict_cache__' in cls.__dict__:") # CHANGEDwithself.indent():
self.add_line("cls.__dialect_to_dict_cache__ = {}")
ifnotdialects_featureordialects_featureandself.dialect:
returnself._add_to_dict()
...
defadd_from_dict(self) ->None:
self.reset()
dialects_feature=self.is_code_generation_option_enabled(ADD_DIALECT_SUPPORT)
ifdialects_feature:
#self.add_line("if not hasattr(cls, '__dialect_from_dict_cache__'):") # CHANGEDself.add_line("if not '__dialect_from_dict_cache__' in cls.__dict__:") # CHANGEDwithself.indent():
self.add_line("cls.__dialect_from_dict_cache__ = {}")
ifnotdialects_featureordialects_featureandself.dialect:
returnself._add_from_dict()
...
The text was updated successfully, but these errors were encountered:
Description
If more classes share the same base class the
to_dict
andfrom_dict
functions get only generated for the class which first calledto_dict
orfrom dict
. This is because all classes use the__dialect_to_dict_cache__
attribute from the base class. So the first execution ofto_dict
generates the function and put it into the__dialect_to_dict_cache__
from the base class. Every call ofto_dict
in a different class uses the fuction from the cache.What I Did
In the following example the call
entity2.to_dict()
raises an exception because theto_dict
function from Entity1 is used.This can be seen by the function adress in the
__dialect_to_dict_cache__
Dictionary.Output:
Possible solution
In mashamuro/core/meta/builder.py - CodeBuilder the hasattr function is used to check if dialect_to_dict_cache exists in this class. Bus
hasattr
also searches in the BaseClasses. So instead ofhasattr
one could check incls.__dict__
for the existence. So only the actual class is checked.The text was updated successfully, but these errors were encountered: