Skip to content

Commit

Permalink
fix regression in v2.2.2
Browse files Browse the repository at this point in the history
when using tagged value pairs, the string representation behaved not as expected
  • Loading branch information
Cube707 committed Jan 29, 2024
1 parent 0b658ef commit 0b1e56b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions examples/list_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import inquirer # noqa

choices_hints = {
"Jumbo": "The biggest one we have",
"Large": "If you need the extra kick",
"Standard": "For your every day use",
"Large": "If you need the extra kick",
"Jumbo": "The biggest one we have",
}

questions = [
Expand Down
22 changes: 11 additions & 11 deletions src/inquirer/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@


class TaggedValue:
def __init__(self, choice):
self.label = choice[0]
self.tag = choice[1]
self._hash = hash(choice)
def __init__(self, tag, value):
self.tag = tag
self.value = value
self.tuple = (tag, value)

def __str__(self):
return self.label
return self.tag

def __repr__(self):
return repr(self.tag)
return repr(self.value)

def __eq__(self, other):
if isinstance(other, TaggedValue):
return other.tag == self.tag
return other.value == self.value
if isinstance(other, tuple):
return other == (self.label, self.tag)
return other == self.tag
return other == self.tuple
return other == self.value

def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self) -> int:
return self._hash
return hash(self.tuple)


class Question:
Expand Down Expand Up @@ -93,7 +93,7 @@ def default(self):
@property
def choices_generator(self):
for choice in self._solve(self._choices):
yield (TaggedValue(choice) if isinstance(choice, tuple) and len(choice) == 2 else choice)
yield (TaggedValue(*choice) if isinstance(choice, tuple) and len(choice) == 2 else choice)

@property
def choices(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,16 @@ def test_default_value_validation(self):

def test_tagged_value():
LABEL = "label"
TAG = "l"
tp = (LABEL, TAG)
tv = questions.TaggedValue(tp)
VALUE = "l"
tp = (LABEL, VALUE)
tv = questions.TaggedValue(*tp)

assert (str(tv) == str(LABEL)) is True
assert (repr(tv) == repr(TAG)) is True
assert (repr(tv) == repr(VALUE)) is True
assert (hash(tv) == hash(tp)) is True

assert (tv == tv) is True
assert (tv != tv) is False
assert (tv == tp) is True
assert (tv == TAG) is True
assert (tv == VALUE) is True
assert (tv == "") is False

0 comments on commit 0b1e56b

Please sign in to comment.