Skip to content

Should _comparator return NotImplemented instead of raising TypeError? #316

Closed

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?

Activity

  1. added
    Release_3.x.yOnly for the major release 3
    BugError, flaw or fault to produce incorrect or unexpected results
    QuestionUnclear or open issue subject for debate
    on Nov 5, 2020
  2. changed the title [-]Should _comparator raise NotImplemented instead of TypeError?[/-] [+]Should _comparator return NotImplemented instead of raising TypeError?[/+] on Nov 5, 2020
  3. self-assigned this
    on Nov 5, 2020
  4. tlaferriere commented on Nov 6, 2020

    @tlaferriere
    Contributor

    I think this would make sense since this function is meant to be used in the __gt__ magic method and family. I also don't see much of a problem changing this because we're making it private.

    Sounds good to me overall.

  5. added a commit that references this issue on Nov 8, 2020
    6793320
  6. added a commit that references this issue on Nov 8, 2020
    c75737d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

Fields

SIze

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions