Ubuntu 22.04 Python 3.11 [asr]: multiple errors `dataclasses ValueError: mutable default * for field * is not allowed: use default_factory`
State: closed | Pull request: no
Labels: name: bug

**Describe the bug**

After installing latest stable `1.19.1` from pipI, or the latest current commit with `[asr]` extras, I'm getting this error from `hydra-core==1.2.0` when trying to import `nemo.collections.asr`:
```
Traceback (most recent call last):
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/test.py", line 5, in <module>
    import nemo.collections.asr as nemo_asr
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/__init__.py", line 15, in <module>
    from nemo.collections.asr import data, losses, models, modules
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/losses/__init__.py", line 15, in <module>
    from nemo.collections.asr.losses.angularloss import AngularSoftmaxLoss
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/losses/angularloss.py", line 18, in <module>
    from nemo.core.classes import Loss, Typing, typecheck
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/core/__init__.py", line 16, in <module>
    from nemo.core.classes import *
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/core/classes/__init__.py", line 16, in <module>
    import hydra
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/hydra/__init__.py", line 5, in <module>
    from hydra import utils
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/hydra/utils.py", line 8, in <module>
    import hydra._internal.instantiate._instantiate2
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 12, in <module>
    from hydra._internal.utils import _locate
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/hydra/_internal/utils.py", line 18, in <module>
    from hydra.core.utils import get_valid_filename, validate_config_path
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/hydra/core/utils.py", line 20, in <module>
    from hydra.core.hydra_config import HydraConfig
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/hydra/core/hydra_config.py", line 6, in <module>
    from hydra.conf import HydraConf
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/hydra/conf/__init__.py", line 46, in <module>
    class JobConf:
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/hydra/conf/__init__.py", line 75, in JobConf
    @dataclass
     ^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 1230, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 1220, 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 'hydra.conf.JobConf.JobConfig.OverrideDirname'> for field override_dirname is not allowed: use default_factory
```
If I then manually upgrade `hydra-core` to the current latest (`1.3.2`), I get similar errors from `nemo-toolkit`:
```
Traceback (most recent call last):
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/test.py", line 5, in <module>
    import nemo.collections.asr as nemo_asr
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/__init__.py", line 15, in <module>
    from nemo.collections.asr import data, losses, models, modules
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/losses/__init__.py", line 16, in <module>
    from nemo.collections.asr.losses.audio_losses import SDRLoss
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/losses/audio_losses.py", line 21, in <module>
    from nemo.collections.asr.parts.preprocessing.features import make_seq_mask_like
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/parts/preprocessing/__init__.py", line 16, in <module>
    from nemo.collections.asr.parts.preprocessing.features import FeaturizerFactory, FilterbankFeatures, WaveformFeaturizer
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/parts/preprocessing/features.py", line 44, in <module>
    from nemo.collections.asr.parts.preprocessing.perturb import AudioAugmentor
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/parts/preprocessing/perturb.py", line 50, in <module>
    from nemo.collections.common.parts.preprocessing import collections, parsers
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/common/__init__.py", line 16, in <module>
    from nemo.collections.common import data, losses, parts, tokenizers
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/common/parts/__init__.py", line 15, in <module>
    from nemo.collections.common.parts.adapter_modules import LinearAdapter, LinearAdapterConfig
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/common/parts/adapter_modules.py", line 147, in <module>
    @dataclass
     ^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 1230, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 1220, 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 'nemo.core.classes.mixins.adapter_mixin_strategies.ResidualAddAdapterStrategyConfig'> for field adapter_strategy is not allowed: use default_factory
```
It's easy to fix with a patch like this:
```
--- nemo/collections/common/parts/adapter_modules.py.orig       2023-08-04 13:55:53.464534800 +0200
+++ nemo/collections/common/parts/adapter_modules.py    2023-08-04 14:05:45.579537700 +0200
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from dataclasses import dataclass, is_dataclass
+from dataclasses import dataclass, is_dataclass, field
 from typing import Any, Optional
 
 from hydra.utils import instantiate
@@ -151,5 +151,5 @@ class LinearAdapterConfig:
     activation: str = 'swish'
     norm_position: str = 'pre'
     dropout: float = 0.0
-    adapter_strategy: Optional[Any] = adapter_mixin_strategies.ResidualAddAdapterStrategyConfig()
+    adapter_strategy: Optional[Any] = field(default_factory=adapter_mixin_strategies.ResidualAddAdapterStrategyConfig)
     _target_: str = "{0}.{1}".format(LinearAdapter.__module__, LinearAdapter.__name__)
```
However then another error of the kind comes up:
```
Traceback (most recent call last):
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/test.py", line 5, in <module>
    import nemo.collections.asr as nemo_asr
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/__init__.py", line 15, in <module>
    from nemo.collections.asr import data, losses, models, modules
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/models/__init__.py", line 18, in <module>
    from nemo.collections.asr.models.clustering_diarizer import ClusteringDiarizer
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/models/clustering_diarizer.py", line 29, in <module>
    from nemo.collections.asr.metrics.der import score_labels
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/metrics/der.py", line 24, in <module>
    from nemo.collections.asr.metrics.wer import word_error_rate
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/metrics/wer.py", line 27, in <module>
    from nemo.collections.asr.parts.submodules import ctc_beam_decoding, ctc_greedy_decoding
  File "/home/alex/T7/src/speaker_verification/nemo_speaker/venv11/lib/python3.11/site-packages/nemo/collections/asr/parts/submodules/ctc_beam_decoding.py", line 593, in <module>
    @dataclass
     ^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 1230, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/usr/lib/python3.11/dataclasses.py", line 1220, 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 'nemo.collections.asr.parts.submodules.ctc_beam_decoding.FlashlightConfig'> for field flashlight_cfg is not allowed: use default_factory
```
This can also be fixed accordingly, but all in all, it appears these issues are pretty common in the code base.

Looks like NeMo isn't Python 3.11 ready, at least the `asr` collection.

**Steps/Code to reproduce bug**

With Python 3.11
1. Install `nemo-toolkit[asr]` either 1.19.1, or current HEAD from git (b498d438fc4c35ebf364a9a1c5cd3e29a2c0fe50)
2. Run `import nemo.collections.asr`


**Expected behavior**

Import `nemo.collections.asr` without errors

**Environment overview (please complete the following information)**

 - Environment location: Bare-metal
 - Method of NeMo install: `pip install nemo-toolkit==1.19.1` or from source b498d438fc4c35ebf364a9a1c5cd3e29a2c0fe50

**Environment details**
- Ubuntu 22.04
- PyTorch version 2.0.1
- Python version 3.11.4