Coverage for pydantic/alias_generators.py: 100.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2024-06-21 17:00 +0000

1"""Alias generators for converting between different capitalization conventions.""" 

2 

3import re 1abcdefghijklmnopqrstuvwxyzABCDEF

4 

5__all__ = ('to_pascal', 'to_camel', 'to_snake') 1abcdefghijklmnopqrstuvwxyzABCDEF

6 

7# TODO: in V3, change the argument names to be more descriptive 

8# Generally, don't only convert from snake_case, or name the functions 

9# more specifically like snake_to_camel. 

10 

11 

12def to_pascal(snake: str) -> str: 1abcdefghijklmnopqrstuvwxyzABCDEF

13 """Convert a snake_case string to PascalCase. 

14 

15 Args: 

16 snake: The string to convert. 

17 

18 Returns: 

19 The PascalCase string. 

20 """ 

21 camel = snake.title() 1abcdefghijklmnopqrstuvwxyzABCDEF

22 return re.sub('([0-9A-Za-z])_(?=[0-9A-Z])', lambda m: m.group(1), camel) 1abcdefghijklmnopqrstuvwxyzABCDEF

23 

24 

25def to_camel(snake: str) -> str: 1abcdefghijklmnopqrstuvwxyzABCDEF

26 """Convert a snake_case string to camelCase. 

27 

28 Args: 

29 snake: The string to convert. 

30 

31 Returns: 

32 The converted camelCase string. 

33 """ 

34 # If the string is already in camelCase and does not contain a digit followed 

35 # by a lowercase letter, return it as it is 

36 if re.match('^[a-z]+[A-Za-z0-9]*$', snake) and not re.search(r'\d[a-z]', snake): 1abcdefghijklmnopqrstuvwxyzABCDEF

37 return snake 1abcdefghijklmnopqrstuvwxyzABCDEF

38 

39 camel = to_pascal(snake) 1abcdefghijklmnopqrstuvwxyzABCDEF

40 return re.sub('(^_*[A-Z])', lambda m: m.group(1).lower(), camel) 1abcdefghijklmnopqrstuvwxyzABCDEF

41 

42 

43def to_snake(camel: str) -> str: 1abcdefghijklmnopqrstuvwxyzABCDEF

44 """Convert a PascalCase, camelCase, or kebab-case string to snake_case. 

45 

46 Args: 

47 camel: The string to convert. 

48 

49 Returns: 

50 The converted string in snake_case. 

51 """ 

52 snake = re.sub(r'([a-zA-Z])([0-9])', lambda m: f'{m.group(1)}_{m.group(2)}', camel) 1abcdefghijklmnopqrstuvwxyzABCDEF

53 snake = re.sub(r'([a-z0-9])([A-Z])', lambda m: f'{m.group(1)}_{m.group(2)}', snake) 1abcdefghijklmnopqrstuvwxyzABCDEF

54 snake = re.sub(r'([A-Z]+)([A-Z][a-z])', lambda m: f'{m.group(1)}_{m.group(2)}', snake) 1abcdefghijklmnopqrstuvwxyzABCDEF

55 # Replace hyphens with underscores to handle kebab-case 

56 snake = snake.replace('-', '_') 1abcdefghijklmnopqrstuvwxyzABCDEF

57 return snake.lower() 1abcdefghijklmnopqrstuvwxyzABCDEF