3838import dataclasses
3939import functools
4040import typing as t
41- from typing import (
42- Any ,
43- Callable ,
44- Protocol ,
45- TypeVar ,
46- runtime_checkable ,
47- )
4841
4942# Type definitions for better type hints
50- T = TypeVar ("T" , bound = type )
43+ T = t . TypeVar ("T" , bound = type )
5144
5245
53- @runtime_checkable
54- class SealableProtocol (Protocol ):
46+ @t . runtime_checkable
47+ class SealableProtocol (t . Protocol ):
5548 """Protocol defining the interface for sealable objects."""
5649
5750 _sealed : bool
@@ -116,8 +109,8 @@ def is_sealable(cls) -> bool:
116109
117110
118111def mutable_field (
119- factory : Callable [[], Any ] = list ,
120- ) -> dataclasses .Field [Any ]:
112+ factory : t . Callable [[], t . Any ] = list ,
113+ ) -> dataclasses .Field [t . Any ]:
121114 """Create a field that is mutable during initialization but immutable after sealing.
122115
123116 Parameters
@@ -136,8 +129,8 @@ def mutable_field(
136129
137130
138131def mutable_during_init (
139- field_method : Callable [[], T ] | None = None ,
140- ) -> Any : # mypy doesn't handle complex return types well here
132+ field_method : t . Callable [[], T ] | None = None ,
133+ ) -> t . Any : # mypy doesn't handle complex return types well here
141134 """Mark a field as mutable during initialization but immutable after sealing.
142135
143136 This decorator applies to a method that returns the field's default value.
@@ -230,7 +223,7 @@ def mutable_during_init(
230223 )
231224
232225
233- def is_sealable (cls_or_obj : Any ) -> bool :
226+ def is_sealable (cls_or_obj : t . Any ) -> bool :
234227 """Check if a class or object is sealable.
235228
236229 Parameters
@@ -498,7 +491,7 @@ def frozen_dataclass_sealable(cls: type) -> type:
498491 mutable_fields .add (name )
499492
500493 # Custom attribute setting implementation
501- def custom_setattr (self : Any , name : str , value : Any ) -> None :
494+ def custom_setattr (self : t . Any , name : str , value : t . Any ) -> None :
502495 # Allow setting private attributes always
503496 if name .startswith ("_" ):
504497 object .__setattr__ (self , name , value )
@@ -525,7 +518,7 @@ def custom_setattr(self: Any, name: str, value: Any) -> None:
525518 raise AttributeError (error_msg )
526519
527520 # Custom attribute deletion implementation
528- def custom_delattr (self : Any , name : str ) -> None :
521+ def custom_delattr (self : t . Any , name : str ) -> None :
529522 if name .startswith ("_" ):
530523 object .__delattr__ (self , name )
531524 return
@@ -539,7 +532,7 @@ def custom_delattr(self: Any, name: str) -> None:
539532 raise AttributeError (error_msg )
540533
541534 # Custom initialization to set initial attribute values
542- def custom_init (self : Any , * args : Any , ** kwargs : Any ) -> None :
535+ def custom_init (self : t . Any , * args : t . Any , ** kwargs : t . Any ) -> None :
543536 # Set the initializing flag
544537 object .__setattr__ (self , "_initializing" , True )
545538 object .__setattr__ (self , "_sealed" , False )
@@ -643,7 +636,7 @@ def custom_init(self: Any, *args: Any, **kwargs: Any) -> None:
643636 seal_method ()
644637
645638 # Define methods that will be attached to the class
646- def seal_method (self : Any , deep : bool = False ) -> None :
639+ def seal_method (self : t . Any , deep : bool = False ) -> None :
647640 """Seal the object to prevent further modifications.
648641
649642 Parameters
0 commit comments