Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataclass objects are being converted into dicts #77

Open
ljmccarthy opened this issue Jul 2, 2024 · 0 comments
Open

dataclass objects are being converted into dicts #77

ljmccarthy opened this issue Jul 2, 2024 · 0 comments

Comments

@ljmccarthy
Copy link

ljmccarthy commented Jul 2, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant