Coverage for src/pylint_sort_functions/utils/__init__.py: 100%

1"""Utility functions for AST analysis and sorting logic.

2

3 This module provides the core analysis functions for the pylint-sort-functions plugin.

4 Refactored from a single 1117-line file into focused modules for better maintainability.

5

6 For backward compatibility, all public functions are re-exported from their

7 specialized modules. All existing imports continue to work unchanged.

8

9 For detailed information about the sorting algorithm and rules, see the documentation

10 at docs/sorting.rst which explains the complete sorting methodology, special method

11 handling, privacy detection, and configuration options.

12

13 Function Privacy Detection:

14 The plugin uses import analysis to identify functions that should be private by

15 scanning actual usage patterns across the project:

16- Analyzes cross-module imports and function calls in all Python files

17- Identifies functions that are only used within their own module

18- Skips common public API patterns (main, run, setup, etc.)

19- Provides accurate detection based on real usage patterns

20"""

21

22# Re-export all public functions for backward compatibility

23# All existing imports continue to work:

24# from pylint_sort_functions.utils import CategoryConfig ✓

25# from pylint_sort_functions import utils; utils.get_functions_from_node() ✓

26

27# AST Analysis functions

28# AST Analysis functions

29 from .ast_analysis import (

30 get_functions_from_node,

31 get_methods_from_class,

32 is_dunder_method,

33 is_private_function,

34)

35

36# Categorization system

37 from .categorization import (

38 CategoryConfig,

39 MethodCategory,

40 _get_category_match_priority,

41 _method_name_matches_pattern,

42 categorize_method,

43 find_method_section_boundaries,

44 get_expected_section_for_method,

45 is_method_in_correct_section,

46 parse_section_headers,

47)

48

49# Decorator analysis

50 from .decorators import (

51 _decorator_node_to_string,

52 decorator_matches_pattern,

53 function_has_excluded_decorator,

54 get_decorator_strings,

55)

56

57# File pattern utilities

58 from .file_patterns import (

59 _matches_file_pattern,

60 find_python_files,

61 is_unittest_file,

62)

63

64# Privacy analysis

65 from .privacy import (

66 _build_cross_module_usage_graph,

67 _extract_attribute_accesses,

68 _extract_imports_from_file,

69 _is_function_used_externally,

70 should_function_be_private,

71 should_function_be_public,

72)

73

74# Sorting validation logic

75 from .sorting import (

76 _are_categories_properly_ordered,

77 _are_functions_sorted,

78 _are_methods_sorted,

79 _get_function_categories,

80 _get_function_groups,

81 are_functions_properly_separated,

82 are_functions_sorted_with_exclusions,

83 are_methods_in_correct_sections,

84 are_methods_sorted_with_exclusions,

85 find_empty_section_headers,

86 find_missing_section_headers,

87 get_section_violations,

88)

89

90 __all__ = [

91 # Public API - Core functionality

92 "CategoryConfig",

93 "MethodCategory",

94 "are_functions_properly_separated",

95 "are_functions_sorted_with_exclusions",

96 "are_methods_in_correct_sections",

97 "are_methods_sorted_with_exclusions",

98 "categorize_method",

99 "decorator_matches_pattern",

100 "find_empty_section_headers",

101 "find_method_section_boundaries",

102 "find_missing_section_headers",

103 "find_python_files",

104 "function_has_excluded_decorator",

105 "get_decorator_strings",

106 "get_expected_section_for_method",

107 "get_functions_from_node",

108 "get_methods_from_class",

109 "get_section_violations",

110 "is_dunder_method",

111 "is_method_in_correct_section",

112 "is_private_function",

113 "is_unittest_file",

114 "parse_section_headers",

115 "should_function_be_private",

116 "should_function_be_public",

117 # Private functions that tests depend on

118 "_are_categories_properly_ordered",

119 "_are_functions_sorted",

120 "_are_methods_sorted",

121 "_build_cross_module_usage_graph",

122 "_decorator_node_to_string",

123 "_extract_attribute_accesses",

124 "_extract_imports_from_file",

125 "_get_category_match_priority",

126 "_get_function_categories",

127 "_get_function_groups",

128 "_is_function_used_externally",

129 "_matches_file_pattern",

130 "_method_name_matches_pattern",

131]