Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Analysis/Ast/Impl/Modules/BuiltinsPythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private void SpecializeFunctions() {
Analysis.SpecializeFunction("cmp", Interpreter.GetBuiltinType(BuiltinTypeId.Int));
Analysis.SpecializeFunction("dir", BuiltinsSpecializations.ListOfStrings);
Analysis.SpecializeFunction("eval", Interpreter.GetBuiltinType(BuiltinTypeId.Object));
Analysis.SpecializeFunction("getattr", BuiltinsSpecializations.GetAttr);
Analysis.SpecializeFunction("globals", BuiltinsSpecializations.DictStringToObject);
Analysis.SpecializeFunction(@"isinstance", _boolType);
Analysis.SpecializeFunction(@"issubclass", _boolType);
Expand Down
25 changes: 25 additions & 0 deletions src/Analysis/Ast/Impl/Specializations/BuiltinsSpecializations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,30 @@ public static IMember Open(IPythonModule declaringModule, IPythonFunctionOverloa
var returnType = io?.GetMember(returnTypeName)?.GetPythonType();
return returnType != null ? new PythonInstance(returnType) : null;
}

public static IMember GetAttr(IPythonModule module, IPythonFunctionOverload overload, IArgumentSet argSet) {
// TODO: Try __getattr__ first; this may not be as reliable in practice
// given we could be assuming that __getattr__ always returns the same type,
// which is incorrect more often than not.

var args = argSet.Values<IMember>();
if (args.Count < 2) {
return null;
}

var o = args[0];
var name = (args[1] as IPythonConstant)?.GetString();

IMember def = null;
if (args.Count >= 3) {
def = args[2];
}

if (name == null) {
return def;
}

return o?.GetPythonType().GetMember(name) ?? def;
}
}
}
23 changes: 23 additions & 0 deletions src/Analysis/Ast/Test/ClassesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,5 +572,28 @@ class C(B):
analysis.Should().HaveClass("B").Which.Should().HaveDocumentation("class B doc");
analysis.Should().HaveClass("C").Which.Should().HaveDocumentation("class C doc");
}

[TestMethod, Priority(0)]
public async Task GetAttr() {
const string code = @"
class A:
def __init__(self):
self.x = 123

a = A()
b = getattr(a, 'x')
c = getattr(a, 'y', 3.141)
d = getattr(a)
e = getattr()
f = getattr(a, 3.141)
";
var analysis = await GetAnalysisAsync(code);
analysis.Should().HaveVariable("a").OfType("A")
.And.HaveVariable("b").OfType(BuiltinTypeId.Int)
.And.HaveVariable("c").OfType(BuiltinTypeId.Float)
.And.HaveVariable("d").OfType(BuiltinTypeId.Unknown)
.And.HaveVariable("e").OfType(BuiltinTypeId.Unknown)
.And.HaveVariable("f").OfType(BuiltinTypeId.Unknown);
}
}
}