|
1 | | -# -*- coding: utf-8 -*- |
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2021- Spyder Project Contributors |
2 | 3 | # |
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 | +# ----------------------------------------------------------------------------- |
6 | 7 |
|
7 | 8 | """ |
8 | 9 | Spyder API helper decorators. |
9 | 10 | """ |
10 | 11 |
|
| 12 | +from __future__ import annotations |
| 13 | + |
11 | 14 | # Standard library imports |
12 | 15 | 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 |
14 | 24 |
|
15 | 25 | # Local imports |
16 | 26 | from spyder.config.types import ConfigurationKey |
17 | 27 |
|
18 | 28 |
|
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 | +""" |
21 | 43 |
|
22 | 44 |
|
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: |
26 | 50 | """ |
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. |
29 | 52 |
|
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. |
34 | 68 |
|
35 | 69 | Parameters |
36 | 70 | ---------- |
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. |
47 | 82 |
|
48 | 83 | Returns |
49 | 84 | ------- |
50 | 85 | func: Callable |
51 | | - The same method that was given as input. |
| 86 | + The method passed as ``func`` with the config listener set up. |
52 | 87 | """ |
53 | 88 | if func is None: |
54 | 89 | return functools.partial( |
55 | | - on_conf_change, section=section, option=option) |
| 90 | + on_conf_change, section=section, option=option |
| 91 | + ) |
56 | 92 |
|
57 | 93 | if option is None: |
58 | 94 | # Use special __section identifier to signal that the function |
59 | 95 | # observes any change on the section options. |
60 | | - option = '__section' |
| 96 | + option = "__section" |
61 | 97 |
|
62 | 98 | info = [] |
63 | 99 | if isinstance(option, list): |
|
0 commit comments