Skip to content

Commit 9c89a53

Browse files
committed
Implement semver.VersionInfo.to_dict
* Replace _asdict -> to_dict * Add docstring to to_dict * Add test case * Keep _asdict as a (deprecated) function for compatibility reasons * Use to_dict in all lines instead of _asdict
1 parent 9a43790 commit 9c89a53

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ Removals
3838
- ``semver.bump_{major,minor,patch,prerelease,build}``
3939
- ``semver.finalize_version``
4040
- ``semver.replace``
41+
- ``semver.VersionInfo._asdict`` (use the new, public available
42+
function ``semver.VersionInfo.to_dict()``)
4143

42-
These functions will be removed in major 3 of semver.
44+
These functions will be removed in major 4 of semver.
4345

4446

4547

semver.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

8585
def 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

410428
def _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

test_semver.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ def test_should_versioninfo_bump_multiple():
393393
assert v.bump_prerelease().bump_build().bump_build().bump_prerelease() == expected
394394

395395

396+
def test_should_versioninfo_todict(version):
397+
resultdict = version.to_dict()
398+
assert isinstance(resultdict, dict), "Got type from to_dict"
399+
assert list(resultdict.keys()) == ["major", "minor", "patch", "prerelease", "build"]
400+
401+
396402
def test_should_ignore_extensions_for_bump():
397403
assert bump_patch("3.4.5-rc1+build4") == "3.4.6"
398404

0 commit comments

Comments
 (0)