Skip to content
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

Ensure that VersionRange is immutable and hashable #21

Merged
merged 2 commits into from
Jan 11, 2022
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
7 changes: 5 additions & 2 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ class VersionRange:

# A list of lists of VersionConstraint that are signposts on the versions
# timeline
constraints = attr.ib(type=list, default=attr.Factory(list))
constraints = attr.ib(type=tuple, default=attr.Factory(tuple))

def __attrs_post_init__(self, *args, **kwargs):
self.constraints.sort()
constraints = tuple(sorted(self.constraints))
# Notes: setattr is used because this is an immutable frozen instance.
# See https://www.attrs.org/en/stable/init.html?#post-init
object.__setattr__(self, "constraints", constraints)

@classmethod
def from_native(cls, string):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ def test_VersionRange_from_string_pypi(self):
version_range = VersionRange.from_string(vers)
assert version_range.scheme == "pypi"
# note the sorting taking place
expected = [
expected = (
VersionConstraint(comparator="=", version=PypiVersion(string="0.0.0")),
VersionConstraint(comparator="=", version=PypiVersion(string="0.0.1")),
VersionConstraint(comparator="=", version=PypiVersion(string="0.0.2")),
VersionConstraint(comparator="=", version=PypiVersion(string="0.0.3")),
VersionConstraint(comparator="=", version=PypiVersion(string="0.0.4")),
VersionConstraint(comparator="=", version=PypiVersion(string="0.0.5")),
VersionConstraint(comparator="=", version=PypiVersion(string="0.0.6")),
]
)
assert version_range.constraints == expected
# note the sorting taking place
assert str(version_range) == "vers:pypi/0.0.0|0.0.1|0.0.2|0.0.3|0.0.4|0.0.5|0.0.6"
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_GemVersionRange_from_native_range_with_pessimistic_operator(self):
gem_range = "~>2.0.8"
version_range = GemVersionRange.from_native(gem_range)
assert version_range.to_string() == "vers:gem/>=2.0.8|<2.1"
assert version_range.constraints == [
assert version_range.constraints == (
VersionConstraint(comparator=">=", version=RubygemsVersion(string="2.0.8")),
VersionConstraint(comparator="<", version=RubygemsVersion(string="2.1")),
]
)