Skip to content

Remove namedtuple inheritance #93

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 1 commit into from
May 26, 2018
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
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Version 2.8.1 (WIP)

* Issue #77 (PR #47). Convert multiple tests into pytest.mark.parametrize
* Issue #89 (PR #90). Add doctests.
* Issue #40 (PR #88). Add a static parse method to VersionInfo
* Issue #87 #94 (PR #93). Remove named tuple inheritance. Fix bad rendering in Pandas DataFrame

Version 2.8.0
=============
Expand Down
33 changes: 28 additions & 5 deletions semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def parse(version):
return version_parts


class VersionInfo(collections.namedtuple(
'VersionInfo', 'major minor patch prerelease build')):
class VersionInfo:
"""
:param int major: version when you make incompatible API changes.
:param int minor: version when you add functionality in
Expand All @@ -83,7 +82,26 @@ class VersionInfo(collections.namedtuple(
:param str prerelease: an optional prerelease string
:param str build: an optional build string
"""
__slots__ = ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now VersionInfo is a mutable object with dynamic attrs. That's quite too dramatic change in the name of pretty printting pandas dataframes /:

Copy link
Member Author

@s-celles s-celles May 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR to make VersionInfo immutable (without inheriting from namedtuple) is highly appreciated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what's wrong with "namedtuple" approach? (Apart from problems with printing in Pandas)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing else than Pandas printing but that's quite problematic as it won't be solved soon (my 2 cts)

Some tests to ensure that VersionInfo are immutable should be added if mutability of VersionInfo is a problem


def __init__(self, major, minor, patch, prerelease, build):
self.major = major
self.minor = minor
self.patch = patch
self.prerelease = prerelease
self.build = build

def _astuple(self):
return (self.major, self.minor, self.patch,
self.prerelease, self.build)

def _asdict(self):
return collections.OrderedDict((
("major", self.major),
("minor", self.minor),
("patch", self.patch),
("prerelease", self.prerelease),
("build", self.build)
))

def __eq__(self, other):
if not isinstance(other, (VersionInfo, dict)):
Expand Down Expand Up @@ -115,11 +133,16 @@ def __ge__(self, other):
return NotImplemented
return _compare_by_keys(self._asdict(), _to_dict(other)) >= 0

def __repr__(self):
s = ", ".join("%s=%r" % (key, val)
for key, val in self._asdict().items())
return "VersionInfo(%s)" % s

def __str__(self):
return format_version(*self)
return format_version(*(self._astuple()))

def __hash__(self):
return hash(tuple(self))
return hash(self._astuple())

@staticmethod
def parse(version):
Expand Down