Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pathspec/_backends/_utils.py: 77%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

22 statements  

1""" 

2This module provides private utility functions for backends. 

3 

4WARNING: The *pathspec._backends* package is not part of the public API. Its 

5contents and structure are likely to change. 

6""" 

7 

8from collections.abc import ( 

9 Iterable) 

10from typing import ( 

11 Optional, # Replaced by `X | None` in 3.10. 

12 TypeVar, 

13 Union, # Replaced by `X | Y` in 3.10. 

14 overload) 

15 

16from ..pattern import ( 

17 Pattern) 

18 

19T = TypeVar("T") 

20U = TypeVar("U") 

21TPattern = TypeVar("TPattern", bound=Pattern) 

22 

23 

24def enumerate_patterns( 

25 patterns: Iterable[TPattern], 

26 filter: bool, 

27 reverse: bool, 

28) -> list[tuple[int, TPattern]]: 

29 """ 

30 Enumerate the patterns. 

31 

32 *patterns* (:class:`Iterable` of :class:`.Pattern`) contains the patterns. 

33 

34 *filter* (:class:`bool`) is whether to remove no-op patterns (:data:`True`), 

35 or keep them (:data:`False`). 

36 

37 *reverse* (:class:`bool`) is whether to reverse the pattern order 

38 (:data:`True`), or keep the order (:data:`True`). 

39 

40 Returns the enumerated patterns (:class:`list` of :class:`tuple`). 

41 """ 

42 out_patterns = [ 

43 (__i, __pat) 

44 for __i, __pat in enumerate(patterns) 

45 if not filter or __pat.include is not None 

46 ] 

47 if reverse: 

48 out_patterns.reverse() 

49 

50 return out_patterns 

51 

52 

53@overload 

54def first(iterable: Iterable[T], default: T) -> T: 

55 ... 

56 

57 

58@overload 

59def first(iterable: Iterable[T], default: None) -> Optional[T]: 

60 ... 

61 

62 

63def first(iterable: Iterable[T], default: Optional[T]) -> Optional[T]: 

64 """ 

65 Get the first value of the iterable. 

66 

67 *iterable* (:class:`.Iterable`) is the iterable. 

68 

69 *default* is the default value to return if the iterable is empty. 

70 

71 Returns the first value of the iterable or the default value. 

72 """ 

73 for val in iterable: 

74 return val 

75 

76 return default