Skip to content

Commit c374ee1

Browse files
committed
format examples for mkdocs
1 parent fc3cb6a commit c374ee1

File tree

6 files changed

+90
-64
lines changed

6 files changed

+90
-64
lines changed

docs/reference/typing.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: objinspect.typing

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ nav:
8888
- Method: reference/method.md
8989
- Function: reference/function.md
9090
- Class: reference/class.md
91+
- typing: reference/typing.md
9192
- util: reference/util.md
9293

9394
extra:

objinspect/__init__.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,24 @@ def inspect(
3939
Returns:
4040
An object representing the structure of the inspected object.
4141
42-
4342
Example:
44-
``` python
45-
>>> from objinspect import inspect, pdir
46-
>>> import math
47-
>>> inspect(math.pow)
48-
Function(name='pow', parameters=2, description='Return x**y (x to the power of y).')
49-
50-
>>> inspect(math.pow).dict
51-
{
52-
'name': 'pow',
53-
'parameters': [
54-
{'name': 'x', 'kind': <_ParameterKind.POSITIONAL_ONLY: 0>, 'type': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>, 'description': None},
55-
{'name': 'y', 'kind': <_ParameterKind.POSITIONAL_ONLY: 0>, 'type': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>, 'description': None}],
56-
'docstring': 'Return x**y (x to the power of y).'
57-
}
58-
59-
>>> inspect(inspect)
60-
Function(name='inspect', parameters=7, description='Inspects an object and returns a structured representation of its attributes and methods.')
43+
```python
44+
>>> from objinspect import inspect, pdir
45+
>>> import math
46+
>>> inspect(math.pow)
47+
Function(name='pow', parameters=2, description='Return x**y (x to the power of y).')
48+
49+
>>> inspect(math.pow).dict
50+
{
51+
'name': 'pow',
52+
'parameters': [
53+
{'name': 'x', 'kind': <_ParameterKind.POSITIONAL_ONLY: 0>, 'type': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>, 'description': None},
54+
{'name': 'y', 'kind': <_ParameterKind.POSITIONAL_ONLY: 0>, 'type': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>, 'description': None}],
55+
'docstring': 'Return x**y (x to the power of y).'
56+
}
57+
58+
>>> inspect(inspect)
59+
Function(name='inspect', parameters=7, description='Inspects an object and returns a structured representation of its attributes and methods.')
6160
```
6261
"""
6362
if _inspect.isclass(obj):

objinspect/method.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ def is_inherited(self) -> bool:
7171
class MethodFilter:
7272
def __init__(
7373
self,
74-
init=True,
75-
public=True,
76-
inherited=True,
77-
static_methods=True,
78-
protected=False,
79-
private=False,
80-
classmethod=False,
74+
init: bool = True,
75+
public: bool = True,
76+
inherited: bool = True,
77+
static_methods: bool = True,
78+
protected: bool = False,
79+
private: bool = False,
80+
classmethod: bool = False,
8181
) -> None:
8282
self.checks = []
8383
if not init:

objinspect/typing.py

+56-34
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ def type_name(t: T.Any) -> str:
2121
str: The string representation of the Python type.
2222
2323
Example:
24+
```python
2425
>>> type_to_str(datetime.datetime)
2526
'datetime'
2627
>>> type_to_str(int)
2728
'int'
29+
```
2830
"""
2931
type_str = repr(t)
3032
if "<class '" in type_str:
@@ -37,13 +39,15 @@ def is_generic_alias(t) -> bool:
3739
Check if a type is an alias type (list[str], dict[str, int], etc...])
3840
3941
Example:
40-
>>> is_generic_alias(list[str])
41-
True
42-
>>> is_generic_alias(int)
43-
False
44-
>>> String = str
45-
>>> is_generic_alias(String)
46-
False
42+
```python
43+
>>> is_generic_alias(list[str])
44+
True
45+
>>> is_generic_alias(int)
46+
False
47+
>>> String = str
48+
>>> is_generic_alias(String)
49+
False
50+
```
4751
"""
4852
return type(t) in ALIAS_TYPES
4953

@@ -52,14 +56,16 @@ def is_union_type(t) -> bool:
5256
"""
5357
Check if a type is a union type (float | int, str | None, etc...)
5458
55-
Examples:
56-
>>> is_union_type(int | str)
57-
True
58-
>>> from typing import Union
59-
>>> is_union_type(Union[int, str])
60-
True
61-
>>> is_union_type(list)
62-
False
59+
Example:
60+
```python
61+
>>> is_union_type(int | str)
62+
True
63+
>>> from typing import Union
64+
>>> is_union_type(Union[int, str])
65+
True
66+
>>> is_union_type(list)
67+
False
68+
```
6369
"""
6470
return type(t) in UNION_TYPES
6571

@@ -68,7 +74,8 @@ def is_iterable_type(t) -> bool:
6874
"""
6975
Check if a type is an iterable type (list, tuple, etc...)
7076
71-
Examples:
77+
Example:
78+
```python
7279
>>> is_iterable_type(list)
7380
True
7481
>>> is_iterable_type(dict)
@@ -79,6 +86,7 @@ def is_iterable_type(t) -> bool:
7986
True
8087
>>> is_iterable_type(typing.Dict)
8188
True
89+
```
8290
"""
8391
typing_iterables = [
8492
typing.List,
@@ -113,7 +121,8 @@ def is_mapping_type(t) -> bool:
113121
"""
114122
Check if a type is a mapping type (dict, OrderedDict, etc...)
115123
116-
Examples:
124+
Example:
125+
```python
117126
>>> is_mapping_type(dict)
118127
True
119128
>>> is_mapping_type(list)
@@ -122,6 +131,7 @@ def is_mapping_type(t) -> bool:
122131
True
123132
>>> is_mapping_type(typing.OrderedDict)
124133
True
134+
```
125135
"""
126136
typing_mappings = [
127137
typing.Dict,
@@ -140,11 +150,13 @@ def is_mapping_type(t) -> bool:
140150

141151
def type_simplified(t: T.Any) -> T.Any | tuple[T.Any, ...]:
142152
"""
143-
Examples:
144-
>>> type_simplify(list[str])
145-
<class 'list'>
146-
>>> type_simplify(float | list[str])
147-
(<class 'float'>, <class 'list'>)
153+
Example:
154+
```python
155+
>>> type_simplify(list[str])
156+
<class 'list'>
157+
>>> type_simplify(float | list[str])
158+
(<class 'float'>, <class 'list'>)
159+
```
148160
"""
149161
origin = type_origin(t)
150162
if isinstance(type(origin), types.NoneType) or origin is None:
@@ -172,13 +184,15 @@ def get_enum_choices(e) -> tuple[str, ...]:
172184
tuple: A tuple of the names of the Enum options.
173185
174186
Example:
187+
```python
175188
>>> import enum
176189
>>> class Color(enum.Enum):
177190
... RED = 1
178191
... GREEN = 2
179192
... BLUE = 3
180193
>>> get_enum_choices(Color)
181194
('RED', 'GREEN', 'BLUE')
195+
```
182196
"""
183197
if not is_enum(e):
184198
raise TypeError(f"'{e}' is not an Enum")
@@ -197,7 +211,8 @@ def is_direct_literal(t: T.Any) -> bool:
197211
Returns:
198212
bool: True if the type is a pure Literal, False otherwise.
199213
200-
Examples:
214+
Example:
215+
```python
201216
>>> from typing_extensions import Literal
202217
>>> is_direct_literal(Literal[1, 2, 3])
203218
True
@@ -207,6 +222,7 @@ def is_direct_literal(t: T.Any) -> bool:
207222
False
208223
>>> is_direct_literal(Union[str, Literal[1, 2]])
209224
False
225+
```
210226
"""
211227
if t is typing_extensions.Literal:
212228
return False
@@ -219,17 +235,19 @@ def is_or_contains_literal(t: T.Any) -> bool:
219235
"""
220236
Determine if the given type is a Literal type or contains a Literal type.
221237
222-
Examples:
223-
>>> from typing import Union, Optional
224-
>>> from typing_extensions import Literal
225-
>>> is_or_contains_literal(Literal[1, 2, 3])
226-
True
227-
>>> is_or_contains_literal(Union[int, Literal[1, 2]])
228-
True
229-
>>> is_or_contains_literal(Optional[Literal['a', 'b']])
230-
True
231-
>>> is_or_contains_literal(int)
232-
False
238+
Example:
239+
```python
240+
>>> from typing import Union, Optional
241+
>>> from typing_extensions import Literal
242+
>>> is_or_contains_literal(Literal[1, 2, 3])
243+
True
244+
>>> is_or_contains_literal(Union[int, Literal[1, 2]])
245+
True
246+
>>> is_or_contains_literal(Optional[Literal['a', 'b']])
247+
True
248+
>>> is_or_contains_literal(int)
249+
False
250+
```
233251
"""
234252
if is_direct_literal(t):
235253
return True
@@ -270,10 +288,12 @@ def type_origin(t: T.Any) -> T.Any:
270288
A wrapper for typing.get_origin to get the origin of a type.
271289
272290
Example:
291+
```python
273292
>>> type_args(list[list[str]])
274293
<class 'list'>
275294
>>> type_origin(float | int)
276295
<class 'types.UnionType'>
296+
```
277297
"""
278298
return typing.get_origin(t)
279299

@@ -283,12 +303,14 @@ def type_args(t: T.Any) -> tuple[T.Any, ...]:
283303
A wrapper for typing.get_args to get the arguments of a type.
284304
285305
Example:
306+
```python
286307
>>> type_args(list[str])
287308
(<class 'str'>,)
288309
>>> type_args(dict[str, int])
289310
(<class 'str'>, <class 'int'>)
290311
>>> type_args(list[list[str]])
291312
(list[str],)
313+
```
292314
"""
293315
return typing.get_args(t)
294316

objinspect/util.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ def call_method(obj: object, name: str, args: tuple = (), kwargs: dict = {}) ->
1717
Returns:
1818
object: The result of calling the method.
1919
20-
Examples:
21-
>>> import math
22-
>>> call_method(math, "pow", args=(2, 2))
23-
4.0
20+
Example:
21+
```python
22+
>>> import math
23+
>>> call_method(math, "pow", args=(2, 2))
24+
4.0
25+
```
2426
"""
2527
return getattr(obj, name)(*args, **kwargs)
2628

@@ -56,6 +58,7 @@ def create_function(
5658
docstring (str, optional): The docstring of the function.
5759
5860
Example:
61+
```python
5962
>>> add = create_function(
6063
... name="add",
6164
... args={
@@ -71,7 +74,7 @@ def create_function(
7174
... )
7275
>>> add(2, 2)
7376
4
74-
77+
```
7578
"""
7679

7780
arg_str = []

0 commit comments

Comments
 (0)