@@ -79,7 +79,7 @@ def parse(version):
79
79
>>> ver['build']
80
80
'build.4'
81
81
"""
82
- return VersionInfo .parse (version )._asdict ()
82
+ return VersionInfo .parse (version ).to_dict ()
83
83
84
84
85
85
def comparator (operator ):
@@ -186,7 +186,22 @@ def build(self, value):
186
186
def _astuple (self ):
187
187
return (self .major , self .minor , self .patch , self .prerelease , self .build )
188
188
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
+ """
190
205
return collections .OrderedDict (
191
206
(
192
207
("major" , self .major ),
@@ -197,6 +212,9 @@ def _asdict(self):
197
212
)
198
213
)
199
214
215
+ # For compatibility reasons:
216
+ _asdict = deprecated ("to_dict" )(to_dict )
217
+
200
218
def __iter__ (self ):
201
219
"""Implement iter(self)."""
202
220
# 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"):
286
304
287
305
@comparator
288
306
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
290
308
291
309
@comparator
292
310
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
294
312
295
313
@comparator
296
314
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
298
316
299
317
@comparator
300
318
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
302
320
303
321
@comparator
304
322
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
306
324
307
325
@comparator
308
326
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
310
328
311
329
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 ())
313
331
return "%s(%s)" % (type (self ).__name__ , s )
314
332
315
333
def __str__ (self ):
@@ -376,12 +394,12 @@ def replace(self, **parts):
376
394
parts
377
395
:raises: TypeError, if ``parts`` contains invalid keys
378
396
"""
379
- version = self ._asdict ()
397
+ version = self .to_dict ()
380
398
version .update (parts )
381
399
try :
382
400
return VersionInfo (** version )
383
401
except TypeError :
384
- unknownkeys = set (parts ) - set (self ._asdict ())
402
+ unknownkeys = set (parts ) - set (self .to_dict ())
385
403
error = "replace() got %d unexpected keyword " "argument(s): %s" % (
386
404
len (unknownkeys ),
387
405
", " .join (unknownkeys ),
@@ -409,9 +427,9 @@ def isvalid(cls, version):
409
427
410
428
def _to_dict (obj ):
411
429
if isinstance (obj , VersionInfo ):
412
- return obj ._asdict ()
430
+ return obj .to_dict ()
413
431
elif isinstance (obj , tuple ):
414
- return VersionInfo (* obj )._asdict ()
432
+ return VersionInfo (* obj ).to_dict ()
415
433
return obj
416
434
417
435
@@ -512,8 +530,8 @@ def compare(ver1, ver2):
512
530
0
513
531
"""
514
532
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 ()
517
535
518
536
return _compare_by_keys (v1 , v2 )
519
537
0 commit comments