@@ -45,23 +45,23 @@ class Class:
4545
4646 def __init__ (
4747 self ,
48- cls ,
49- init = True ,
50- public = True ,
51- inherited = True ,
52- static_methods = True ,
53- protected = False ,
54- private = False ,
55- classmethod = True ,
56- skip_self = True ,
48+ cls : type | object ,
49+ init : bool = True ,
50+ public : bool = True ,
51+ inherited : bool = True ,
52+ static_methods : bool = True ,
53+ protected : bool = False ,
54+ private : bool = False ,
55+ classmethod : bool = True ,
56+ skip_self : bool = True ,
5757 ) -> None :
5858 self .cls = cls
5959 self .is_initialized = False
6060 self .skip_self = skip_self
6161 self .receieved_instance = False
6262
6363 try :
64- self .name : str = self .cls . __name__
64+ self .name : str = getattr ( self .cls , " __name__" , str ( self . cls ))
6565 except AttributeError :
6666 self .receieved_instance = True
6767 self .name = f"{ self .cls .__class__ .__name__ } instance"
@@ -90,10 +90,10 @@ def __repr__(self) -> str:
9090 return f"{ self .__class__ .__name__ } (name='{ self .name } ', methods={ len (self ._methods )} , has_init={ self .has_init } , description={ self .description } )"
9191
9292 @functools .cached_property
93- def _class_base (self ):
93+ def _class_base (self ) -> type :
9494 if self .is_initialized :
95- return self .cls .__class__
96- return self .cls
95+ return self .cls .__class__ if hasattr ( self . cls , "__class__" ) else type ( self . cls )
96+ return self .cls # type: ignore[return-value]
9797
9898 def _find_methods (self ) -> dict [str , Method ]:
9999 method_filter = MethodFilter (** self .extractor_kwargs )
@@ -106,7 +106,7 @@ def _find_methods(self) -> dict[str, Method]:
106106 methods [method .name ] = method
107107 return methods
108108
109- def init (self , * args , ** kwargs ) -> None :
109+ def init (self , * args : Any , ** kwargs : Any ) -> None :
110110 """
111111 Initializes the class as an instance using the provided arguments.
112112
@@ -116,10 +116,13 @@ def init(self, *args, **kwargs) -> None:
116116 """
117117 if self .is_initialized :
118118 raise ValueError (f"Class { self .cls } is already initialized" )
119- self .instance = self .cls (* args , ** kwargs )
119+ if callable (self .cls ):
120+ self .instance = self .cls (* args , ** kwargs )
121+ else :
122+ raise TypeError (f"Cannot initialize object of type { type (self .cls )} " )
120123 self .is_initialized = True
121124
122- def call_method (self , method : str | int , * args , ** kwargs ) -> Any :
125+ def call_method (self , method : str | int , * args : Any , ** kwargs : Any ) -> Any :
123126 """
124127 Calls the specified method on the class or instance.
125128
@@ -160,7 +163,7 @@ def get_method(self, method: str | int) -> Method:
160163 raise TypeError (type (method ))
161164
162165 @property
163- def init_method (self ):
166+ def init_method (self ) -> Method | None :
164167 try :
165168 return self .get_method ("__init__" )
166169 except KeyError :
@@ -188,11 +191,16 @@ def dict(self) -> dict[str, Any]:
188191 }
189192
190193 def as_str (
191- self , * , color : bool = True , indent : int = 2 , theme : ClassStrTheme | None = None
194+ self ,
195+ * ,
196+ color : bool = True ,
197+ indent : int = 2 ,
198+ theme : ClassStrTheme | None = None ,
192199 ) -> str :
193200 if theme is None :
194201 theme = ClassStrTheme ()
195202
203+ string : str
196204 if color :
197205 string = colored ("class" , theme .class_kw ) + " " + colored (self .name , theme .name ) + ":"
198206 else :
@@ -202,7 +210,7 @@ def as_str(
202210 if color :
203211 string += "\n " + colored (self .description , theme .description )
204212 else :
205- string += "\n " + self .description
213+ string += "\n " + str ( self .description )
206214 if not len (self .methods ):
207215 return string
208216
@@ -211,7 +219,11 @@ def as_str(
211219 return string
212220
213221
214- def split_init_args (args : dict , cls : Class , method : Method ) -> tuple [dict , dict ]:
222+ def split_init_args (
223+ args : dict [str , Any ],
224+ cls : Class ,
225+ method : Method ,
226+ ) -> tuple [dict [str , Any ], dict [str , Any ]]:
215227 """
216228 Split the arguments into those that should be passed to the __init__ method
217229 and those that should be passed to the method call.
0 commit comments