Coverage for src/backend/InvenTree/common/setting/type.py: 67%

10 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Types for settings.""" 

2 

3import sys 

4from collections.abc import Callable 

5from typing import Any, TypedDict 

6 

7if sys.version_info >= (3, 11): 7 ↛ 11line 7 didn't jump to line 11 because the condition on line 7 was always true

8 from typing import NotRequired # pragma: no cover 

9else: 

10 

11 class NotRequired: # pragma: no cover 

12 """NotRequired type helper is only supported with Python 3.11+.""" 

13 

14 def __class_getitem__(cls, item): 

15 """Return the item.""" 

16 return item 

17 

18 

19class SettingsKeyType(TypedDict, total=False): 

20 """Type definitions for a SettingsKeyType. 

21 

22 Attributes: 

23 name: Translatable string name of the setting (required) 

24 description: Translatable string description of the setting (required) 

25 units: Units of the particular setting (optional) 

26 validator: Validation function/list of functions for the setting (optional, default: None, e.g: bool, int, str, MinValueValidator, ...) 

27 default: Default value or function that returns default value (optional) 

28 choices: Function that returns or value of list[tuple[str: key, str: display value]] (optional) 

29 model_filters: Filters to apply when querying the associated model (optional) 

30 hidden: Hide this setting from settings page (optional) 

31 before_save: Function that gets called after save with *args, **kwargs (optional) 

32 after_save: Function that gets called after save with *args, **kwargs (optional) 

33 protected: Protected values are not returned to the client, instead "***" is returned (optional, default: False) 

34 required: Is this setting required to work, can be used in combination with .check_all_settings(...) (optional, default: False) 

35 model: Auto create a dropdown menu to select an associated model instance (e.g. 'company.company', 'auth.user' and 'auth.group' are possible too, optional) 

36 confirm: Require an explicit confirmation before changing the setting (optional, default: False) 

37 confirm_text: Text to display in the confirmation dialog (optional) 

38 """ 

39 

40 name: str 

41 description: str 

42 units: str 

43 validator: Callable | list[Callable] | tuple[Callable] 

44 default: Callable | Any 

45 choices: list[tuple[str, str]] | Callable[[], list[tuple[str, str]]] 

46 model_filters: dict[str, Any] 

47 hidden: bool 

48 before_save: Callable[..., None] 

49 after_save: Callable[..., None] 

50 protected: bool 

51 required: bool 

52 model: str 

53 confirm: bool 

54 confirm_text: str 

55 

56 

57class InvenTreeSettingsKeyType(SettingsKeyType): 

58 """InvenTreeSettingsKeyType has additional properties only global settings support. 

59 

60 Attributes: 

61 requires_restart: If True, a server restart is required after changing the setting 

62 """ 

63 

64 requires_restart: NotRequired[bool]