Skip to content

Cannot compare with list or string #244

@tomschr

Description

@tomschr

Situation

A VersionInfo instance has the ability to compare with another VersionInfo instance, a tuple, or a dict :

>>> import semver
>>> v1 = semver.VersionInfo.parse("1.2.3")
>>> v1 < (1, 2, 4)
True

However, it is not possible for a list or a string:

>>> v1 < [1, 2, 4]
Traceback (most recent call last):
...
TypeError: other type <class 'list'> must be in (<class 'semver.VersionInfo'>, <class 'dict'>, <class 'tuple'>)
>>> v1 < "1.2.4"
Traceback (most recent call last):
...
TypeError: other type <class 'str'> must be in (<class 'semver.VersionInfo'>, <class 'dict'>, <class 'tuple'>)

Situation

After analyzing the situation above, I found the comparator which allows VersionInfo instance, a tuple, or a dict :

def comparator(operator):
    """Wrap a VersionInfo binary op method in a type-check."""

    @wraps(operator)
    def wrapper(self, other):
        comparable_types = (VersionInfo, dict, tuple)  # bug!
        if not isinstance(other, comparable_types):
            raise TypeError(
                "other type %r must be in %r" % (type(other), comparable_types)
            )
        return operator(self, other)

    return wrapper

We should add a list and a string to the allowed types.

Would that be ok @python-semver/reviewers?

Metadata

Metadata

Assignees

Labels

BugError, flaw or fault to produce incorrect or unexpected results

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy