Skip to content

make Frame not derived from dataclass

dataclass should not be used to mix properties and attributes given the way in which init and post_init are resolved. Take the following diff (which technically works, but is a bad idea):

diff --git a/src/sgn/base.py b/src/sgn/base.py
index 2ddffc5..708bc18 100644
--- a/src/sgn/base.py
+++ b/src/sgn/base.py
@@ -5,7 +5,7 @@ from __future__ import annotations
 import logging
 import os
 import uuid
-from dataclasses import dataclass, field
+from dataclasses import InitVar, dataclass, field
 from typing import Any, Callable, Dict, Optional, Sequence, Union

 SGN_LOG_LEVELS = {
@@ -73,11 +73,23 @@ class Frame:
     EOS: bool = False
     is_gap: bool = False
     metadata: dict = field(default_factory=dict)
-    data: Any = None
+    data: InitVar[Any] = None

-    def __post_init__(self):
+    def __post_init__(self, data):
+        if isinstance(data, property):
+            self._data = None
+        else:
+            self._data = data
         pass

+    @property  # type: ignore
+    def data(self):
+        return self._data
+
+    @data.setter
+    def data(self, data):
+        self._data = data
+

The issue is that if data is not passed during init (which should be allowed) even the post init gets data as a "property" object rather than None. It can be hacked around as done in this diff, but this feels unsafe and asking for problems down the road. Since Frame is not derived from anything, the simplests solution is to not make it a dataclass which is what this MR does.

Merge request reports

Loading