Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 475f557

Browse files
committed
Persistence update:
- changing qualified name to :SuperType - trimming the leading ":" from builttin types
1 parent 75f36b4 commit 475f557

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

src/Analysis/Ast/Impl/Types/PythonSuperType.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public PythonSuperType(Location location, IReadOnlyList<IPythonType> mro)
2929
Mro = mro;
3030
}
3131

32+
public override string QualifiedName {
33+
get {
34+
return $":SuperType[{string.Join(",", Mro.Select(t => t.QualifiedName))}]";
35+
}
36+
}
37+
3238
public IReadOnlyList<IPythonType> Mro { get; }
3339

3440
public override IMember GetMember(string name) => Mro.MaybeEnumerate().Select(c => c.GetMember(name)).ExcludeDefault().FirstOrDefault();

src/Caching/Impl/Models/ModuleModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal sealed class ModuleModel : MemberModel {
3838
public ClassModel[] Classes { get; set; }
3939
public TypeVarModel[] TypeVars { get; set; }
4040
public NamedTupleModel[] NamedTuples { get; set; }
41-
41+
4242
/// <summary>
4343
/// Collection of new line information for conversion of linear spans
4444
/// to line/columns in navigation to member definitions and references.
@@ -69,7 +69,7 @@ public static ModuleModel FromAnalysis(IDocumentAnalysis analysis, IServiceConta
6969
var classes = new Dictionary<string, ClassModel>();
7070
var typeVars = new Dictionary<string, TypeVarModel>();
7171
var namedTuples = new Dictionary<string, NamedTupleModel>();
72-
72+
7373
// Go directly through variables which names are listed in GetMemberNames
7474
// as well as variables that are declarations.
7575
var exportedNames = new HashSet<string>(analysis.Document.GetMemberNames());

src/Caching/Impl/ModuleFactory.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private IMember GetMember(IMember root, IEnumerable<string> memberNames) {
169169
if (mc is IBuiltinsPythonModule builtins) {
170170
// Builtins require special handling since there may be 'hidden' names
171171
// which need to be mapped to visible types.
172-
member = GetBuiltinMember(builtins, memberName) ?? builtins.Interpreter.UnknownType;
172+
member = GetBuiltinMember(builtins, memberName, typeArgs) ?? builtins.Interpreter.UnknownType;
173173
} else {
174174
member = mc?.GetMember(memberName);
175175
// Work around problem that some stubs have incorrectly named tuples.
@@ -193,7 +193,7 @@ private IMember GetMember(IMember root, IEnumerable<string> memberNames) {
193193
return member;
194194
}
195195

196-
private IMember GetBuiltinMember(IBuiltinsPythonModule builtins, string memberName) {
196+
private IMember GetBuiltinMember(IBuiltinsPythonModule builtins, string memberName, IReadOnlyList<IPythonType> typeArgs) {
197197
if (memberName.StartsWithOrdinal("__")) {
198198
memberName = memberName.Substring(2, memberName.Length - 4);
199199
}
@@ -203,6 +203,10 @@ private IMember GetBuiltinMember(IBuiltinsPythonModule builtins, string memberNa
203203
return builtins.Interpreter.GetBuiltinType(BuiltinTypeId.None);
204204
case "Unknown":
205205
return builtins.Interpreter.UnknownType;
206+
case "SuperType":
207+
var location = new Location(Module, default);
208+
return new PythonSuperType(location, typeArgs);
209+
206210
}
207211
return builtins.GetMember(memberName);
208212
}

src/Caching/Impl/TypeNames.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ private static void GetObjectTypeFromPrefix(string qualifiedName, ref QualifiedN
8989
private static void GetModuleNameAndMembers(string qualifiedName, ref QualifiedNameParts parts, int prefixOffset) {
9090
// Strip the prefix, turning i:module:A.B.C into module:A.B.C
9191
var typeName = qualifiedName.Substring(prefixOffset);
92-
92+
9393
var moduleSeparatorIndex = typeName.IndexOf(':');
94-
if (moduleSeparatorIndex < 0) {
94+
if (moduleSeparatorIndex <= 0) {
95+
typeName = typeName.TrimStart(':');
9596
switch (parts.ObjectType) {
9697
case ObjectType.Type:
9798
case ObjectType.Instance:

src/Caching/Test/ClassesTests.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ def __init__(self):
152152
}
153153

154154
[TestMethod, Priority(0)]
155-
[Ignore("Todo: super() persistence support, Asserts in debug due to no model found for super")]
156155
public async Task ClassesWithSuper() {
157156
const string code = @"
158157
class A:
@@ -162,18 +161,38 @@ def methodA(self):
162161
class B(A):
163162
def methodB(self):
164163
return super()
164+
";
165+
var analysis = await GetAnalysisAsync(code);
166+
167+
var model = ModuleModel.FromAnalysis(analysis, Services, AnalysisCachingLevel.Library);
168+
169+
using (var dbModule = CreateDbModule(model, analysis.Document.FilePath)) {
170+
dbModule.Should().HaveSameMembersAs(analysis.Document);
171+
}
172+
}
165173

166-
b = B().methodB()
174+
[TestMethod, Priority(0)]
175+
public async Task GlobalSuper() {
176+
const string code = @"
177+
class Baze:
178+
def baze_foo(self):
179+
pass
180+
181+
class Derived(Baze):
182+
def foo(self):
183+
pass
184+
185+
d = Derived()
186+
187+
x = super(Derived, d)
167188
";
168189
var analysis = await GetAnalysisAsync(code);
169-
analysis.Should().HaveVariable("b").Which.Should().HaveType("super");
170190

171191
var model = ModuleModel.FromAnalysis(analysis, Services, AnalysisCachingLevel.Library);
172-
192+
173193
using (var dbModule = CreateDbModule(model, analysis.Document.FilePath)) {
174194
dbModule.Should().HaveSameMembersAs(analysis.Document);
175195
}
176196
}
177-
178197
}
179198
}

0 commit comments

Comments
 (0)