@@ -79,7 +79,7 @@ def parse(version):
7979 >>> ver['build']
8080 'build.4'
8181 """
82- return VersionInfo .parse (version )._asdict ()
82+ return VersionInfo .parse (version ).to_dict ()
8383
8484
8585def comparator (operator ):
@@ -186,7 +186,22 @@ def build(self, value):
186186 def _astuple (self ):
187187 return (self .major , self .minor , self .patch , self .prerelease , self .build )
188188
189- def _asdict (self ):
189+ def to_dict (self ):
190+ """
191+ Convert the VersionInfo object to an OrderedDict.
192+
193+ .. versionadded:: 2.9.2
194+ Renamed from ``VersionInfo._asdict`` to ``VersionInfo.to_dict`` to
195+ make this function available in the public API.
196+
197+ :return: an OrderedDict with the keys in the order ``major``, ``minor``,
198+ ``patch``, ``prerelease``, and ``build``.
199+ :rtype: :class:`collections.OrderedDict`
200+
201+ >>> semver.VersionInfo(3, 2, 1).to_dict()
202+ OrderedDict([('major', 3), ('minor', 2), ('patch', 1), \
203+ ('prerelease', None), ('build', None)])
204+ """
190205 return collections .OrderedDict (
191206 (
192207 ("major" , self .major ),
@@ -197,6 +212,9 @@ def _asdict(self):
197212 )
198213 )
199214
215+ # For compatibility reasons:
216+ _asdict = deprecated ("to_dict" )(to_dict )
217+
200218 def __iter__ (self ):
201219 """Implement iter(self)."""
202220 # As long as we support Py2.7, we can't use the "yield from" syntax
@@ -286,30 +304,30 @@ def bump_build(self, token="build"):
286304
287305 @comparator
288306 def __eq__ (self , other ):
289- return _compare_by_keys (self ._asdict (), _to_dict (other )) == 0
307+ return _compare_by_keys (self .to_dict (), _to_dict (other )) == 0
290308
291309 @comparator
292310 def __ne__ (self , other ):
293- return _compare_by_keys (self ._asdict (), _to_dict (other )) != 0
311+ return _compare_by_keys (self .to_dict (), _to_dict (other )) != 0
294312
295313 @comparator
296314 def __lt__ (self , other ):
297- return _compare_by_keys (self ._asdict (), _to_dict (other )) < 0
315+ return _compare_by_keys (self .to_dict (), _to_dict (other )) < 0
298316
299317 @comparator
300318 def __le__ (self , other ):
301- return _compare_by_keys (self ._asdict (), _to_dict (other )) <= 0
319+ return _compare_by_keys (self .to_dict (), _to_dict (other )) <= 0
302320
303321 @comparator
304322 def __gt__ (self , other ):
305- return _compare_by_keys (self ._asdict (), _to_dict (other )) > 0
323+ return _compare_by_keys (self .to_dict (), _to_dict (other )) > 0
306324
307325 @comparator
308326 def __ge__ (self , other ):
309- return _compare_by_keys (self ._asdict (), _to_dict (other )) >= 0
327+ return _compare_by_keys (self .to_dict (), _to_dict (other )) >= 0
310328
311329 def __repr__ (self ):
312- s = ", " .join ("%s=%r" % (key , val ) for key , val in self ._asdict ().items ())
330+ s = ", " .join ("%s=%r" % (key , val ) for key , val in self .to_dict ().items ())
313331 return "%s(%s)" % (type (self ).__name__ , s )
314332
315333 def __str__ (self ):
@@ -376,12 +394,12 @@ def replace(self, **parts):
376394 parts
377395 :raises: TypeError, if ``parts`` contains invalid keys
378396 """
379- version = self ._asdict ()
397+ version = self .to_dict ()
380398 version .update (parts )
381399 try :
382400 return VersionInfo (** version )
383401 except TypeError :
384- unknownkeys = set (parts ) - set (self ._asdict ())
402+ unknownkeys = set (parts ) - set (self .to_dict ())
385403 error = "replace() got %d unexpected keyword " "argument(s): %s" % (
386404 len (unknownkeys ),
387405 ", " .join (unknownkeys ),
@@ -409,9 +427,9 @@ def isvalid(cls, version):
409427
410428def _to_dict (obj ):
411429 if isinstance (obj , VersionInfo ):
412- return obj ._asdict ()
430+ return obj .to_dict ()
413431 elif isinstance (obj , tuple ):
414- return VersionInfo (* obj )._asdict ()
432+ return VersionInfo (* obj ).to_dict ()
415433 return obj
416434
417435
@@ -512,8 +530,8 @@ def compare(ver1, ver2):
512530 0
513531 """
514532
515- v1 = VersionInfo .parse (ver1 )._asdict ()
516- v2 = VersionInfo .parse (ver2 )._asdict ()
533+ v1 = VersionInfo .parse (ver1 ).to_dict ()
534+ v2 = VersionInfo .parse (ver2 ).to_dict ()
517535
518536 return _compare_by_keys (v1 , v2 )
519537
0 commit comments