sopel-irc / sopel

:robot::speech_balloon: An easy-to-use and highly extensible IRC Bot framework. Formerly Willie.
https://sopel.chat
Other
948 stars 402 forks source link

Error when accepting default `ListAttribute` value of `int` instead of `str` #2625

Open dgw opened 2 months ago

dgw commented 2 months ago

Description

A ListAttribute that defines its default as a list of something other than str (in my case, ints) breaks sopel-plugins configure if the user simply enters two blank lines to accept the default.

Reproduction steps

Step 1: In ~/.sopel/plugins, create a dummy.py plugin containing this minimal reproducer:

from sopel.config import types

class DummySection(types.StaticSection):
    broken = types.ListAttribute('broken', default=[1, 2, 3])

def configure(config):
    config.define_section('dummy', DummySection)
    config.dummy.configure_setting(
        'broken',
        'Specify the list to use:',
    )

Step 1.5: Enable the plugin if needed (sopel-plugins enable dummy, edit the .cfg file, etc.)

Step 2: Run sopel-plugins configure dummy

Step 3: Press Enter twice to accept the default list, which appears to be ["1","2","3"] (strings) as rendered by the configure_setting() wizard logic.

The command line will error out; see full traceback below.

Expected behavior

Ideally, the default value should Just Work™ if the list members can be cast to str.

While we could consider this to be a plugin developer's problem (as ListAttribute expects str values and the developer isn't providing strs), I do think it would be nice to support this use case—maybe even allow a custom parse type like the basic ValidatedAttribute type does so the plugin can declare its ListAttribute to be a list of e.g. ints instead of strs.

Relevant logs

$ sopel-plugins configure dummy
Configure dummy plugin
Specify the list to use: ["1","2","3"]
? 
? 
Traceback (most recent call last):
  File "/workspace/.pyenv_mirror/user/3.12.6/bin/sopel-plugins", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/cli/plugins.py", line 559, in main
    return handle_configure(options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/cli/plugins.py", line 332, in handle_configure
    plugin.configure(settings)
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/plugins/handlers.py", line 414, in configure
    self.module.configure(settings)
  File "/workspace/.sopel/plugins/dummy.py", line 10, in configure
    config.dummy.configure_setting(
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/config/types.py", line 114, in configure_setting
    value = attribute.configure(
            ^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/config/types.py", line 622, in configure
    values = self._serialize(values, parent, section)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/config/types.py", line 254, in _serialize
    return self.serialize(value)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/config/types.py", line 582, in serialize
    return '\n' + '\n'.join(self.serialize_item(item) for item in value)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/config/types.py", line 582, in <genexpr>
    return '\n' + '\n'.join(self.serialize_item(item) for item in value)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/sopel/config/types.py", line 593, in serialize_item
    if item.startswith('#'):
       ^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'startswith'

Notes

No response

Sopel version

8.0.0

Installation method

pip install

Python version

3.12.6

Operating system

Ubuntu 22.04.4 LTS

IRCd

No response

Relevant plugins

No response