38
38
import dataclasses
39
39
import functools
40
40
import typing as t
41
- from typing import (
42
- Any ,
43
- Callable ,
44
- Protocol ,
45
- TypeVar ,
46
- runtime_checkable ,
47
- )
48
41
49
42
# Type definitions for better type hints
50
- T = TypeVar ("T" , bound = type )
43
+ T = t . TypeVar ("T" , bound = type )
51
44
52
45
53
- @runtime_checkable
54
- class SealableProtocol (Protocol ):
46
+ @t . runtime_checkable
47
+ class SealableProtocol (t . Protocol ):
55
48
"""Protocol defining the interface for sealable objects."""
56
49
57
50
_sealed : bool
@@ -116,8 +109,8 @@ def is_sealable(cls) -> bool:
116
109
117
110
118
111
def mutable_field (
119
- factory : Callable [[], Any ] = list ,
120
- ) -> dataclasses .Field [Any ]:
112
+ factory : t . Callable [[], t . Any ] = list ,
113
+ ) -> dataclasses .Field [t . Any ]:
121
114
"""Create a field that is mutable during initialization but immutable after sealing.
122
115
123
116
Parameters
@@ -136,8 +129,8 @@ def mutable_field(
136
129
137
130
138
131
def 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
141
134
"""Mark a field as mutable during initialization but immutable after sealing.
142
135
143
136
This decorator applies to a method that returns the field's default value.
@@ -230,7 +223,7 @@ def mutable_during_init(
230
223
)
231
224
232
225
233
- def is_sealable (cls_or_obj : Any ) -> bool :
226
+ def is_sealable (cls_or_obj : t . Any ) -> bool :
234
227
"""Check if a class or object is sealable.
235
228
236
229
Parameters
@@ -498,7 +491,7 @@ def frozen_dataclass_sealable(cls: type) -> type:
498
491
mutable_fields .add (name )
499
492
500
493
# 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 :
502
495
# Allow setting private attributes always
503
496
if name .startswith ("_" ):
504
497
object .__setattr__ (self , name , value )
@@ -525,7 +518,7 @@ def custom_setattr(self: Any, name: str, value: Any) -> None:
525
518
raise AttributeError (error_msg )
526
519
527
520
# Custom attribute deletion implementation
528
- def custom_delattr (self : Any , name : str ) -> None :
521
+ def custom_delattr (self : t . Any , name : str ) -> None :
529
522
if name .startswith ("_" ):
530
523
object .__delattr__ (self , name )
531
524
return
@@ -539,7 +532,7 @@ def custom_delattr(self: Any, name: str) -> None:
539
532
raise AttributeError (error_msg )
540
533
541
534
# 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 :
543
536
# Set the initializing flag
544
537
object .__setattr__ (self , "_initializing" , True )
545
538
object .__setattr__ (self , "_sealed" , False )
@@ -643,7 +636,7 @@ def custom_init(self: Any, *args: Any, **kwargs: Any) -> None:
643
636
seal_method ()
644
637
645
638
# 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 :
647
640
"""Seal the object to prevent further modifications.
648
641
649
642
Parameters
0 commit comments