Mutable default error when implementing __eq__() with unittest enabled

18 Views Asked by At

I am implementing an object to behave as a selection range from 0..unbound (potentially) but as soon as I try to implement __eq__ I get a Unittest Discovery Error, commenting out the function the error goes away and both full (removing comments) and partial (as-is) get the error below. I don't understand why __eq__ would cause a field to become mutable or the full scope of what that even means, also the __len__ implementation is not the greatest either.

class MyRange:
'''A Range object that only allows for elements greater than 0 or unrestricted'''
start: int | None = None
stop: int | None = None

def __init__(self, start: int | None = None, stop: int | None = None) -> None:
    if start is not None:
        if start > 0:
            self.start = start
    if stop is not None:
        if stop > 0:
            self.stop = stop

def __repr__(self) -> str:
    return f"MyRange(start={self.start}, stop={self.stop})"

def __str__(self) -> str:
    return f"{self.start}..{self.stop}"

def __len__(self) -> int:
    if self.stop is None:
        return 0
    if self.start is None:
        return self.stop
    return self.stop - self.start

def __eq__(self, __value: object) -> bool:
    # if isinstance(__value, self.__class__):
    #     return ((self.start == __value.start) and (self.stop == __value.stop))
    return NotImplemented


@dataclasses.dataclass
class Model:
    '''A dataclass to hold the static elements of report generation'''
    name: str
    data_directory: str | pathlib.Path
    sizes: MyRange = MyRange(1)
    style: int | str = 0

Error below

Failed to import test module: src.project
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\unittest\loader.py", line 452, in _find_test_path
    package = self._get_module_from_name(name)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\unittest\loader.py", line 362, in _get_module_from_name
    __import__(name)
  File "c:\package\src\project\__init__.py", line 13, in <module>
    from .module import PeriodicReport, Model, MonthlyReport, WeeklyReport
  File "c:\package\src\project\module.py", line 48, in <module>
    @dataclasses.dataclass
     ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\dataclasses.py", line 1230, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\dataclasses.py", line 1220, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\dataclasses.py", line 958, in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\dataclasses.py", line 815, in _get_field
    raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class 'src.project.module.MyRange'> for field sizes is not allowed: use default_factory
0

There are 0 best solutions below