Skip to content

Commit 958763e

Browse files
committed
Python: Add test for ClassValue.isSequence() and isMapping()
For Python 3.6
1 parent 8513c69 commit 958763e

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
| mapping | builtin-class collections.defaultdict |
2+
| mapping | builtin-class dict |
3+
| neither sequence nor mapping | builtin-class set |
4+
| sequence | builtin-class bytes |
5+
| sequence | builtin-class collections.OrderedDict |
6+
| sequence | builtin-class list |
7+
| sequence | builtin-class str |
8+
| sequence | builtin-class tuple |
9+
| sequence | class OrderedDict |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import python
2+
3+
from ClassValue cls, string res
4+
where
5+
exists(CallNode call |
6+
call.getFunction().(NameNode).getId() = "test" and
7+
call.getAnArg().pointsTo(cls)
8+
) and
9+
(
10+
cls.isSequence() and
11+
cls.isMapping() and
12+
res = "IS BOTH. SHOULD NOT HAPPEN. THEY ARE MUTUALLY EXCLUSIVE."
13+
or
14+
cls.isSequence() and not cls.isMapping() and res = "sequence"
15+
or
16+
not cls.isSequence() and cls.isMapping() and res = "mapping"
17+
or
18+
not cls.isSequence() and not cls.isMapping() and res = "neither sequence nor mapping"
19+
)
20+
select res, cls.toString()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --max-import-depth=2
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from collections import OrderedDict, defaultdict
2+
import collections.abc
3+
4+
def test(*args):
5+
pass
6+
7+
test(
8+
list,
9+
tuple,
10+
str,
11+
bytes,
12+
set,
13+
dict,
14+
OrderedDict,
15+
defaultdict,
16+
)
17+
18+
for seq_cls in (list, tuple, str, bytes):
19+
assert issubclass(seq_cls, collections.abc.Sequence)
20+
assert not issubclass(seq_cls, collections.abc.Mapping)
21+
22+
for map_cls in (dict, OrderedDict, defaultdict):
23+
assert not issubclass(map_cls, collections.abc.Sequence)
24+
assert issubclass(map_cls, collections.abc.Mapping)
25+
26+
assert not issubclass(set, collections.abc.Sequence)
27+
assert not issubclass(set, collections.abc.Mapping)

0 commit comments

Comments
 (0)