mlco2 / codecarbon

Track emissions from Compute and recommend ways to reduce their impact on the environment.
https://mlco2.github.io/codecarbon
MIT License
1.02k stars 158 forks source link

Add Python 3.11 Support - `dataclass` complains about not using `default_factory` for field #390

Closed arrrrrmin closed 1 year ago

arrrrrmin commented 1 year ago

Description

Hi πŸ‘‹ first of all thanks for publishing the package. Upon importing codecarbon the dataclass wrapper complains about Energyhttps://github.com/mlco2/codecarbon/blob/2108c72ebfa81587d69656464173aecb8989cb0a/codecarbon/core/rapl.py#L12

mutable default <class 'codecarbon.core.units.Energy'> for field energy_delta is not allowed: use default_factory

Here is the complete Exception:

tests/test_energy.py:2: in <module>
    from codecarbon import OfflineEmissionsTracker
../../Library/Caches/pypoetry/virtualenvs/neuromorphic-P4cmU1Sb-py3.11/lib/python3.11/site-packages/codecarbon/__init__.py:5: in <module>
    from .emissions_tracker import (
../../Library/Caches/pypoetry/virtualenvs/neuromorphic-P4cmU1Sb-py3.11/lib/python3.11/site-packages/codecarbon/emissions_tracker.py:16: in <module>
    from codecarbon.core import cpu, gpu
../../Library/Caches/pypoetry/virtualenvs/neuromorphic-P4cmU1Sb-py3.11/lib/python3.11/site-packages/codecarbon/core/cpu.py:18: in <module>
    from codecarbon.core.rapl import RAPLFile
../../Library/Caches/pypoetry/virtualenvs/neuromorphic-P4cmU1Sb-py3.11/lib/python3.11/site-packages/codecarbon/core/rapl.py:7: in <module>
    @dataclass
/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py:1223: in dataclass
    return wrap(cls)
/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py:1213: in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py:958: in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py:815: in _get_field
    raise ValueError(f'mutable default {type(f.default)} for field '
E   ValueError: mutable default <class 'codecarbon.core.units.Energy'> for field energy_delta is not allowed: use default_factory

What I Did

from codecarbon import OfflineEmissionsTracker
tracker = OfflineEmissionsTracker()
arrrrrmin commented 1 year ago

Update:

Appears to be an issue coming through the python version. Using Python 3.10.10 everything looks fine. πŸ‘ Maybe I missed the python version requirement for this package. Should I close this issue?

benoit-cty commented 1 year ago

Hello,

Thanks for reporting this, it's a good think to know that we have something to do to support 3.11.

We will keep this issue open and I will rename it "Python 3.11 Support"

arrrrrmin commented 1 year ago

@benoit-cty I try to could contribute to the project. Forked, branched, cloned and installed as pointed out in the Guideline, but I'm not really understanding why these fail. Im using venv with 3.10/3.11 installed and execute the tox command:

====================================== short test summary info ======================================
FAILED tests/test_emissions_tracker_constant.py::TestCarbonTrackerConstant::test_carbon_tracker_offline_constant_default_cpu_power - AssertionError: 500.0 != 5.0
FAILED tests/test_ram.py::TestRAM::test_ram_diff - AssertionError: False is not true : (1000, 1000, 1000), 0.9313225746154785, 0.8813629150390625, ...

I'm asking myself if this relevant since, my cpu (Apple-M1 (2020)) might be different from the one used in the GitHub-Action for CI. The emissions.csv written during the first test is showing cpu_power-value of 5.0, maybe this file should not be written?

benoit-cty commented 1 year ago

Don't worry, I think you are fine, these tests are very sensitives and may fail under some conditions.

You could launch only the test that fail with 3.11 with 'pytest tests/test_energy.py'