CITGuru / PyInquirer

A Python module for common interactive command line user interfaces
MIT License
1.91k stars 235 forks source link

'tuple' object has no attribute 'get' #121

Closed theduckchannel closed 3 years ago

theduckchannel commented 3 years ago

Hi,

I need create choice with (label, value) i write this: ` sensors = psutil.sensors_temperatures() tempUserChoices = [] for i, key in enumerate(sensors): tempUserChoices.append(( f'index: [{i}] [{key}] current temp: {sensors[key][i].current}°', i ))

        print(tempUserChoices)
        # Temperature Questions
        tempQuestions = [
            {
                'type': 'list',
                'name': 'temp',
                'message': 'Select what is temperature sensor you want gonha to show',
                'choices': tempUserChoices
            }
        ]
        tempResponse = prompt(tempQuestions)
        print(tempResponse)

`

And i got following error.

[('index: [0] [amdgpu] current temp: 47.0°', 0), ('index: [1] [k10temp] current temp: 47.75°', 1)]
'tuple' object has no attribute 'get'
Traceback (most recent call last):
  File "/home/fredlins/.local/lib/python3.8/site-packages/PyInquirer/prompt.py", line 67, in prompt
    application = getattr(prompts, type).question(message, **_kwargs)
  File "/home/fredlins/.local/lib/python3.8/site-packages/PyInquirer/prompts/list.py", line 121, in question
    ic = InquirerControl(choices)
  File "/home/fredlins/.local/lib/python3.8/site-packages/PyInquirer/prompts/list.py", line 43, in __init__
    self._init_choices(choices)
  File "/home/fredlins/.local/lib/python3.8/site-packages/PyInquirer/prompts/list.py", line 58, in _init_choices
    name = c.get('name')
AttributeError: 'tuple' object has no attribute 'get'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/fredlins/.local/bin/gonha", line 87, in <module>
    Config()
  File "/home/fredlins/.local/bin/gonha", line 63, in __init__
    tempResponse = prompt(tempQuestions)
  File "/home/fredlins/.local/lib/python3.8/site-packages/PyInquirer/prompt.py", line 88, in prompt
    raise ValueError('No question type \'%s\'' % type)
ValueError: No question type 'list'

Whats are wrong? How to i return the value ?

SethMMorton commented 3 years ago

choices is supposed to be either a list of the choices, or a list of dictionaries. You have a list of tuples. Is your intent that the first value be what is presented to the user, and the second be what is returned? If so, change this:

[('index: [0] [amdgpu] current temp: 47.0°', 0), ('index: [1] [k10temp] current temp: 47.75°', 1)]

to

[{"name": "index: [0] [amdgpu] current temp: 47.0°", "value": 0}, {"name": "index: [1] [k10temp] current temp: 47.75°", "value": 1}]

Relevant documentation (admittedly it is a bit thin):

choices: (Array|Function) Choices array or a function returning a choices array. If defined as a function, the first parameter will be the current inquirer session answers. Array values can be simple strings, or objects containing a name (to display in list), a value (to save in the answers hash) and a short (to display after selection) properties. The choices array can also contain a Separator.

Relevant example:

    {
        'type': 'expand',
        'name': 'toppings',
        'message': 'What about the toppings?',
        'choices': [
            {
                'key': 'p',
                'name': 'Pepperoni and cheese',
                'value': 'PepperoniCheese'
            },
            {
                'key': 'a',
                'name': 'All dressed',
                'value': 'alldressed'
            },
            {
                'key': 'w',
                'name': 'Hawaiian',
                'value': 'hawaiian'
            }
        ]
    },