Skip to content

inspectfilters

Class info

🛈 DocStrings

get_argspec

get_argspec(obj: Any, remove_self: bool = True) -> FullArgSpec

Return a cleaned-up FullArgSpec for given callable.

ArgSpec is cleaned up by removing self from method callables.

Parameters:

Name Type Description Default
obj Any

A callable python object

required
remove_self bool

Whether to remove "self" argument from method argspecs

True
Source code in src/jinjarope/inspectfilters.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def get_argspec(obj: Any, remove_self: bool = True) -> inspect.FullArgSpec:
    """Return a cleaned-up FullArgSpec for given callable.

    ArgSpec is cleaned up by removing `self` from method callables.

    Args:
        obj: A callable python object
        remove_self: Whether to remove "self" argument from method argspecs
    """
    if inspect.isfunction(obj):
        argspec = inspect.getfullargspec(obj)
    elif inspect.ismethod(obj):
        argspec = inspect.getfullargspec(obj)
        if remove_self:
            del argspec.args[0]
    elif inspect.isclass(obj):
        if obj.__init__ is object.__init__:  # to avoid an error
            argspec = inspect.getfullargspec(lambda self: None)
        else:
            argspec = inspect.getfullargspec(obj.__init__)
        if remove_self:
            del argspec.args[0]
    elif callable(obj):
        argspec = inspect.getfullargspec(obj.__call__)
        if remove_self:
            del argspec.args[0]
    else:
        msg = f"{obj} is not callable"
        raise TypeError(msg)
    return argspec

get_deprecated_message

get_deprecated_message(obj: Any) -> str | None

Return deprecated message (created by deprecated decorator).

Parameters:

Name Type Description Default
obj Any

Object to check

required
Source code in src/jinjarope/inspectfilters.py
230
231
232
233
234
235
236
def get_deprecated_message(obj: Any) -> str | None:
    """Return deprecated message (created by deprecated decorator).

    Args:
        obj: Object to check
    """
    return obj.__deprecated__ if hasattr(obj, "__deprecated__") else None

get_doc cached

get_doc(
    obj: Any,
    *,
    escape: bool = False,
    fallback: str = "",
    from_base_classes: bool = False,
    only_summary: bool = False,
    only_description: bool = False
) -> str

Get doc for given object.

Parameters:

Name Type Description Default
obj Any

Object to get docstrings from

required
escape bool

Whether docstrings should get escaped

False
fallback str

Fallback in case docstrings dont exist

''
from_base_classes bool

Use base class docstrings if docstrings dont exist

False
only_summary bool

Only return first line of docstrings

False
only_description bool

Only return block after first line

False
Source code in src/jinjarope/inspectfilters.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@functools.cache
def get_doc(
    obj: Any,
    *,
    escape: bool = False,
    fallback: str = "",
    from_base_classes: bool = False,
    only_summary: bool = False,
    only_description: bool = False,
) -> str:
    """Get __doc__ for given object.

    Args:
        obj: Object to get docstrings from
        escape: Whether docstrings should get escaped
        fallback: Fallback in case docstrings dont exist
        from_base_classes: Use base class docstrings if docstrings dont exist
        only_summary: Only return first line of docstrings
        only_description: Only return block after first line
    """
    from jinjarope import mdfilters

    match obj:
        case _ if from_base_classes:
            doc = inspect.getdoc(obj)
        case _ if obj.__doc__:
            doc = inspect.cleandoc(obj.__doc__)
        case _:
            doc = None
    if not doc:
        return fallback
    if only_summary:
        doc = doc.split("\n")[0]
    if only_description:
        doc = "\n".join(doc.split("\n")[1:])
    return mdfilters.md_escape(doc) if doc and escape else doc

get_file cached

get_file(obj: HasCodeType) -> Path | None

Cached wrapper for inspect.getfile.

Parameters:

Name Type Description Default
obj HasCodeType

Object to get file for

required
Source code in src/jinjarope/inspectfilters.py
280
281
282
283
284
285
286
287
288
289
@functools.cache
def get_file(obj: HasCodeType) -> pathlib.Path | None:
    """Cached wrapper for inspect.getfile.

    Args:
        obj: Object to get file for
    """
    with contextlib.suppress(TypeError):
        return pathlib.Path(inspect.getfile(obj))
    return None

get_members cached

get_members(obj: object, predicate: Callable[[Any], bool] | None = None)

Cached version of inspect.getmembers.

Parameters:

Name Type Description Default
obj object

Object to get members for

required
predicate Callable[[Any], bool] | None

Optional predicate for the members

None
Source code in src/jinjarope/inspectfilters.py
269
270
271
272
273
274
275
276
277
@functools.cache
def get_members(obj: object, predicate: Callable[[Any], bool] | None = None):
    """Cached version of inspect.getmembers.

    Args:
        obj: Object to get members for
        predicate: Optional predicate for the members
    """
    return inspect.getmembers(obj, predicate)

get_signature cached

get_signature(obj: Callable[..., Any]) -> Signature

Cached wrapper for inspect.signature.

Parameters:

Name Type Description Default
obj Callable[..., Any]

Callable to get a signature for.

required
Source code in src/jinjarope/inspectfilters.py
259
260
261
262
263
264
265
266
@functools.cache
def get_signature(obj: Callable[..., Any]) -> inspect.Signature:
    """Cached wrapper for inspect.signature.

    Args:
        obj: Callable to get a signature for.
    """
    return inspect.signature(obj)

get_source cached

get_source(obj: HasCodeType) -> str

Cached wrapper for inspect.getsource.

Parameters:

Name Type Description Default
obj HasCodeType

Object to return source for.

required
Source code in src/jinjarope/inspectfilters.py
239
240
241
242
243
244
245
246
@functools.cache
def get_source(obj: HasCodeType) -> str:
    """Cached wrapper for inspect.getsource.

    Args:
        obj: Object to return source for.
    """
    return inspect.getsource(obj)

get_source_lines cached

get_source_lines(obj: HasCodeType) -> tuple[list[str], int]

Cached wrapper for inspect.getsourcelines.

Parameters:

Name Type Description Default
obj HasCodeType

Object to return source lines for.

required
Source code in src/jinjarope/inspectfilters.py
249
250
251
252
253
254
255
256
@functools.cache
def get_source_lines(obj: HasCodeType) -> tuple[list[str], int]:
    """Cached wrapper for inspect.getsourcelines.

    Args:
        obj: Object to return source lines for.
    """
    return inspect.getsourcelines(obj)

iter_baseclasses

iter_baseclasses(
    klass: ClassType,
    *,
    recursive: bool = True,
    filter_abstract: bool = False,
    filter_generic: bool = True,
    filter_locals: bool = True
) -> Iterator[ClassType]

(Recursively) iterate all baseclasses of given klass.

Parameters:

Name Type Description Default
klass ClassType

class to get subclasses from

required
filter_abstract bool

whether abstract base classes should be included.

False
filter_generic bool

whether generic base classes should be included.

True
filter_locals bool

whether local base classes should be included.

True
recursive bool

whether to also get baseclasses of baseclasses.

True
Source code in src/jinjarope/inspectfilters.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def iter_baseclasses(
    klass: ClassType,
    *,
    recursive: bool = True,
    filter_abstract: bool = False,
    filter_generic: bool = True,
    filter_locals: bool = True,
) -> Iterator[ClassType]:
    """(Recursively) iterate all baseclasses of given klass.

    Args:
        klass: class to get subclasses from
        filter_abstract: whether abstract base classes should be included.
        filter_generic: whether generic base classes should be included.
        filter_locals: whether local base classes should be included.
        recursive: whether to also get baseclasses of baseclasses.
    """
    for kls in klass.__bases__:
        if recursive:
            yield from iter_baseclasses(
                kls,
                recursive=recursive,
                filter_abstract=filter_abstract,
                filter_generic=filter_generic,
                filter_locals=filter_locals,
            )
        if filter_abstract and inspect.isabstract(kls):
            continue
        if filter_generic and kls.__qualname__.endswith("]"):
            continue
        if filter_locals and "<locals>" in kls.__qualname__:
            continue
        yield kls

iter_subclasses

iter_subclasses(
    klass: ClassType,
    *,
    recursive: bool = True,
    filter_abstract: bool = False,
    filter_generic: bool = True,
    filter_locals: bool = True
) -> Iterator[ClassType]

(Recursively) iterate all subclasses of given klass.

Parameters:

Name Type Description Default
klass ClassType

class to get subclasses from

required
filter_abstract bool

whether abstract base classes should be included.

False
filter_generic bool

whether generic base classes should be included.

True
filter_locals bool

whether local base classes should be included.

True
recursive bool

whether to also get subclasses of subclasses.

True
Source code in src/jinjarope/inspectfilters.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def iter_subclasses(
    klass: ClassType,
    *,
    recursive: bool = True,
    filter_abstract: bool = False,
    filter_generic: bool = True,
    filter_locals: bool = True,
) -> Iterator[ClassType]:
    """(Recursively) iterate all subclasses of given klass.

    Args:
        klass: class to get subclasses from
        filter_abstract: whether abstract base classes should be included.
        filter_generic: whether generic base classes should be included.
        filter_locals: whether local base classes should be included.
        recursive: whether to also get subclasses of subclasses.
    """
    if getattr(klass.__subclasses__, "__self__", None) is None:
        return
    for kls in klass.__subclasses__():
        if recursive:
            yield from iter_subclasses(
                kls,
                filter_abstract=filter_abstract,
                filter_generic=filter_generic,
                filter_locals=filter_locals,
            )
        if filter_abstract and inspect.isabstract(kls):
            continue
        if filter_generic and kls.__qualname__.endswith("]"):
            continue
        if filter_locals and "<locals>" in kls.__qualname__:
            continue
        yield kls

list_baseclasses cached

list_baseclasses(
    klass: ClassType,
    *,
    recursive: bool = True,
    filter_abstract: bool = False,
    filter_generic: bool = True,
    filter_locals: bool = True
) -> list[ClassType]

Return list of all baseclasses of given klass.

Parameters:

Name Type Description Default
klass ClassType

class to get subclasses from

required
filter_abstract bool

whether abstract base classes should be included.

False
filter_generic bool

whether generic base classes should be included.

True
filter_locals bool

whether local base classes should be included.

True
recursive bool

whether to also get baseclasses of baseclasses.

True
Source code in src/jinjarope/inspectfilters.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@functools.cache
def list_baseclasses(
    klass: ClassType,
    *,
    recursive: bool = True,
    filter_abstract: bool = False,
    filter_generic: bool = True,
    filter_locals: bool = True,
) -> list[ClassType]:
    """Return list of all baseclasses of given klass.

    Args:
        klass: class to get subclasses from
        filter_abstract: whether abstract base classes should be included.
        filter_generic: whether generic base classes should be included.
        filter_locals: whether local base classes should be included.
        recursive: whether to also get baseclasses of baseclasses.
    """
    return list(
        iter_baseclasses(
            klass,
            recursive=recursive,
            filter_abstract=filter_abstract,
            filter_generic=filter_generic,
            filter_locals=filter_locals,
        ),
    )

list_subclasses cached

list_subclasses(
    klass: ClassType,
    *,
    recursive: bool = True,
    filter_abstract: bool = False,
    filter_generic: bool = True,
    filter_locals: bool = True
) -> list[ClassType]

Return list of all subclasses of given klass.

Note: This call is cached. Consider iter_subclasses for uncached iterating.

Parameters:

Name Type Description Default
klass ClassType

class to get subclasses from

required
filter_abstract bool

whether abstract base classes should be included.

False
filter_generic bool

whether generic base classes should be included.

True
filter_locals bool

whether local base classes should be included.

True
recursive bool

whether to also get subclasses of subclasses.

True
Source code in src/jinjarope/inspectfilters.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@functools.cache
def list_subclasses(
    klass: ClassType,
    *,
    recursive: bool = True,
    filter_abstract: bool = False,
    filter_generic: bool = True,
    filter_locals: bool = True,
) -> list[ClassType]:
    """Return list of all subclasses of given klass.

    Note: This call is cached. Consider iter_subclasses for uncached iterating.

    Args:
        klass: class to get subclasses from
        filter_abstract: whether abstract base classes should be included.
        filter_generic: whether generic base classes should be included.
        filter_locals: whether local base classes should be included.
        recursive: whether to also get subclasses of subclasses.
    """
    return list(
        iter_subclasses(
            klass,
            recursive=recursive,
            filter_abstract=filter_abstract,
            filter_generic=filter_generic,
            filter_locals=filter_locals,
        ),
    )