Skip to content

Commit 2a6b4c0

Browse files
committed
Make docstrings more readable by switching to Napoleon
1 parent 0d873c5 commit 2a6b4c0

18 files changed

+488
-320
lines changed

.github/CONTRIBUTING.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,20 @@ But it's way more comfortable to run it locally and catch avoidable errors befor
121121
## Code
122122

123123
- Obey [PEP 8](https://www.python.org/dev/peps/pep-0008/) and [PEP 257](https://www.python.org/dev/peps/pep-0257/).
124-
We use the `"""`-on-separate-lines style for docstrings:
124+
We use the `"""`-on-separate-lines style for docstrings with [Napoleon](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html)-style API documentation:
125125

126126
```python
127127
def func(x: str) -> str:
128128
"""
129129
Do something.
130130
131-
:param str x: A very important parameter.
131+
Arguments:
132132
133-
:rtype: str
133+
x: A very important parameter.
134+
135+
Returns:
136+
137+
A very important return value.
134138
"""
135139
```
136140
- If you add or change public APIs, tag the docstring using `.. versionadded:: 16.0.0 WHAT` or `.. versionchanged:: 16.2.0 WHAT`.

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ API Reference
292292
:members: bind, unbind, try_unbind, new, debug, info, warning, warn, error, critical, exception, log, adebug, ainfo, awarning, aerror, acritical, aexception, alog
293293

294294
.. autoclass:: AsyncBoundLogger
295+
:members: sync_bl
295296

296297
.. autoclass:: LoggerFactory
297298
:members: __call__

src/structlog/_base.py

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def unbind(self, *keys: str) -> BoundLoggerBase:
7878
"""
7979
Return a new logger with *keys* removed from the context.
8080
81-
:raises KeyError: If the key is not part of the context.
81+
Raises:
82+
83+
KeyError: If the key is not part of the context.
8284
"""
8385
bl = self.bind()
8486
for key in keys:
@@ -121,19 +123,29 @@ def _process_event(
121123
Call it to combine your *event* and *context* into an event_dict and
122124
process using the processor chain.
123125
124-
:param method_name: The name of the logger method. Is passed into
125-
the processors.
126-
:param event: The event -- usually the first positional argument to a
127-
logger.
128-
:param event_kw: Additional event keywords. For example if someone
129-
calls ``log.info("foo", bar=42)``, *event* would to be ``"foo"``
130-
and *event_kw* ``{"bar": 42}``.
126+
Arguments:
127+
128+
method_name:
129+
The name of the logger method. Is passed into the processors.
130+
131+
event:
132+
The event -- usually the first positional argument to a logger.
133+
134+
event_kw:
135+
Additional event keywords. For example if someone calls
136+
``log.info("foo", bar=42)``, *event* would to be ``"foo"`` and
137+
*event_kw* ``{"bar": 42}``.
138+
139+
Raises:
140+
141+
structlog.DropEvent: if log entry should be dropped.
131142
132-
:raises: `structlog.DropEvent` if log entry should be dropped.
133-
:raises: `ValueError` if the final processor doesn't return a
134-
str, bytes, bytearray, tuple, or a dict.
143+
ValueError:
144+
if the final processor doesn't return a str, bytes, bytearray,
145+
tuple, or a dict.
135146
136-
:returns: `tuple` of ``(*args, **kw)``
147+
Returns:
148+
`tuple` of ``(*args, **kw)``
137149
138150
.. note::
139151
@@ -169,7 +181,10 @@ def _process_event(
169181
if isinstance(event_dict, dict):
170182
return (), event_dict
171183

172-
msg = "Last processor didn't return an appropriate value. Valid return values are a dict, a tuple of (args, kwargs), bytes, or a str."
184+
msg = (
185+
"Last processor didn't return an appropriate value. "
186+
"Valid return values are a dict, a tuple of (args, kwargs), bytes, or a str."
187+
)
173188
raise ValueError(msg)
174189

175190
def _proxy_to_logger(
@@ -182,14 +197,20 @@ def _proxy_to_logger(
182197
handling :exc:`structlog.DropEvent`, and finally calls *method_name* on
183198
:attr:`_logger` with the result.
184199
185-
:param method_name: The name of the method that's going to get
186-
called. Technically it should be identical to the method the
187-
user called because it also get passed into processors.
188-
:param event: The event -- usually the first positional argument to a
189-
logger.
190-
:param event_kw: Additional event keywords. For example if someone
191-
calls ``log.info("foo", bar=42)``, *event* would to be ``"foo"``
192-
and *event_kw* ``{"bar": 42}``.
200+
Arguments:
201+
202+
method_name:
203+
The name of the method that's going to get called. Technically
204+
it should be identical to the method the user called because it
205+
also get passed into processors.
206+
207+
event:
208+
The event -- usually the first positional argument to a logger.
209+
210+
event_kw:
211+
Additional event keywords. For example if someone calls
212+
``log.info("foo", bar=42)``, *event* would to be ``"foo"`` and
213+
*event_kw* ``{"bar": 42}``.
193214
194215
.. note::
195216
@@ -211,12 +232,15 @@ def get_context(bound_logger: BindableLogger) -> Context:
211232
The type of *bound_logger* and the type returned depend on your
212233
configuration.
213234
214-
:param bound_logger: The bound logger whose context you want.
235+
Arguments:
236+
237+
bound_logger: The bound logger whose context you want.
238+
239+
Returns:
215240
216-
:returns: The *actual* context from *bound_logger*. It is *not* copied
217-
first.
241+
The *actual* context from *bound_logger*. It is *not* copied first.
218242
219-
.. versionadded:: 20.2
243+
.. versionadded:: 20.2.0
220244
"""
221245
# This probably will get more complicated in the future.
222246
return bound_logger._context

src/structlog/_config.py

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def is_configured() -> bool:
8181
8282
If `False`, *structlog* is running with builtin defaults.
8383
84-
.. versionadded: 18.1
84+
.. versionadded: 18.1.0
8585
"""
8686
return _CONFIG.is_configured
8787

@@ -94,7 +94,7 @@ def get_config() -> dict[str, Any]:
9494
9595
Changes to the returned dictionary do *not* affect *structlog*.
9696
97-
.. versionadded: 18.1
97+
.. versionadded: 18.1.0
9898
"""
9999
return {
100100
"processors": _CONFIG.default_processors,
@@ -114,12 +114,18 @@ def get_logger(*args: Any, **initial_values: Any) -> Any:
114114
>>> log.info("hello", x=42)
115115
y=23 x=42 event='hello'
116116
117-
:param args: *Optional* positional arguments that are passed unmodified to
118-
the logger factory. Therefore it depends on the factory what they
119-
mean.
120-
:param initial_values: Values that are used to pre-populate your contexts.
117+
Arguments:
121118
122-
:returns: A proxy that creates a correctly configured bound logger when
119+
args:
120+
*Optional* positional arguments that are passed unmodified to the
121+
logger factory. Therefore it depends on the factory what they
122+
mean.
123+
124+
initial_values: Values that are used to pre-populate your contexts.
125+
126+
Returns:
127+
128+
A proxy that creates a correctly configured bound logger when
123129
necessary. The type of that bound logger depends on your configuration
124130
and is `structlog.BoundLogger` by default.
125131
@@ -128,8 +134,7 @@ def get_logger(*args: Any, **initial_values: Any) -> Any:
128134
If you prefer CamelCase, there's an alias for your reading pleasure:
129135
`structlog.getLogger`.
130136
131-
.. versionadded:: 0.4.0
132-
*args*
137+
.. versionadded:: 0.4.0 *args*
133138
"""
134139
return wrap_logger(None, logger_factory_args=args, **initial_values)
135140

@@ -158,23 +163,28 @@ def wrap_logger(
158163
Default values for *processors*, *wrapper_class*, and *context_class* can
159164
be set using `configure`.
160165
161-
If you set an attribute here, `configure` calls have *no* effect for
162-
the *respective* attribute.
166+
If you set an attribute here, `configure` calls have *no* effect for the
167+
*respective* attribute.
163168
164169
In other words: selective overwriting of the defaults while keeping some
165170
*is* possible.
166171
167-
:param initial_values: Values that are used to pre-populate your contexts.
168-
:param logger_factory_args: Values that are passed unmodified as
169-
``*logger_factory_args`` to the logger factory if not `None`.
172+
Arguments:
173+
174+
initial_values: Values that are used to pre-populate your contexts.
170175
171-
:returns: A proxy that creates a correctly configured bound logger when
176+
logger_factory_args:
177+
Values that are passed unmodified as ``*logger_factory_args`` to
178+
the logger factory if not `None`.
179+
180+
Returns:
181+
182+
A proxy that creates a correctly configured bound logger when
172183
necessary.
173184
174185
See `configure` for the meaning of the rest of the arguments.
175186
176-
.. versionadded:: 0.4.0
177-
*logger_factory_args*
187+
.. versionadded:: 0.4.0 *logger_factory_args*
178188
"""
179189
return BoundLoggerLazyProxy(
180190
logger,
@@ -207,22 +217,29 @@ def configure(
207217
208218
Use `reset_defaults` to undo your changes.
209219
210-
:param processors: The processor chain. See :doc:`processors` for details.
211-
:param wrapper_class: Class to use for wrapping loggers instead of
212-
`structlog.BoundLogger`. See `standard-library`, :doc:`twisted`, and
213-
`custom-wrappers`.
214-
:param context_class: Class to be used for internal context keeping. The
215-
default is a `dict` and since dictionaries are ordered as of Python
216-
3.6, there's few reasons to change this option.
217-
:param logger_factory: Factory to be called to create a new logger that
218-
shall be wrapped.
219-
:param cache_logger_on_first_use: `wrap_logger` doesn't return an actual
220-
wrapped logger but a proxy that assembles one when it's first used. If
221-
this option is set to `True`, this assembled logger is cached. See
222-
`performance`.
223-
224-
.. versionadded:: 0.3.0
225-
*cache_logger_on_first_use*
220+
Arguments:
221+
222+
processors: The processor chain. See :doc:`processors` for details.
223+
224+
wrapper_class:
225+
Class to use for wrapping loggers instead of
226+
`structlog.BoundLogger`. See `standard-library`, :doc:`twisted`,
227+
and `custom-wrappers`.
228+
229+
context_class:
230+
Class to be used for internal context keeping. The default is a
231+
`dict` and since dictionaries are ordered as of Python 3.6, there's
232+
few reasons to change this option.
233+
234+
logger_factory:
235+
Factory to be called to create a new logger that shall be wrapped.
236+
237+
cache_logger_on_first_use:
238+
`wrap_logger` doesn't return an actual wrapped logger but a proxy
239+
that assembles one when it's first used. If this option is set to
240+
`True`, this assembled logger is cached. See `performance`.
241+
242+
.. versionadded:: 0.3.0 *cache_logger_on_first_use*
226243
"""
227244
_CONFIG.is_configured = True
228245

@@ -251,7 +268,9 @@ def configure_once(
251268
It does *not* matter whether it was configured using `configure` or
252269
`configure_once` before.
253270
254-
Raises a `RuntimeWarning` if repeated configuration is attempted.
271+
Raises:
272+
273+
RuntimeWarning: if repeated configuration is attempted.
255274
"""
256275
if not _CONFIG.is_configured:
257276
configure(
@@ -290,11 +309,10 @@ class BoundLoggerLazyProxy:
290309
The only points where a bound logger changes state are ``bind()``,
291310
``unbind()``, and ``new()`` and that return the actual ``BoundLogger``.
292311
293-
If and only if configuration says so, that actual bound logger is
294-
cached on first usage.
312+
If and only if configuration says so, that actual bound logger is cached on
313+
first usage.
295314
296-
.. versionchanged:: 0.4.0
297-
Added support for *logger_factory_args*.
315+
.. versionchanged:: 0.4.0 Added support for *logger_factory_args*.
298316
"""
299317

300318
def __init__(

src/structlog/_frames.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ def _find_first_app_frame_and_name(
4040
"""
4141
Remove all intra-structlog calls and return the relevant app frame.
4242
43-
:param additional_ignores: Additional names with which the first frame must
44-
not start.
43+
Arguments:
4544
46-
:returns: tuple of (frame, name)
45+
additional_ignores:
46+
Additional names with which the first frame must not start.
47+
48+
Returns:
49+
50+
tuple of (frame, name)
4751
"""
4852
ignores = ["structlog"] + (additional_ignores or [])
4953
f = sys._getframe()

src/structlog/_log_levels.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,14 @@ def make_filtering_bound_logger(min_level: int) -> type[FilteringBoundLogger]:
128128
- You *can* have (much) more fine-grained filtering by :ref:`writing a
129129
simple processor <finer-filtering>`.
130130
131-
:param min_level: The log level as an integer. You can use the constants
132-
from `logging` like ``logging.INFO`` or pass the values directly. See
133-
`this table from the logging docs
134-
<https://docs.python.org/3/library/logging.html#levels>`_ for possible
135-
values.
131+
Arguments:
132+
133+
min_level:
134+
The log level as an integer. You can use the constants from
135+
`logging` like ``logging.INFO`` or pass the values directly. See
136+
`this table from the logging docs
137+
<https://docs.python.org/3/library/logging.html#levels>`_ for
138+
possible values.
136139
137140
.. versionadded:: 20.2.0
138141
.. versionchanged:: 21.1.0 The returned loggers are now pickleable.

0 commit comments

Comments
 (0)