Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Return dict instead of OrderedDict #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/pr418.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Replace :class:`~collection.OrderedDict` with :class:`dict`.

The dict datatype is ordered since Python 3.7. As we do not support
Python 3.6 anymore, it can be considered safe to avoid :class:`~collection.OrderedDict`.
Related to :gh:`419`.
2 changes: 1 addition & 1 deletion docs/usage/convert-version-into-different-types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ It is possible to convert a :class:`~semver.version.Version` instance:

>>> v = Version(major=3, minor=4, patch=5)
>>> v.to_dict()
OrderedDict([('major', 3), ('minor', 4), ('patch', 5), ('prerelease', None), ('build', None)])
{'major': 3, 'minor': 4, 'patch': 5, 'prerelease': None, 'build': None}

* Into a tuple with :meth:`~semver.version.Version.to_tuple`::

Expand Down
2 changes: 1 addition & 1 deletion docs/usage/create-a-version.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Depending on your use case, the following methods are available:
To access individual parts, you can use the function :func:`semver.parse`::

>>> semver.parse("3.4.5-pre.2+build.4")
OrderedDict([('major', 3), ('minor', 4), ('patch', 5), ('prerelease', 'pre.2'), ('build', 'build.4')])
{'major': 3, 'minor': 4, 'patch': 5, 'prerelease': 'pre.2', 'build': 'build.4'}

If you pass an invalid version string you will get a :py:exc:`ValueError`::

Expand Down
24 changes: 10 additions & 14 deletions src/semver/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Version handling by a semver compatible version class."""

import collections
import re
from functools import wraps
from typing import (
Expand Down Expand Up @@ -218,27 +217,24 @@ def to_tuple(self) -> VersionTuple:

def to_dict(self) -> VersionDict:
"""
Convert the Version object to an OrderedDict.
Convert the Version object to an dict.

.. versionadded:: 2.10.0
Renamed :meth:`Version._asdict` to :meth:`Version.to_dict` to
make this function available in the public API.

:return: an OrderedDict with the keys in the order ``major``, ``minor``,
:return: an dict with the keys in the order ``major``, ``minor``,
``patch``, ``prerelease``, and ``build``.

>>> semver.Version(3, 2, 1).to_dict()
OrderedDict([('major', 3), ('minor', 2), ('patch', 1), \
('prerelease', None), ('build', None)])
"""
return collections.OrderedDict(
(
("major", self.major),
("minor", self.minor),
("patch", self.patch),
("prerelease", self.prerelease),
("build", self.build),
)
{'major': 3, 'minor': 2, 'patch': 1, 'prerelease': None, 'build': None}
"""
return dict(
major=self.major,
minor=self.minor,
patch=self.patch,
prerelease=self.prerelease,
build=self.build,
)

def __iter__(self) -> VersionIterator:
Expand Down