From 67933209234509b6bb6ac3a68a24bcc9c65f921f Mon Sep 17 00:00:00 2001 From: Tom Schraitle Date: Sat, 7 Nov 2020 23:38:21 +0100 Subject: [PATCH] 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 --- changelog.d/316.trivial.rst | 10 ++++++++++ src/semver/version.py | 4 +--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 changelog.d/316.trivial.rst diff --git a/changelog.d/316.trivial.rst b/changelog.d/316.trivial.rst new file mode 100644 index 00000000..edb555ff --- /dev/null +++ b/changelog.d/316.trivial.rst @@ -0,0 +1,10 @@ +Comparisons of :class:`~semver.version.Version` class and other +types return now a :py:const:`NotImplemented` constant instead +of a :py:exc:`TypeError` exception. + +The `NotImplemented`_ section of the Python documentation recommends +returning this constant when comparing with ``__gt__``, ``__lt__``, +and other comparison operators to "to indicate that the operation is +not implemented with respect to the other type". + +.. _NotImplemented: https://docs.python.org/3/library/constants.html#NotImplemented \ No newline at end of file diff --git a/src/semver/version.py b/src/semver/version.py index 40132526..4633f4bc 100644 --- a/src/semver/version.py +++ b/src/semver/version.py @@ -72,9 +72,7 @@ def wrapper(self: "Version", other: Comparable) -> bool: *String.__args__, # type: ignore ) if not isinstance(other, comparable_types): - raise TypeError( - "other type %r must be in %r" % (type(other), comparable_types) - ) + return NotImplemented return operator(self, other) return wrapper