11import functools
22import inspect
33from dataclasses import dataclass
4- from typing import Any
54
65import docstring_parser
76from docstring_parser import Docstring
@@ -29,7 +28,7 @@ class Class:
2928 public (bool, optional): Include public methods.
3029 inherited (bool, optional): Include inherited methods.
3130 static_methods (bool, optional): Include static methods.
32- classmethod (bool, optional): Include class methods.
31+ class_method (bool, optional): Include class methods.
3332 protected (bool, optional): Include protected methods.
3433 private (bool, optional): Include private methods.
3534
@@ -55,9 +54,19 @@ def __init__(
5554 static_methods : bool = True ,
5655 protected : bool = False ,
5756 private : bool = False ,
58- classmethod : bool = True ,
57+ class_method : bool = True ,
5958 skip_self : bool = True ,
59+ ** legacy_options : object ,
6060 ) -> None :
61+ legacy_classmethod = legacy_options .pop ("classmethod" , None )
62+ if legacy_classmethod is not None :
63+ if not isinstance (legacy_classmethod , bool ):
64+ raise TypeError ("`classmethod` must be a bool" )
65+ class_method = legacy_classmethod
66+ if legacy_options :
67+ unexpected_keys = ", " .join (sorted (legacy_options ))
68+ raise TypeError (f"Unexpected keyword argument(s): { unexpected_keys } " )
69+
6170 self .cls = cls
6271 self .skip_self = skip_self
6372 self .receieved_instance = not inspect .isclass (cls )
@@ -79,13 +88,14 @@ def __init__(
7988 "static_methods" : static_methods ,
8089 "protected" : protected ,
8190 "private" : private ,
82- "classmethod " : classmethod ,
91+ "class_method " : class_method ,
8392 }
8493 self ._methods = self ._find_methods ()
8594 self .has_init = "__init__" in self ._methods
86- self .docstring : Docstring | None = (
87- docstring_parser .parse (self .docstring_text ) if self .has_docstring else None # type: ignore
88- )
95+ if self .has_docstring and self .docstring_text is not None :
96+ self .docstring : Docstring | None = docstring_parser .parse (self .docstring_text )
97+ else :
98+ self .docstring = None
8999 self .description = _get_docstr_description (self .docstring )
90100
91101 def __repr__ (self ) -> str :
@@ -95,7 +105,9 @@ def __repr__(self) -> str:
95105 def _class_base (self ) -> type :
96106 if self .is_initialized :
97107 return self .cls .__class__ if hasattr (self .cls , "__class__" ) else type (self .cls )
98- return self .cls # type: ignore[return-value]
108+ if isinstance (self .cls , type ):
109+ return self .cls
110+ return type (self .cls )
99111
100112 def _find_methods (self ) -> dict [str , Method ]:
101113 method_filter = MethodFilter (** self .extractor_kwargs )
@@ -112,7 +124,7 @@ def _find_methods(self) -> dict[str, Method]:
112124 methods [method .name ] = method
113125 return methods
114126
115- def init (self , * args : Any , ** kwargs : Any ) -> None :
127+ def init (self , * args : object , ** kwargs : object ) -> None :
116128 """
117129 Initializes the class as an instance using the provided arguments.
118130
@@ -128,7 +140,7 @@ def init(self, *args: Any, **kwargs: Any) -> None:
128140 raise TypeError (f"Cannot initialize object of type { type (self .cls )} " )
129141 self .is_initialized = True
130142
131- def call_method (self , method : str | int , * args : Any , ** kwargs : Any ) -> Any :
143+ def call_method (self , method : str | int , * args : object , ** kwargs : object ) -> object :
132144 """
133145 Calls the specified method on the class or instance.
134146
@@ -189,7 +201,7 @@ def methods(self) -> list[Method]:
189201 return list (self ._methods .values ())
190202
191203 @property
192- def dict (self ) -> dict [str , Any ]:
204+ def dict (self ) -> dict [str , object ]:
193205 """Return a dictionary representation of the class."""
194206 return {
195207 "name" : self .name ,
@@ -238,10 +250,10 @@ def as_str(
238250
239251
240252def split_init_args (
241- args : dict [str , Any ],
253+ args : dict [str , object ],
242254 cls : Class ,
243255 method : Method ,
244- ) -> tuple [dict [str , Any ], dict [str , Any ]]:
256+ ) -> tuple [dict [str , object ], dict [str , object ]]:
245257 """
246258 Split the arguments into those that should be passed to the __init__ method
247259 and those that should be passed to the method call.
0 commit comments