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

Fix issue with mutuable default value in dataclass parameter #196

Merged
merged 1 commit into from
Jun 1, 2023
Merged

Fix issue with mutuable default value in dataclass parameter #196

merged 1 commit into from
Jun 1, 2023

Conversation

fredrikrab
Copy link
Contributor

Use default_factory to initialize AnkiHookManager instead, solving the ValueError exception enforced since Python 3.11.

See https://docs.python.org/3/library/dataclasses.html#mutable-default-values.

I got the following error upon opening Anki prior to this commit:

When loading '⁨CrowdAnki JSON exportimport Edit history Collaborate on deck creation⁩':
⁨Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/aqt/addons.py", line 246, in loadAddons
    __import__(addon.dir_name)
  File "/home/fredmb/.local/share/Anki2/addons21/1788670778/__init__.py", line 5, in <module>
    from . import main
  File "/home/fredmb/.local/share/Anki2/addons21/1788670778/main.py", line 8, in <module>
    from .anki.hook_vendor import HookVendor
  File "/home/fredmb/.local/share/Anki2/addons21/1788670778/anki/hook_vendor.py", line 13, in <module>
    @dataclass
     ^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 1223, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 1213, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 958, in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 815, in _get_field
    raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class '1788670778.anki.adapters.hook_manager.AnkiHookManager'> for field hook_manager is not allowed: use default_factory

Use default_factory to initialize AnkiHookManager instead, solving the ValueError exception enforced since Python 3.11.

See https://docs.python.org/3/library/dataclasses.html#mutable-default-values.
@aplaice aplaice merged commit 2499c4b into Stvad:master Jun 1, 2023
2 checks passed
@aplaice
Copy link
Collaborator

aplaice commented Jun 1, 2023

Thanks very much!

(I understand ArchLinux and/or other rolling-release distros are now using Python3.11?)

@fredrikrab
Copy link
Contributor Author

Thank you as well :) Arch Linux is indeed hosting version 3.11 as the default Python package in their Core repo.

(It was upgraded on the same day as the official release!)

@Mxbonn
Copy link

Mxbonn commented Jun 25, 2023

Can we get a release and update on ankiweb for this @aplaice?

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

Successfully merging this pull request may close these issues.

None yet

3 participants