Skip to content

Should _comparator return NotImplemented instead of raising TypeError? #316

@tomschr

Description

@tomschr

Situation

when comparing an incompatible type, the Version class raises TypeError (through the _comparator decorator):

>>> class Foo:
...    pass

>>> v1 = semver.version.Version(1, 2, 3)
>>> v1 < Foo()
Traceback (most recent call last)
...
TypeError: other type <class '__main__.Foo'> must be in (<class 'semver.version.Version'>, <class 'dict'>, <class 'tuple'>, <class 'list'>, <class 'str'>, <class 'bytes'>)

From a semantic point of view this is correct.

Question

According to the NotImplemented documentation (emphasize by me):

Special value which should be returned by the binary special methods (e.g. __eq__(), __lt__(), __add__(), __rsub__(), etc.) to indicate that the operation is not implemented with respect to the other type; may be returned by the in-place binary special methods (e.g. __imul__(), __iand__(), etc.) for the same purpose. It should not be evaluated in a boolean context.

Should we return NotImplemented instead of raising TypeError?

@python-semver/reviewers @tlaferriere Thoughts? Concerns?

Metadata

Metadata

Assignees

Labels

BugError, flaw or fault to produce incorrect or unexpected resultsQuestionUnclear or open issue subject for debateRelease_3.x.yOnly for the major release 3

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions