Skip to content

Commit

Permalink
Fix #86 module type can't be self type (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinwang0 authored Mar 21, 2017
1 parent dc6d8f1 commit 0750a7a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/main/java/org/yinwang/pysonar/visitor/TypeInferencer.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,11 @@ public Type visit(Call node, State s) {
{
Node target = ((Attribute) node.func).target;
Name attr = ((Attribute) node.func).attr;
selfType = visit(target, s);
Set<Binding> b = selfType.table.lookupAttr(attr.id);
Type targetType = visit(target, s);
if (!(targetType instanceof ModuleType)) {
selfType = targetType;
}
Set<Binding> b = targetType.table.lookupAttr(attr.id);
if (b != null) {
Analyzer.self.putRef(attr, b);
fun = State.makeUnion(b);
Expand Down
3 changes: 3 additions & 0 deletions tests/import/same-level.test/mod1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

o = mod2.B()
print o.a

u = mod2.foo(10)
print u
6 changes: 5 additions & 1 deletion tests/import/same-level.test/mod2.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class B:
a = 'hi'
a = 'hi'


def foo(x):
return x + 1
90 changes: 87 additions & 3 deletions tests/import/same-level.test/refs.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,96 @@
{
"name": "a",
"file": "mod2.py",
"start": 11,
"end": 12,
"start": 13,
"end": 14,
"line": 2,
"col": 3,
"col": 5,
"type": "str"
}
]
},
{
"ref": {
"name": "mod2",
"file": "mod1.py",
"start": 41,
"end": 45,
"line": 6,
"col": 5
},
"dests": [
{
"name": "mod2",
"file": "mod2.py",
"start": 0,
"end": 0,
"line": 0,
"col": 0,
"type": "mod2"
}
]
},
{
"ref": {
"name": "foo",
"file": "mod1.py",
"start": 46,
"end": 49,
"line": 6,
"col": 10
},
"dests": [
{
"name": "foo",
"file": "mod2.py",
"start": 28,
"end": 31,
"line": 5,
"col": 5,
"type": "int -> int"
}
]
},
{
"ref": {
"name": "x",
"file": "mod2.py",
"start": 47,
"end": 48,
"line": 6,
"col": 12
},
"dests": [
{
"name": "x",
"file": "mod2.py",
"start": 32,
"end": 33,
"line": 5,
"col": 9,
"type": "int"
}
]
},
{
"ref": {
"name": "u",
"file": "mod1.py",
"start": 60,
"end": 61,
"line": 7,
"col": 7
},
"dests": [
{
"name": "u",
"file": "mod1.py",
"start": 37,
"end": 38,
"line": 6,
"col": 1,
"type": "int"
}
]
}
]

0 comments on commit 0750a7a

Please sign in to comment.