Skip to content

Commit 31da5c3

Browse files
authored
Merge pull request #25451 from CAM-Gerlach/spyder-api-revise-config-docstrings-master
PR: Add & revise docstrings in the `spyder.api.config` module
2 parents b82a3c5 + 31797ae commit 31da5c3

File tree

2 files changed

+273
-162
lines changed

2 files changed

+273
-162
lines changed

spyder/api/config/decorators.py

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,99 @@
1-
# -*- coding: utf-8 -*-
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2021- Spyder Project Contributors
23
#
3-
# Copyright © Spyder Project Contributors
4-
# Licensed under the terms of the MIT License
5-
# (see spyder/__init__.py for details)
4+
# Released under the terms of the MIT License
5+
# (see LICENSE.txt in the project root directory for details)
6+
# -----------------------------------------------------------------------------
67

78
"""
89
Spyder API helper decorators.
910
"""
1011

12+
from __future__ import annotations
13+
1114
# Standard library imports
1215
import functools
13-
from typing import Callable, Optional, Union, List
16+
import sys
17+
from collections.abc import Callable
18+
from typing import Union
19+
20+
if sys.version_info < (3, 10):
21+
from typing_extensions import TypeAlias
22+
else:
23+
from typing import TypeAlias # noqa: ICN003
1424

1525
# Local imports
1626
from spyder.config.types import ConfigurationKey
1727

1828

19-
ConfigurationKeyList = List[ConfigurationKey]
20-
ConfigurationKeyOrList = Union[ConfigurationKeyList, ConfigurationKey]
29+
ConfigurationKeyList: TypeAlias = list[ConfigurationKey]
30+
"""Type alias for a list of string/string tuple keys of Spyder config options.
31+
32+
A :class:`list` of :class:`spyder.config.types.ConfigurationKey`\\s.
33+
"""
34+
35+
ConfigurationKeyOrList: TypeAlias = Union[
36+
ConfigurationKeyList, ConfigurationKey
37+
]
38+
"""Type alias for either a list of config keys or a single key.
39+
40+
Union of types :class:`ConfigurationKeyList` and
41+
:class:`spyder.config.types.ConfigurationKey`.
42+
"""
2143

2244

23-
def on_conf_change(func: Callable = None,
24-
section: Optional[str] = None,
25-
option: Optional[ConfigurationKeyOrList] = None) -> Callable:
45+
def on_conf_change(
46+
func: Callable | None = None,
47+
section: str | None = None,
48+
option: ConfigurationKeyOrList | None = None,
49+
) -> Callable:
2650
"""
27-
Method decorator used to handle changes on the configuration option
28-
`option` of the section `section`.
51+
Decorator to handle changing a config option in a given section.
2952
30-
The methods that use this decorator must have the following signature
31-
`def method(self, value)` when observing a single value or the whole
32-
section and `def method(self, option, value): ...` when observing
33-
multiple values.
53+
The methods that use this decorator must have the signature
54+
55+
.. code-block:: python
56+
57+
def method(self, value: Any):
58+
...
59+
60+
when observing a single value or the whole section, and
61+
62+
.. code-block:: python
63+
64+
def method(self, option: ConfigurationKeyOrList | None, value: Any):
65+
...
66+
67+
when observing multiple values.
3468
3569
Parameters
3670
----------
37-
func: Callable
38-
Method to decorate. Given by default when applying the decorator.
39-
section: Optional[str]
40-
Name of the configuration whose option is going to be observed for
41-
changes. If None, then the `CONF_SECTION` attribute of the class
42-
where the method is defined is used.
43-
option: Optional[ConfigurationKeyOrList]
44-
Name/tuple of the option to observe or a list of name/tuples if the
45-
method expects updates from multiple keys. If None, then all changes
46-
on the specified section are observed.
71+
func: Callable | None, optional
72+
Method to decorate, passed automatically when applying the decorator.
73+
section: str | None, optional
74+
Name of the configuration section to observe for changes.
75+
If ``None``, then the ``CONF_SECTION`` attribute of the class
76+
where the method defined is used.
77+
option: ConfigurationKeyOrList | None, optional
78+
Name (:class:`str` / :class:`tuple` of :class:`str`) of the option
79+
to observe, or a list of names if the method expects updates from
80+
multiple keys. If ``None``, then all changes to options in the
81+
specified section are observed.
4782
4883
Returns
4984
-------
5085
func: Callable
51-
The same method that was given as input.
86+
The method passed as ``func`` with the config listener set up.
5287
"""
5388
if func is None:
5489
return functools.partial(
55-
on_conf_change, section=section, option=option)
90+
on_conf_change, section=section, option=option
91+
)
5692

5793
if option is None:
5894
# Use special __section identifier to signal that the function
5995
# observes any change on the section options.
60-
option = '__section'
96+
option = "__section"
6197

6298
info = []
6399
if isinstance(option, list):

0 commit comments

Comments
 (0)