@@ -40,79 +40,73 @@ predicate is_ternary_op(string name) {
40
40
41
41
predicate is_quad_op ( string name ) { name = "__setslice__" or name = "__exit__" }
42
42
43
- int argument_count ( PythonFunctionValue f , string name , ClassValue cls ) {
44
- cls .declaredAttribute ( name ) = f and
45
- (
46
- is_unary_op ( name ) and result = 1
47
- or
48
- is_binary_op ( name ) and result = 2
49
- or
50
- is_ternary_op ( name ) and result = 3
51
- or
52
- is_quad_op ( name ) and result = 4
53
- )
43
+ int argument_count ( string name ) {
44
+ is_unary_op ( name ) and result = 1
45
+ or
46
+ is_binary_op ( name ) and result = 2
47
+ or
48
+ is_ternary_op ( name ) and result = 3
49
+ or
50
+ is_quad_op ( name ) and result = 4
54
51
}
55
52
56
53
predicate incorrect_special_method_defn (
57
- PythonFunctionValue func , string message , boolean show_counts , string name , ClassValue owner
54
+ Function func , string message , boolean show_counts , string name
58
55
) {
59
- exists ( int required | required = argument_count ( func , name , owner ) |
56
+ exists ( int required | required = argument_count ( name ) |
60
57
/* actual_non_default <= actual */
61
- if required > func .maxParameters ( )
58
+ if required > func .getMaxPositionalArguments ( )
62
59
then message = "Too few parameters" and show_counts = true
63
60
else
64
- if required < func .minParameters ( )
61
+ if required < func .getMinPositionalArguments ( )
65
62
then message = "Too many parameters" and show_counts = true
66
63
else (
67
- func .minParameters ( ) < required and
68
- not func .getScope ( ) .hasVarArg ( ) and
69
- message = ( required - func .minParameters ( ) ) + " default values(s) will never be used" and
64
+ func .getMinPositionalArguments ( ) < required and
65
+ not func .hasVarArg ( ) and
66
+ message =
67
+ ( required - func .getMinPositionalArguments ( ) ) + " default values(s) will never be used" and
70
68
show_counts = false
71
69
)
72
70
)
73
71
}
74
72
75
- predicate incorrect_pow ( FunctionValue func , string message , boolean show_counts , ClassValue owner ) {
76
- owner .declaredAttribute ( "__pow__" ) = func and
73
+ predicate incorrect_pow ( Function func , string message , boolean show_counts ) {
77
74
(
78
- func .maxParameters ( ) < 2 and message = "Too few parameters" and show_counts = true
75
+ func .getMaxPositionalArguments ( ) < 2 and message = "Too few parameters" and show_counts = true
79
76
or
80
- func .minParameters ( ) > 3 and message = "Too many parameters" and show_counts = true
77
+ func .getMinPositionalArguments ( ) > 3 and message = "Too many parameters" and show_counts = true
81
78
or
82
- func .minParameters ( ) < 2 and
83
- message = ( 2 - func .minParameters ( ) ) + " default value(s) will never be used" and
79
+ func .getMinPositionalArguments ( ) < 2 and
80
+ message = ( 2 - func .getMinPositionalArguments ( ) ) + " default value(s) will never be used" and
84
81
show_counts = false
85
82
or
86
- func .minParameters ( ) = 3 and
83
+ func .getMinPositionalArguments ( ) = 3 and
87
84
message = "Third parameter to __pow__ should have a default value" and
88
85
show_counts = false
89
86
)
90
87
}
91
88
92
- predicate incorrect_get ( FunctionValue func , string message , boolean show_counts , ClassValue owner ) {
93
- owner .declaredAttribute ( "__get__" ) = func and
89
+ predicate incorrect_get ( Function func , string message , boolean show_counts ) {
94
90
(
95
- func .maxParameters ( ) < 3 and message = "Too few parameters" and show_counts = true
91
+ func .getMaxPositionalArguments ( ) < 3 and message = "Too few parameters" and show_counts = true
96
92
or
97
- func .minParameters ( ) > 3 and message = "Too many parameters" and show_counts = true
93
+ func .getMinPositionalArguments ( ) > 3 and message = "Too many parameters" and show_counts = true
98
94
or
99
- func .minParameters ( ) < 2 and
100
- not func .getScope ( ) . hasVarArg ( ) and
101
- message = ( 2 - func .minParameters ( ) ) + " default value(s) will never be used" and
95
+ func .getMinPositionalArguments ( ) < 2 and
96
+ not func .hasVarArg ( ) and
97
+ message = ( 2 - func .getMinPositionalArguments ( ) ) + " default value(s) will never be used" and
102
98
show_counts = false
103
99
)
104
100
}
105
101
106
- string should_have_parameters ( PythonFunctionValue f , string name , ClassValue owner ) {
107
- exists ( int i | i = argument_count ( f , name , owner ) | result = i .toString ( ) )
108
- or
109
- owner .declaredAttribute ( name ) = f and
110
- ( name = "__get__" or name = "__pow__" ) and
111
- result = "2 or 3"
102
+ string should_have_parameters ( string name ) {
103
+ if name in [ "__pow__" , "__get__" ]
104
+ then result = "2 or 3"
105
+ else result = argument_count ( name ) .toString ( )
112
106
}
113
107
114
- string has_parameters ( PythonFunctionValue f ) {
115
- exists ( int i | i = f .minParameters ( ) |
108
+ string has_parameters ( Function f ) {
109
+ exists ( int i | i = f .getMinPositionalArguments ( ) |
116
110
i = 0 and result = "no parameters"
117
111
or
118
112
i = 1 and result = "1 parameter"
@@ -125,19 +119,20 @@ from
125
119
PythonFunctionValue f , string message , string sizes , boolean show_counts , string name ,
126
120
ClassValue owner
127
121
where
122
+ owner .declaredAttribute ( name ) = f and
128
123
(
129
- incorrect_special_method_defn ( f , message , show_counts , name , owner )
124
+ incorrect_special_method_defn ( f . getScope ( ) , message , show_counts , name )
130
125
or
131
- incorrect_pow ( f , message , show_counts , owner ) and name = "__pow__"
126
+ incorrect_pow ( f . getScope ( ) , message , show_counts ) and name = "__pow__"
132
127
or
133
- incorrect_get ( f , message , show_counts , owner ) and name = "__get__"
128
+ incorrect_get ( f . getScope ( ) , message , show_counts ) and name = "__get__"
134
129
) and
135
130
(
136
131
show_counts = false and sizes = ""
137
132
or
138
133
show_counts = true and
139
134
sizes =
140
- ", which has " + has_parameters ( f ) + ", but should have " +
141
- should_have_parameters ( f , name , owner )
135
+ ", which has " + has_parameters ( f . getScope ( ) ) + ", but should have " +
136
+ should_have_parameters ( name )
142
137
)
143
138
select f , message + " for special method " + name + sizes + ", in class $@." , owner , owner .getName ( )
0 commit comments