55import os
66from collections .abc import MutableMapping
77from dataclasses import MISSING as _MISSING
8- from dataclasses import Field , asdict , dataclass , fields , is_dataclass
8+ from dataclasses import Field , asdict , dataclass , fields , is_dataclass , replace
99from pathlib import Path
1010from pprint import pprint
1111from typing import Any , Dict , Generic , List , Optional , Type , TypeVar , Union , get_type_hints
@@ -558,20 +558,8 @@ def _is_initialized(self):
558558 at the initialization when no attribute has been defined."""
559559 return "_initialized" in vars (self ) and self ._initialized
560560
561- def __setattr__ (self , name : str , value : Any ) -> None :
562- if self ._is_initialized () and issubclass (type (value ), Coqpit ):
563- self .__fields__ [name ].type = type (value )
564- return super ().__setattr__ (name , value )
565-
566- def __set_fields (self ):
567- """Create a list of fields defined at the object initialization"""
568- self .__fields__ = {} # pylint: disable=attribute-defined-outside-init
569- for field in fields (self ):
570- self .__fields__ [field .name ] = field
571-
572561 def __post_init__ (self ):
573562 self ._initialized = True
574- self .__set_fields ()
575563 try :
576564 self .check_values ()
577565 except AttributeError :
@@ -583,7 +571,7 @@ def __iter__(self):
583571 return iter (asdict (self ))
584572
585573 def __len__ (self ):
586- return len (self . __fields__ )
574+ return len (fields ( self ) )
587575
588576 def __setitem__ (self , arg : str , value : Any ):
589577 setattr (self , arg , value )
@@ -645,6 +633,9 @@ def check_values(self):
645633 def has (self , arg : str ) -> bool :
646634 return arg in vars (self )
647635
636+ def copy (self ):
637+ return replace (self )
638+
648639 def update (self , new : dict , allow_new = False ) -> None :
649640 """Update Coqpit fields by the input ```dict```.
650641
0 commit comments