Coverage for src / template_python / settings.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-14 20:19 +0000

1""" 

2Settings configuration for the template-python application. 

3 

4This module provides the primary configuration structure for the application 

5using Pydantic Settings. It aggregates configuration from multiple sources, 

6including environment variables and CLI arguments, and exposes a unified interface 

7for safe, typed configuration access across the codebase. 

8""" 

9 

10from __future__ import annotations 

11 

12from pathlib import Path 

13from typing import ClassVar, Literal 

14 

15from pydantic import Field 

16from pydantic_settings import ( 

17 BaseSettings, 

18 SettingsConfigDict, 

19) 

20 

21__all__ = ["Settings"] 

22 

23 

24class Settings(BaseSettings): 

25 """ 

26 Configuration state for the application. 

27 

28 This class aggregates and prioritizes configuration from multiple sources, 

29 providing a unified state for the application. It is built on top of 

30 pydantic-settings to allow validation, default values, and type coercion. 

31 

32 Example: 

33 .. code-block:: python 

34 

35 from template_python.settings import Settings 

36 

37 settings = Settings(environment="production") 

38 print(settings.project_root) 

39 """ 

40 

41 model_config: ClassVar[SettingsConfigDict] = SettingsConfigDict( 

42 arbitrary_types_allowed=True, 

43 extra="ignore", 

44 populate_by_name=True, 

45 validate_assignment=True, 

46 env_prefix="TEMPLATE_PYTHON__", 

47 ) 

48 """Pydantic config dict dictating environment prefixes and validation.""" 

49 

50 # Core Application Properties 

51 project_root: Path = Field( 

52 default_factory=Path.cwd, 

53 description=( 

54 "The root directory of the project. Used for resolving relative paths." 

55 ), 

56 ) 

57 environment: Literal["development", "staging", "production"] = Field( 

58 default="development", 

59 description="The current deployment environment of the application.", 

60 ) 

61 

62 def __str__(self) -> str: 

63 """ 

64 Return a concise string representation of the settings. 

65 

66 :return: A concise, human-readable string summary of the settings. 

67 :rtype: str 

68 """ 

69 return ( 

70 f"Settings(environment={self.environment!r}, " 

71 f"project_root={self.project_root!r})" 

72 ) 

73 

74 def __repr__(self) -> str: 

75 """ 

76 Return a detailed string representation of the settings. 

77 

78 :return: A detailed string representation suitable for debugging. 

79 :rtype: str 

80 """ 

81 return ( 

82 f"Settings(" 

83 f"environment={self.environment!r}, " 

84 f"project_root={self.project_root!r}" 

85 f")" 

86 )