Skip to content

Commit 00c3fb4

Browse files
Python: Fix isType false positive on modules named *typing (fixes #22621)
1 parent 5de6283 commit 00c3fb4

7 files changed

Lines changed: 71 additions & 1 deletion

File tree

‎python/ql/lib/semmle/python/objects/TObject.qll‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ private predicate scope_loads_tuplenode(Scope s, TupleNode origin) {
216216
predicate isType(ObjectInternal t) {
217217
t.isClass() = true
218218
or
219-
t.getOrigin().getEnclosingModule().getName().matches("%typing")
219+
exists(string name | name = t.getOrigin().getEnclosingModule().getName() |
220+
name = "typing" or name = "typing_extensions" or name.matches("typing.%")
221+
)
220222
}
221223

222224
private predicate is_power_2(int n) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| main.py:6:1:6:13 | ControlFlowNode for InTyping() | resolved: InTyping() |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import python
2+
private import LegacyPointsTo
3+
4+
from ControlFlowNodeWithPointsTo use, string outcome
5+
where
6+
exists(CallNode call | call.getFunction().(NameNode).getId() = "InTyping" and use = call) and
7+
(
8+
if exists(use.pointsTo())
9+
then outcome = "resolved: " + use.pointsTo().toString()
10+
else outcome = "no points-to info"
11+
)
12+
select use, outcome
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Optional
2+
3+
from pkg.other import InOther
4+
from pkg.typing import InTyping
5+
6+
InTyping("a")
7+
Optional[InTyping]
8+
9+
InOther("b")
10+
Optional[InOther]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
registry = {}
2+
3+
4+
def make_type(name):
5+
if name in registry:
6+
return registry[name]
7+
8+
class Created(str):
9+
pass
10+
11+
registry[name] = Created
12+
return Created
13+
14+
15+
InTyping = make_type("InTyping")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
registry = {}
2+
3+
4+
def make_type(name):
5+
if name in registry:
6+
return registry[name]
7+
8+
class Created(str):
9+
pass
10+
11+
registry[name] = Created
12+
return Created
13+
14+
15+
InOther = make_type("InOther")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
registry = {}
2+
3+
4+
def make_type(name):
5+
if name in registry:
6+
return registry[name]
7+
8+
class Created(str):
9+
pass
10+
11+
registry[name] = Created
12+
return Created
13+
14+
15+
InTyping = make_type("InTyping")

0 commit comments

Comments
 (0)