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

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

Open
dgw opened this issue Sep 25, 2024 · 0 comments
Open

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

dgw opened this issue Sep 25, 2024 · 0 comments
Labels
Bug Things to squish; generally used for issues
Milestone

Comments

@dgw
Copy link
Member

dgw commented Sep 25, 2024

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

@dgw dgw added the Bug Things to squish; generally used for issues label Sep 25, 2024
@dgw dgw added this to the 8.1.0 milestone Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Things to squish; generally used for issues
Projects
None yet
Development

No branches or pull requests

1 participant