InquirerPy icon indicating copy to clipboard operation
InquirerPy copied to clipboard

dataclass objects are being converted into dicts

Open ljmccarthy opened this issue 1 year ago • 0 comments

When I use dataclass objects as the value of a Choice, InquirerPy.inquirer.select returns a dict instead of the original dataclass object.

from dataclasses import dataclass
from InquirerPy import inquirer
from InquirerPy.base.control import Choice

@dataclass(order=True)
class Foo:
    x: str
    y: int

choices = [
    Choice(Foo("bar", 2), "foo"),
    Choice(Foo("foo", 1), "bar"),
]

assert type(choices[0].value) is Foo
choice = inquirer.select(message='Select something', choices=choices).execute()
print(repr(choice))
assert type(choice) is Foo  # This assertion fails

Maybe I'm misunderstanding something, but I expected the original value to be returned unmodified.

Note that when using a collections.namedtuple object the behaviour is different:

import collections
from InquirerPy import inquirer
from InquirerPy.base.control import Choice

Foo = collections.namedtuple('Foo', ['x', 'y'])

choices = [
    Choice(value=Foo("bar", 2), name="foo"),
    Choice(value=Foo("foo", 1), name="bar"),
]

assert type(choices[0].value) is Foo
choice = inquirer.select(message='Select something', choices=choices).execute()
print(choices[0])
assert type(choice) is Foo  # This assertion succeeds

ljmccarthy avatar Jul 02 '24 14:07 ljmccarthy