Skip to content

Commit 6793320

Browse files
committed
Fix #316: Return NotImplemented for comparisons
The former code raised a TypeError exception for comparisons like __gt__, __lt__ etc. to indicate a wrong type. However, according to NotImplemented[1] documentation, we should return(!) NotImplemented (not raise) when a comparison with an invalid type is not implemented. [1] https://docs.python.org/3/library/constants.html#NotImplemented
1 parent 44708ef commit 6793320

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

changelog.d/316.trivial.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Comparisons of :class:`~semver.version.Version` class and other
2+
types return now a :py:const:`NotImplemented` constant instead
3+
of a :py:exc:`TypeError` exception.
4+
5+
The `NotImplemented`_ section of the Python documentation recommends
6+
returning this constant when comparing with ``__gt__``, ``__lt__``,
7+
and other comparison operators to "to indicate that the operation is
8+
not implemented with respect to the other type".
9+
10+
.. _NotImplemented: https://docs.python.org/3/library/constants.html#NotImplemented

src/semver/version.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def wrapper(self: "Version", other: Comparable) -> bool:
7272
*String.__args__, # type: ignore
7373
)
7474
if not isinstance(other, comparable_types):
75-
raise TypeError(
76-
"other type %r must be in %r" % (type(other), comparable_types)
77-
)
75+
return NotImplemented
7876
return operator(self, other)
7977

8078
return wrapper

0 commit comments

Comments
 (0)