Coverage for lintro / enums / action.py: 87%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Action/capability enum for tools (check vs. fix).""" 

2 

3from __future__ import annotations 

4 

5from enum import StrEnum 

6 

7 

8class Action(StrEnum): 

9 """Supported actions a tool can perform.""" 

10 

11 CHECK = "check" 

12 FIX = "fix" 

13 TEST = "test" 

14 

15 

16# Mapping of string aliases to Action enum values 

17_ACTION_ALIASES: dict[str, Action] = { 

18 "check": Action.CHECK, 

19 "fix": Action.FIX, 

20 "fmt": Action.FIX, 

21 "format": Action.FIX, 

22 "test": Action.TEST, 

23} 

24 

25 

26def normalize_action(value: str | Action) -> Action: 

27 """Normalize a raw value to an Action enum. 

28 

29 Args: 

30 value: str or Action to normalize. Accepts "check", "fix", "fmt", 

31 "format", or "test" (case-insensitive). 

32 

33 Returns: 

34 Action: Normalized enum value. 

35 

36 Raises: 

37 ValueError: If the value is not a recognized action string. 

38 

39 Examples: 

40 >>> normalize_action("check") 

41 <Action.CHECK: 'check'> 

42 >>> normalize_action("FMT") 

43 <Action.FIX: 'fix'> 

44 >>> normalize_action(Action.TEST) 

45 <Action.TEST: 'test'> 

46 """ 

47 if isinstance(value, Action): 

48 return value 

49 

50 value_lower = value.lower() 

51 if value_lower in _ACTION_ALIASES: 

52 return _ACTION_ALIASES[value_lower] 

53 

54 valid_values = ", ".join(sorted(_ACTION_ALIASES.keys())) 

55 raise ValueError( 

56 f"Unknown action: {value!r}. Valid actions are: {valid_values}", 

57 )