3.14. Determining Version Equality¶
Version equality means for semver, that major, minor, patch, and prerelease parts are equal in both versions you compare. The build part is ignored. For example:
>>> v = Version.parse("1.2.3-rc4+1e4664d")
>>> v == "1.2.3-rc4+dedbeef"
True
In semver 3, equality comparisons between a Version and
a string, bytes, tuple, or list issue a DeprecationWarning. This
cross-type equality will be removed in semver 4. Convert strings with
Version.parse and tuples with Version(*parts) instead.
To suppress only this warning, add a matching filter during application startup.
The message pattern identifies this semver warning; other
DeprecationWarning messages remain visible. A module filter is not
reliable here because the warning is reported at the comparison’s call site:
import warnings
warnings.filterwarnings(
"ignore",
message=r"Equality comparisons between Version and strings, tuples or lists.*",
category=DeprecationWarning,
)
Warning
Keep dictionary keys and set elements to one type. Although Version
remains hashable in semver 3, a Version can compare equal to a string,
bytes, tuple, or list with a different hash, so mixing these types as keys or elements can
produce failed lookups. Convert values to Version
objects before using them as keys.
This also applies when a Version is a member of a set, or a
dictionary key:
>>> d = {}
>>> v1 = Version.parse("1.2.3-rc4+1e4664d")
>>> v2 = Version.parse("1.2.3-rc4+dedbeef")
>>> d[v1] = 1
>>> d[v2]
1
>>> s = set()
>>> s.add(v1)
>>> v2 in s
True