Datasets:

ArXiv:
Elron commited on
Commit
c892d20
1 Parent(s): 084417b

Upload dataclass.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. dataclass.py +89 -0
dataclass.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Let's modify the code to allow the finalfield and field functions to accept the same parameters as dataclasses.field
2
+ class AbstractFieldValue:
3
+ def __init__(self):
4
+ raise TypeError("Abstract field must be overridden in subclass")
5
+
6
+ import dataclasses
7
+
8
+ class FinalField:
9
+ def __init__(self, *, default=dataclasses.MISSING, default_factory=dataclasses.MISSING,
10
+ init=True, repr=True, hash=None, compare=True, metadata=None):
11
+ self.field = dataclasses.field(default=default, default_factory=default_factory,
12
+ init=init, repr=repr, hash=hash, compare=compare, metadata=metadata)
13
+
14
+ def abstractfield():
15
+ return dataclasses.field(default_factory=AbstractFieldValue)
16
+
17
+ def finalfield(*, default=dataclasses.MISSING, default_factory=dataclasses.MISSING,
18
+ init=True, repr=True, hash=None, compare=True, metadata=None):
19
+ return FinalField(default=default, default_factory=default_factory,
20
+ init=init, repr=repr, hash=hash, compare=compare, metadata=metadata)
21
+
22
+ def field(*, default=dataclasses.MISSING, default_factory=dataclasses.MISSING,
23
+ init=True, repr=True, hash=None, compare=True, metadata=None):
24
+ return dataclasses.field(default=default, default_factory=default_factory,
25
+ init=init, repr=repr, hash=hash, compare=compare, metadata=metadata)
26
+
27
+ class DataclassMeta(type):
28
+ def __new__(cls, name, bases, attrs):
29
+ attrs['__finalfields__'] = attrs.get('__finalfields__', [])
30
+ for base in bases:
31
+ if issubclass(base, Dataclass) and hasattr(base, '__finalfields__'):
32
+ for field in base.__finalfields__:
33
+ if field in attrs:
34
+ raise TypeError(f"Final field '{field}' cannot be overridden in subclass")
35
+ attrs['__finalfields__'].append(field)
36
+
37
+ for attr_name, attr_value in list(attrs.items()):
38
+ if isinstance(attr_value, FinalField):
39
+ attrs[attr_name] = attr_value.field # Replace the final field marker with the actual field
40
+ attrs['__finalfields__'].append(attr_name)
41
+
42
+ new_class = super().__new__(cls, name, bases, attrs)
43
+ new_class = dataclasses.dataclass(new_class)
44
+
45
+ return new_class
46
+
47
+
48
+
49
+ class Dataclass(metaclass=DataclassMeta):
50
+ pass
51
+
52
+ if __name__ == '__main__':
53
+ # Test classes
54
+ class GrandparentClass(Dataclass):
55
+ abstract_field: int = abstractfield()
56
+ final_field: str = finalfield(default_factory=lambda: 'Hello')
57
+
58
+ class ParentClass(GrandparentClass):
59
+ pass
60
+
61
+ try:
62
+ class CorrectChildClass(ParentClass):
63
+ abstract_field: int = 1 # This correctly overrides the abstract field
64
+ correct_child_class_instance = CorrectChildClass()
65
+ print(f'CorrectChildClass instance: {correct_child_class_instance} - passed')
66
+ except Exception as e:
67
+ print(f'CorrectChildClass: {str(e)} - failed')
68
+
69
+ try:
70
+ class IncorrectChildClass1(ParentClass):
71
+ pass # This fails to override the abstract field
72
+ print(f'IncorrectChildClass1: {IncorrectChildClass1} - passed')
73
+ except Exception as e:
74
+ print(f'IncorrectChildClass1: {str(e)} - failed')
75
+
76
+ try:
77
+ incorrect_child_class1_instance = IncorrectChildClass1()
78
+ print(f'IncorrectChildClass1 instance: {incorrect_child_class1_instance} - failed')
79
+ except Exception as e:
80
+ print(f'IncorrectChildClass1 instantiation: {str(e)} - passed')
81
+
82
+ # Testing the final field functionality
83
+
84
+ try:
85
+ class IncorrectChildClass2(ParentClass):
86
+ final_field: str = 'Hello' # This attempts to override the final field
87
+ print(f'IncorrectChildClass2: {IncorrectChildClass2} - failed')
88
+ except Exception as e:
89
+ print(f'IncorrectChildClass2: {str(e)} - passed')