GRIDAPPSD / gridappsd-python

Python connector to gridappsd
5 stars 6 forks source link

Package is not compatible with python 3.11 due to changes in dataclass interface #152

Open danielbjerk opened 4 months ago

danielbjerk commented 4 months ago

Describe the bug Attempting to use gridapps.simulation with python 3.11 will result in ValueError due to Python 3.11 changing dataclass to be non-backwards compatible (see i.e. this discussion). The ValueError is due to SimulationArgs, SimulationConfig, having class defaults (ModelCreationConfig and SimulationArgs, ApplicationConfig, TestConfig, respectively).

To Reproduce

$ mkdir tmp && cd tmp
$ py --version
Python 3.11.4
$ py -m venv .
$ py -m pip install gridappsd-python
$ py
>>> import gridappsd.simulation
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\danielbje\tmp\Lib\site-packages\gridappsd\simulation.py", line 51, in <module>
    @dataclass
     ^^^^^^^^^
  File "C:\Users\danielbje\AppData\Local\Programs\Python\Python311\Lib\dataclasses.py", line 1230, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "C:\Users\danielbje\AppData\Local\Programs\Python\Python311\Lib\dataclasses.py", line 1220, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\danielbje\AppData\Local\Programs\Python\Python311\Lib\dataclasses.py", line 958, in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\danielbje\AppData\Local\Programs\Python\Python311\Lib\dataclasses.py", line 815, in _get_field
    raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class 'gridappsd.simulation.ModelCreationConfig'> for field model_creation_config is not allowed: use default_factory

Proposed solution Use default_factory for these fields as well.

@dataclass
class SimulationArgs(ConfigBase):
    start_time: str = "1655321830"
    duration: str = "300"
    simulator: str = "GridLAB-D"
    timestep_frequency: str = "1000"
    timestep_increment: str = "1000"
    run_realtime: bool = True
    simulation_name: str = "ieee13nodeckt"
    power_flow_solver_method: str = "NR"
    model_creation_config: ModelCreationConfig = field(default_factory=lambda: __default_model_creation_config__)   # __default_model_creation_config__

and

@dataclass
class SimulationConfig(ConfigBase):
    power_system_config: PowerSystemConfig
    application_config: List[ApplicationConfig] = field(default_factory=list)
    simulation_config: SimulationArgs = field(default_factory=lambda: __default_simulation_args__)   # __default_simulation_args__
    service_configs: List[ServiceConfig] = field(default_factory=list)
    application_config: ApplicationConfig = field(default_factory=lambda: __default_application_config__)   # __default_application_config__
    test_config: TestConfig = field(default_factory=lambda: __default_test_config__)   # __default_test_config__

Elsewise, README and pyproject-dotfiles should be changed to reflect <3.11 requirement.

See also this issue for inspiration

afisher1 commented 2 months ago

I can confirm this issue with python greater than 3.10. The suggested solution is will work however, i believe this would work as well: field(default_factory=ModelCreationConfig) field(default_factory=SimulationArgs) field(default_factory=ApplicationConfig) field(default_factory=TestConfig)