@@ -13,11 +13,11 @@ def test_yamlable_incomplete_description():
1313 with pytest .raises (NotImplementedError ) as err_info :
1414 class Foo (YamlAble ):
1515 # __yaml_tag_suffix__ = 'foo'
16- def to_yaml_dict (self ) -> Dict [str , Any ]:
16+ def __to_yaml_dict__ (self ) -> Dict [str , Any ]:
1717 return copy (vars (self ))
1818
1919 @classmethod
20- def from_yaml_dict (cls : 'Type[Y]' , dct : Dict , yaml_tag : str ) -> Y :
20+ def __from_yaml_dict__ (cls : 'Type[Y]' , dct : Dict , yaml_tag : str ) -> Y :
2121 return Foo (** dct )
2222
2323 # instantiate
@@ -43,11 +43,11 @@ def __init__(self, a, b):
4343 def __eq__ (self , other ):
4444 return vars (self ) == vars (other )
4545
46- def to_yaml_dict (self ) -> Dict [str , Any ]:
46+ def __to_yaml_dict__ (self ) -> Dict [str , Any ]:
4747 return copy (vars (self ))
4848
4949 @classmethod
50- def from_yaml_dict (cls : 'Type[Y]' , dct : Dict , yaml_tag : str ) -> Y :
50+ def __from_yaml_dict__ (cls : 'Type[Y]' , dct : Dict , yaml_tag : str ) -> Y :
5151 return Foo (** dct )
5252
5353 # instantiate
@@ -80,6 +80,68 @@ def close(self):
8080 assert f == load (y )
8181
8282
83+ def test_yamlable_legacy_method_names ():
84+ """ Tests that YamlAbleMixIn works correctly """
85+
86+ global enc
87+ global dec
88+ enc , dec = False , False
89+
90+ @yaml_info (yaml_tag_ns = 'yaml.tests' )
91+ class FooLegacy (YamlAble ):
92+ # __yaml_tag_suffix__ = 'foo' not needed: we used @yaml_info
93+
94+ def __init__ (self , a , b ):
95+ self .a = a
96+ self .b = b
97+
98+ def __eq__ (self , other ):
99+ return vars (self ) == vars (other )
100+
101+ def to_yaml_dict (self ) -> Dict [str , Any ]:
102+ global enc
103+ enc = True
104+ return copy (vars (self ))
105+
106+ @classmethod
107+ def from_yaml_dict (cls : 'Type[Y]' , dct : Dict , yaml_tag : str ) -> Y :
108+ global dec
109+ dec = True
110+ return FooLegacy (** dct )
111+
112+ # instantiate
113+ f = FooLegacy (1 , 'hello' )
114+
115+ # dump
116+ y = f .dumps_yaml ()
117+ assert y == "!yamlable/yaml.tests.FooLegacy {a: 1, b: hello}\n "
118+
119+ # dump io
120+ class MemorizingStringIO (StringIO ):
121+ """ A StringIO object that memorizes its buffer when it is closed (as opposed to the standard StringIO) """
122+ def close (self ):
123+ self .value = self .getvalue ()
124+ super (StringIO , self ).close ()
125+ s = MemorizingStringIO ()
126+ f .dump_yaml (s )
127+ assert s .value == y
128+
129+ # dump pyyaml
130+ assert dump (f ) == y
131+
132+ # load
133+ assert f == FooLegacy .loads_yaml (y )
134+
135+ # load io
136+ assert f == FooLegacy .load_yaml (StringIO (y ))
137+
138+ # load pyyaml
139+ assert f == load (y )
140+
141+ assert enc
142+ assert dec
143+
144+
83145# TODO override so that tag is not supported, to check error message
84146def test_yamlable_not_supported ():
85147
@@ -94,11 +156,11 @@ def __init__(self, a, b):
94156 def __eq__ (self , other ):
95157 return vars (self ) == vars (other )
96158
97- def to_yaml_dict (self ) -> Dict [str , Any ]:
159+ def __to_yaml_dict__ (self ) -> Dict [str , Any ]:
98160 return copy (vars (self ))
99161
100162 @classmethod
101- def from_yaml_dict (cls : 'Type[Y]' , dct : Dict , yaml_tag : str ) -> Y :
163+ def __from_yaml_dict__ (cls : 'Type[Y]' , dct : Dict , yaml_tag : str ) -> Y :
102164 return Foo_Err (** dct )
103165
104166 @classmethod
0 commit comments