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

Python 3.11 is pickier about mutable dataclass fields #284

Closed
jamesjer opened this issue Feb 4, 2022 · 6 comments · Fixed by #285
Closed

Python 3.11 is pickier about mutable dataclass fields #284

jamesjer opened this issue Feb 4, 2022 · 6 comments · Fixed by #285

Comments

@jamesjer
Copy link

jamesjer commented Feb 4, 2022

The Fedora project is building all of its python packages with an alpha version of python 3.11, to try to identify problems and fix them prior to the actual 3.11 release. The sphinxcontrib-bibtex package is failing to build, as can be seen in this bug report. The issue seems to be that python has gotten pickier about ensuring that dataclass fields are not initialized with a mutable value. A sample error:

WARNING: autodoc: failed to import module 'bibtex.bibfile' from module 'sphinxcontrib'; the following exception was raised:
Traceback (most recent call last):
  File "/usr/lib64/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/sphinx/util/logging.py", line 276, in skip_warningiserror
    yield
    ^^^^^
  File "/usr/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 70, in import_module
    return importlib.import_module(modname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 915, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/builddir/build/BUILDROOT/python-sphinxcontrib-bibtex-2.4.1-2.fc36.x86_64/usr/lib/python3.11/site-packages/sphinxcontrib/bibtex/__init__.py", line 9, in <module>
    from .domain import BibtexDomain
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/builddir/build/BUILDROOT/python-sphinxcontrib-bibtex-2.4.1-2.fc36.x86_64/usr/lib/python3.11/site-packages/sphinxcontrib/bibtex/domain.py", line 44, in <module>
    from .style.referencing import (
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/builddir/build/BUILDROOT/python-sphinxcontrib-bibtex-2.4.1-2.fc36.x86_64/usr/lib/python3.11/site-packages/sphinxcontrib/bibtex/style/referencing/__init__.py", line 121, in <module>
    @dataclasses.dataclass
     ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/dataclasses.py", line 1187, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/usr/lib64/python3.11/dataclasses.py", line 1178, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/dataclasses.py", line 958, in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/dataclasses.py", line 815, in _get_field
    raise ValueError(f'mutable default {type(f.default)} for field '
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: mutable default <class 'pybtex.richtext.Text'> for field other is not allowed: use default_factory

This affects dataclass field initializations in the following files:

  • doc/usage.rst
  • src/sphinxcontrib/bibtex/style/referencing/init.py
  • src/sphinxcontrib/bibtex/style/referencing/author_year.py
  • src/sphinxcontrib/bibtex/style/referencing/basic_author_year.py
  • src/sphinxcontrib/bibtex/style/referencing/basic_foot.py
  • src/sphinxcontrib/bibtex/style/referencing/basic_label.py
  • src/sphinxcontrib/bibtex/style/referencing/basic_super.py
  • src/sphinxcontrib/bibtex/style/referencing/extra_author.py
  • src/sphinxcontrib/bibtex/style/referencing/extra_label.py
  • src/sphinxcontrib/bibtex/style/referencing/extra_year.py
  • src/sphinxcontrib/bibtex/style/referencing/foot.py
  • src/sphinxcontrib/bibtex/style/referencing/label.py
  • src/sphinxcontrib/bibtex/style/referencing/super_.py
  • test/roots/test-citation_style_round_brackets/conf.py
  • test/test_citation.py
  • test/test_footcite.py
@jamesjer
Copy link
Author

jamesjer commented Feb 4, 2022

I have taken a stab at addressing this:

https://src.fedoraproject.org/rpms/python-sphinxcontrib-bibtex/blob/rawhide/f/python-sphinxcontrib-bibtex-default-factory.patch

I don't know if that is an acceptable approach for you. If so, I can open a PR.

@mcmtroffaes
Copy link
Owner

Many thanks for reporting and for the base work on this. Patch looks ok besides some small issues; I'll push a similar patch shortly.

@kloczek
Copy link

kloczek commented Apr 10, 2022

Is it possible to make new release with those ffixes?

@mcmtroffaes
Copy link
Owner

Sure.

@kloczek
Copy link

kloczek commented Apr 10, 2022

Tahnk you 😃

@mcmtroffaes
Copy link
Owner

With pleasure!

JoepVanlier added a commit to lumicks/pylake that referenced this issue May 9, 2023
This is needed to support building docs on Python 3.11. See this issue: mcmtroffaes/sphinxcontrib-bibtex#284
JoepVanlier added a commit to lumicks/pylake that referenced this issue May 9, 2023
This is needed to support building docs on Python 3.11. See this issue: mcmtroffaes/sphinxcontrib-bibtex#284
JoepVanlier added a commit to lumicks/pylake that referenced this issue May 10, 2023
This is needed to support building docs on Python 3.11. See this issue: mcmtroffaes/sphinxcontrib-bibtex#284
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 a pull request may close this issue.

3 participants