Skip to content

dataclass objects are being converted into dicts #77

@ljmccarthy

Description

@ljmccarthy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions