Skip to content

Commit 061f318

Browse files
authored
Merge pull request github#3460 from yoff/boolDefault
Python: __bool__ does not raise TypeError by default
2 parents 8a0af0b + 60d5ba2 commit 061f318

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

python/ql/src/Functions/IncorrectRaiseInSpecialMethod.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ private predicate ordering_method(string name) {
8282
private predicate cast_method(string name) {
8383
name = "__nonzero__" and major_version() = 2
8484
or
85-
name = "__bool__"
86-
or
8785
name = "__int__"
8886
or
8987
name = "__float__"
@@ -118,6 +116,8 @@ predicate preferred_raise(string name, ClassObject ex) {
118116
ordering_method(name) and ex = theTypeErrorType()
119117
or
120118
arithmetic_method(name) and ex = Object::builtin("ArithmeticError")
119+
or
120+
name = "__bool__" and ex = theTypeErrorType()
121121
}
122122

123123
predicate no_need_to_raise(string name, string message) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
| protocols.py:98:5:98:33 | Function __getitem__ | Function always raises $@; raise LookupError instead | file://:Compiled Code:0:0:0:0 | builtin-class ZeroDivisionError | builtin-class ZeroDivisionError |
22
| protocols.py:101:5:101:26 | Function __getattr__ | Function always raises $@; raise AttributeError instead | file://:Compiled Code:0:0:0:0 | builtin-class ZeroDivisionError | builtin-class ZeroDivisionError |
3+
| protocols.py:104:5:104:23 | Function __bool__ | Function always raises $@; raise TypeError instead | file://:Compiled Code:0:0:0:0 | builtin-class ZeroDivisionError | builtin-class ZeroDivisionError |

python/ql/test/query-tests/Functions/general/SignatureSpecialMethods.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
| om_test.py:71:5:71:19 | Function WrongSpecials.__repr__ | Too few parameters for special method __repr__, which has no parameters, but should have 1, in class $@. | om_test.py:57:1:57:28 | class WrongSpecials | WrongSpecials |
66
| om_test.py:74:5:74:46 | Function WrongSpecials.__add__ | 1 default values(s) will never be used for special method __add__, in class $@. | om_test.py:57:1:57:28 | class WrongSpecials | WrongSpecials |
77
| om_test.py:97:15:97:34 | Function NotOKSpecials.lambda | Too few parameters for special method __sub__, which has 1 parameter, but should have 2, in class $@. | om_test.py:95:1:95:28 | class NotOKSpecials | NotOKSpecials |
8-
| protocols.py:104:1:104:12 | Function f | Too few parameters for special method __add__, which has 1 parameter, but should have 2, in class $@. | protocols.py:107:1:107:29 | class MissingMethods | MissingMethods |
9-
| protocols.py:104:1:104:12 | Function f | Too few parameters for special method __set__, which has 1 parameter, but should have 3, in class $@. | protocols.py:107:1:107:29 | class MissingMethods | MissingMethods |
8+
| protocols.py:107:1:107:12 | Function f | Too few parameters for special method __add__, which has 1 parameter, but should have 2, in class $@. | protocols.py:110:1:110:29 | class MissingMethods | MissingMethods |
9+
| protocols.py:107:1:107:12 | Function f | Too few parameters for special method __set__, which has 1 parameter, but should have 3, in class $@. | protocols.py:110:1:110:29 | class MissingMethods | MissingMethods |

python/ql/test/query-tests/Functions/general/protocols.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def __getitem__(self, index):
101101
def __getattr__(self):
102102
raise ZeroDivisionError()
103103

104+
def __bool__(self):
105+
raise ZeroDivisionError()
106+
104107
def f(self):
105108
pass
106109

@@ -116,3 +119,8 @@ class OK(object):
116119
def __call__(self):
117120
yield 0
118121
raise StopIteration
122+
123+
# __bool__ returns `True` by default, so raising `TypeError` should not give an alert
124+
# FP reported in https://github.com/github/codeql/issues/2388
125+
def __bool__(self):
126+
raise TypeError

0 commit comments

Comments
 (0)