Coverage for tdom/callables_test.py: 100%

82 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-17 19:54 +0000

1import typing as t 

2 

3from .callables import get_callable_info 

4 

5 

6def callable_zero_args() -> None: # pragma: no cover 

7 """Test callable that takes zero arguments.""" 

8 pass 

9 

10 

11def test_zero_args() -> None: 

12 """Test that a callable that takes zero arguments is detected.""" 

13 info = get_callable_info(callable_zero_args) 

14 assert info.id == id(callable_zero_args) 

15 assert info.named_params == frozenset() 

16 assert info.required_named_params == frozenset() 

17 assert not info.requires_positional 

18 assert not info.kwargs 

19 assert info.supports_zero_args 

20 

21 

22def callable_positional(a: int, b: str) -> None: # pragma: no cover 

23 """Test callable that takes positional arguments.""" 

24 pass 

25 

26 

27def test_positional() -> None: 

28 """Test that a callable that takes positional arguments is detected.""" 

29 info = get_callable_info(callable_positional) 

30 assert info.id == id(callable_positional) 

31 assert info.named_params == frozenset(["a", "b"]) 

32 assert info.required_named_params == frozenset(["a", "b"]) 

33 assert not info.requires_positional 

34 assert not info.kwargs 

35 assert not info.supports_zero_args 

36 

37 

38def callable_positional_only(a: int, /, b: str) -> None: # pragma: no cover 

39 """Test callable that takes positional-only arguments.""" 

40 pass 

41 

42 

43def test_positional_only() -> None: 

44 """Test that a callable that takes positional-only arguments is detected.""" 

45 info = get_callable_info(callable_positional_only) 

46 assert info.id == id(callable_positional_only) 

47 assert info.named_params == frozenset(["b"]) 

48 assert info.required_named_params == frozenset(["b"]) 

49 assert info.requires_positional 

50 assert not info.kwargs 

51 assert not info.supports_zero_args 

52 

53 

54def callable_positional_only_default( 

55 a: int = 1, /, b: str = "b" 

56) -> None: # pragma: no cover 

57 """Test callable that takes positional-only arguments with defaults.""" 

58 pass 

59 

60 

61def test_positional_only_default() -> None: 

62 """Test that a callable that takes positional-only arguments with defaults is detected.""" 

63 info = get_callable_info(callable_positional_only_default) 

64 assert info.id == id(callable_positional_only_default) 

65 assert info.named_params == frozenset(["b"]) 

66 assert info.required_named_params == frozenset() 

67 assert not info.requires_positional 

68 assert not info.kwargs 

69 assert info.supports_zero_args 

70 

71 

72def callable_kwargs(**kwargs: t.Any) -> None: # pragma: no cover 

73 """Test callable that takes **kwargs.""" 

74 pass 

75 

76 

77def test_kwargs() -> None: 

78 """Test that a callable that takes **kwargs is detected.""" 

79 info = get_callable_info(callable_kwargs) 

80 assert info.id == id(callable_kwargs) 

81 assert info.named_params == frozenset() 

82 assert info.required_named_params == frozenset() 

83 assert not info.requires_positional 

84 assert info.kwargs 

85 assert info.supports_zero_args 

86 

87 

88def callable_mixed( 

89 a: int, /, b: str, *, c: float = 1.0, **kwargs: t.Any 

90) -> None: # pragma: no cover 

91 """Test callable that takes a mix of argument types.""" 

92 pass 

93 

94 

95def test_mixed() -> None: 

96 """Test that a callable that takes a mix of argument types is detected.""" 

97 info = get_callable_info(callable_mixed) 

98 assert info.id == id(callable_mixed) 

99 assert info.named_params == frozenset(["b", "c"]) 

100 assert info.required_named_params == frozenset(["b"]) 

101 assert info.requires_positional 

102 assert info.kwargs 

103 assert not info.supports_zero_args 

104 

105 

106def callable_positional_with_defaults( 

107 a: int = 1, b: str = "b", / 

108) -> None: # pragma: no cover 

109 """Test callable that takes positional arguments with defaults.""" 

110 pass 

111 

112 

113def test_positional_with_defaults() -> None: 

114 """Test that a callable that takes positional arguments with defaults is detected.""" 

115 info = get_callable_info(callable_positional_with_defaults) 

116 assert info.id == id(callable_positional_with_defaults) 

117 assert info.named_params == frozenset() 

118 assert info.required_named_params == frozenset() 

119 assert not info.requires_positional 

120 assert not info.kwargs 

121 assert info.supports_zero_args 

122 

123 

124def callable_keyword_only(*, a: int, b: str = "b") -> None: # pragma: no cover 

125 """Test callable that takes keyword-only arguments.""" 

126 pass 

127 

128 

129def test_keyword_only() -> None: 

130 """Test that a callable that takes keyword-only arguments is detected.""" 

131 info = get_callable_info(callable_keyword_only) 

132 assert info.id == id(callable_keyword_only) 

133 assert info.named_params == frozenset(["a", "b"]) 

134 assert info.required_named_params == frozenset(["a"]) 

135 assert not info.requires_positional 

136 assert not info.kwargs 

137 assert not info.supports_zero_args 

138 

139 

140def callable_var_positional(*args: t.Any) -> None: # pragma: no cover 

141 """Test callable that takes *args.""" 

142 pass 

143 

144 

145def test_var_positional() -> None: 

146 """Test that a callable that takes *args is detected.""" 

147 info = get_callable_info(callable_var_positional) 

148 assert info.id == id(callable_var_positional) 

149 assert info.named_params == frozenset() 

150 assert info.required_named_params == frozenset() 

151 assert info.requires_positional 

152 assert not info.kwargs 

153 assert not info.supports_zero_args 

154 

155 

156def callable_all_types( 

157 a: int, /, b: str, *, c: float = 1.0, **kwargs: t.Any 

158) -> None: # pragma: no cover 

159 """Test callable that takes all types of arguments.""" 

160 pass 

161 

162 

163def test_all_types() -> None: 

164 """Test that a callable that takes all types of arguments is detected.""" 

165 info = get_callable_info(callable_all_types) 

166 assert info.id == id(callable_all_types) 

167 assert info.named_params == frozenset(["b", "c"]) 

168 assert info.required_named_params == frozenset(["b"]) 

169 assert info.requires_positional 

170 assert info.kwargs 

171 assert not info.supports_zero_args