Upload dataclass.py with huggingface_hub
Browse files12345123456891011127814159171819201011121314222324251516171819202122232425272829303132333435262728293031323334353738394036373839404142434245434445464748495051525349545556575859606162636465666768697071525354555672587374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363376162636465666768697071727374757677787980818283848586878889338339340341342343344345346347348# Let's modify the code to allow the finalfield and field functions to accept the same parameters as dataclasses.fieldclass AbstractFieldValue: def __init__(self): raise TypeError("Abstract field must be overridden in subclass")
import copyimport dataclassesfrom abc import ABCMetafrom copy import deepcopyfrom typing import Any, final
class FinalField: def __init__(self, *, default=dataclasses.MISSING, default_factory=dataclasses.MISSING, init=True, repr=True, hash=None, compare=True, metadata=None): self.field = dataclasses.field(default=default, default_factory=default_factory, init=init, repr=repr, hash=hash, compare=compare, metadata=metadata)_FIELDS = "__fields__"
def abstractfield(): return dataclasses.field(default_factory=AbstractFieldValue)
def finalfield(*, default=dataclasses.MISSING, default_factory=dataclasses.MISSING, init=True, repr=True, hash=None, compare=True, metadata=None): return FinalField(default=default, default_factory=default_factory, init=init, repr=repr, hash=hash, compare=compare, metadata=metadata)@dataclasses.dataclassclass Field: """ An alternative to dataclasses.dataclass decorator for a more flexible field definition.
def field(*, default=dataclasses.MISSING, default_factory=dataclasses.MISSING, init=True, repr=True, hash=None, compare=True, metadata=None): return dataclasses.field(default=default, default_factory=default_factory, init=init, repr=repr, hash=hash, compare=compare, metadata=metadata) Attributes: default (Any, optional): Default value for the field. Defaults to None. name (str, optional): Name of the field. Defaults to None. type (type, optional): Type of the field. Defaults to None. default_factory (Any, optional): A function that returns the default value. Defaults to None. final (bool, optional): A boolean indicating if the field is final (cannot be overridden). Defaults to False. abstract (bool, optional): A boolean indicating if the field is abstract (must be implemented by subclasses). Defaults to False. required (bool, optional): A boolean indicating if the field is required. Defaults to False. origin_cls (type, optional): The original class that defined the field. Defaults to None. """
class DataclassMeta(type): def __new__(cls, name, bases, attrs): attrs['__finalfields__'] = attrs.get('__finalfields__', []) for base in bases: if issubclass(base, Dataclass) and hasattr(base, '__finalfields__'): for field in base.__finalfields__: if field in attrs: raise TypeError(f"Final field '{field}' cannot be overridden in subclass") attrs['__finalfields__'].append(field) default: Any = None name: str = None type: type = None init: bool = True default_factory: Any = None final: bool = False abstract: bool = False required: bool = False origin_cls: type = None
for attr_name, attr_value in list(attrs.items()): if isinstance(attr_value, FinalField): attrs[attr_name] = attr_value.field # Replace the final field marker with the actual field attrs['__finalfields__'].append(attr_name) def get_default(self): if self.default_factory is not None: return self.default_factory() else: return self.default
new_class = super().__new__(cls, name, bases, attrs) new_class = dataclasses.dataclass(new_class)
return new_class@dataclasses.dataclassclass FinalField(Field): def __post_init__(self): self.final = True
@dataclasses.dataclassclass RequiredField(Field): def __post_init__(self): self.required = True
class Dataclass(metaclass=DataclassMeta):
@dataclasses.dataclassclass AbstractField(Field): def __post_init__(self): self.abstract = True
class FinalFieldError(TypeError): pass
class RequiredFieldError(TypeError): pass
class AbstractFieldError(TypeError): pass
if __name__ == '__main__':# Test classes class GrandparentClass(Dataclass): abstract_field: int = abstractfield() final_field: str = finalfield(default_factory=lambda: 'Hello')
class ParentClass(GrandparentClass):class TypeMismatchError(TypeError): pass
standart_variables = dir(object)
def is_possible_field(field_name, field_value): """ Check if a name-value pair can potentially represent a field.
Args: field_name (str): The name of the field. field_value: The value of the field.
Returns: bool: True if the name-value pair can represent a field, False otherwise. """ return field_name not in standart_variables and not field_name.startswith("__") and not callable(field_value)
def get_fields(cls, attrs): """ Get the fields for a class based on its attributes.
Args: cls (type): The class to get the fields for. attrs (dict): The attributes of the class.
Returns: dict: A dictionary mapping field names to Field instances. """
fields = {**getattr(cls, _FIELDS, {})} annotations = {**attrs.get("__annotations__", {})}
for attr_name, attr_value in attrs.items(): if attr_name not in annotations and is_possible_field(attr_name, attr_value): if attr_name in fields: if not isinstance(attr_value, fields[attr_name].type): raise TypeMismatchError( f"Type mismatch for field '{attr_name}' of class '{fields[attr_name].origin_cls}'. Expected {fields[attr_name].type}, got {type(attr_value)}" ) annotations[attr_name] = fields[attr_name].type
for field_name, field_type in annotations.items(): if field_name in fields and fields[field_name].final: raise FinalFieldError( f"Final field {field_name} defined in {fields[field_name].origin_cls} overridden in {cls}" )
args = { "name": field_name, "type": field_type, "origin_cls": attrs["__qualname__"], }
if field_name in attrs: field = attrs[field_name] if isinstance(field, Field): args = {**dataclasses.asdict(field), **args} elif isinstance(field, dataclasses.Field): args = { "default": field.default, "name": field.name, "type": field.type, "init": field.init, "default_factory": field.default_factory, **args, } else: args["default"] = field else: args["default"] = dataclasses.MISSING args["default_factory"] = None args["required"] = True
field_instance = Field(**args) fields[field_name] = field_instance
return fields
def is_dataclass(obj): """Returns True if obj is a dataclass or an instance of a dataclass.""" cls = obj if isinstance(obj, type) else type(obj) return hasattr(cls, _FIELDS)
def class_fields(obj): all_fields = fields(obj) return [field for field in all_fields if field.origin_cls == obj.__class__.__qualname__]
def fields(cls): return list(getattr(cls, _FIELDS).values())
def fields_names(cls): return list(getattr(cls, _FIELDS).keys())
def final_fields(cls): return [field for field in fields(cls) if field.final]
def required_fields(cls): return [field for field in fields(cls) if field.required]
def abstract_fields(cls): return [field for field in fields(cls) if field.abstract]
def is_abstract_field(field): return field.abstract
def is_final_field(field): return field.final
def get_field_default(field): if field.default_factory is not None: return field.default_factory() else: return field.default
def asdict(obj): assert is_dataclass(obj), f"{obj} must be a dataclass, got {type(obj)} with bases {obj.__class__.__bases__}" return _asdict_inner(obj)
def _asdict_inner(obj): if is_dataclass(obj): result = {} for field in fields(obj): v = getattr(obj, field.name) result[field.name] = _asdict_inner(v) return result elif isinstance(obj, tuple) and hasattr(obj, "_fields"): # named tuple return type(obj)(*[_asdict_inner(v) for v in obj]) elif isinstance(obj, (list, tuple)): return type(obj)([_asdict_inner(v) for v in obj]) elif isinstance(obj, dict): return type(obj)({_asdict_inner(k): _asdict_inner(v) for k, v in obj.items()}) else: return copy.deepcopy(obj)
class DataclassMeta(ABCMeta): """ Metaclass for Dataclass. Checks for final fields when a subclass is created. """
@final def __init__(cls, name, bases, attrs): super().__init__(name, bases, attrs) setattr(cls, _FIELDS, get_fields(cls, attrs))
class Dataclass(metaclass=DataclassMeta): """ Base class for data-like classes that provides additional functionality and control over Python's built-in @dataclasses.dataclass decorator. Other classes can inherit from this class to get the benefits of this implementation. As a base class, it ensures that all subclasses will automatically be data classes.
The usage and field definitions are similar to Python's built-in @dataclasses.dataclass decorator. However, this implementation provides additional classes for defining "final", "required", and "abstract" fields.
Key enhancements of this custom implementation:
1. Automatic Data Class Creation: All subclasses automatically become data classes, without needing to use the @dataclasses.dataclass decorator.
2. Field Immutability: Supports creation of "final" fields (using FinalField class) that cannot be overridden by subclasses. This functionality is not natively supported in Python or in the built-in dataclasses module.
3. Required Fields: Supports creation of "required" fields (using RequiredField class) that must be provided when creating an instance of the class, adding a level of validation not present in the built-in dataclasses module.
4. Abstract Fields: Supports creation of "abstract" fields (using AbstractField class) that must be overridden by any non-abstract subclass. This is similar to abstract methods in an abc.ABC class, but applied to fields.
5. Type Checking: Performs type checking to ensure that if a field is redefined in a subclass, the type of the field remains consistent, adding static type checking not natively supported in Python.
6. Error Definitions: Defines specific error types (FinalFieldError, RequiredFieldError, AbstractFieldError, TypeMismatchError) for providing detailed error information during debugging.
7. MetaClass Usage: Uses a metaclass (DataclassMeta) for customization of class creation, allowing checks and alterations to be made at the time of class creation, providing more control.
Example: ``` class Parent(Dataclass): final_field: int = FinalField(1) # this field cannot be overridden required_field: str = RequiredField() also_required_field: float abstract_field: int = AbstractField()
class Child(Parent): abstract_field = 3 # now once overridden, this is no longer abstract required_field = Field(name="required_field", default="provided", type=str)
class Mixin(Dataclass): mixin_field = Field(name="mixin_field", default="mixin", type=str)
class GrandChild(Child, Mixin): pass
grand_child = GrandChild() print(grand_child.to_dict()) ```
"""
@final def __init__(self, *args, **kwargs): """ Initialize fields based on kwargs. Checks for abstract fields when an instance is created. """ init_fields = [field for field in fields(self) if field.init] for field, arg in zip(init_fields, args): kwargs[field.name] = arg
for field in abstract_fields(self): raise AbstractFieldError( f"Abstract field '{field.name}' of class {field.origin_cls} not implemented in {self.__class__.__name__}" )
for field in required_fields(self): if field.name not in kwargs: raise RequiredFieldError( f"Required field '{field.name}' of class {field.origin_cls} not set in {self.__class__.__name__}" )
for field in fields(self): if field.name in kwargs: setattr(self, field.name, kwargs[field.name]) else: setattr(self, field.name, get_field_default(field))
self.__post_init__()
@property def __is_dataclass__(self) -> bool: return True
def __post_init__(self): """ Post initialization hook. """ pass
try: class CorrectChildClass(ParentClass): abstract_field: int = 1 # This correctly overrides the abstract field correct_child_class_instance = CorrectChildClass() print(f'CorrectChildClass instance: {correct_child_class_instance} - passed') except Exception as e: print(f'CorrectChildClass: {str(e)} - failed')
try: class IncorrectChildClass1(ParentClass): pass # This fails to override the abstract field print(f'IncorrectChildClass1: {IncorrectChildClass1} - passed') except Exception as e: print(f'IncorrectChildClass1: {str(e)} - failed')
try: incorrect_child_class1_instance = IncorrectChildClass1() print(f'IncorrectChildClass1 instance: {incorrect_child_class1_instance} - failed') except Exception as e: print(f'IncorrectChildClass1 instantiation: {str(e)} - passed')
# Testing the final field functionality
try: class IncorrectChildClass2(ParentClass): final_field: str = 'Hello' # This attempts to override the final field print(f'IncorrectChildClass2: {IncorrectChildClass2} - failed') except Exception as e: print(f'IncorrectChildClass2: {str(e)} - passed') def to_dict(self): """ Convert to dict. """ return asdict(self)
def __repr__(self) -> str: """ String representation. """ return f"{self.__class__.__name__}({', '.join([f'{field.name}={repr(getattr(self, field.name))}' for field in fields(self)])})"dataclass.py+331-72