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

Fix fetching members from bases #1742

Merged
merged 15 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
13 changes: 3 additions & 10 deletions src/Analysis/Ast/Impl/Types/PythonClassType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ public override IEnumerable<string> GetMemberNames() {
}

public override IMember GetMember(string name) {
IMember member;

lock (_membersLock) {
if (Members.TryGetValue(name, out member)) {
if (Members.TryGetValue(name, out var member)) {
return member;
}
}
Expand All @@ -101,12 +99,7 @@ public override IMember GetMember(string name) {

using (_memberGuard.Push(this, out var reentered)) {
if (!reentered) {
foreach (var m in Mro.Reverse()) {
if (m == this) {
return member;
}
member = member ?? m.GetMember(name);
}
return Mro.Skip(1).Select(c => c.GetMember(name)).ExcludeDefault().FirstOrDefault();
}
return null;
}
Expand Down Expand Up @@ -348,7 +341,7 @@ private IEnumerable<IPythonType> DisambiguateBases(IEnumerable<IPythonType> base
// Get locally declared variable, make sure it is a declaration
// and that it declared a class.
var lv = scope.Variables[b.Name];
if (lv.Source != VariableSource.Import && lv.Value is IPythonClassType cls && cls.IsDeclaredAfterOrAt(this.Location)) {
if (lv.Source != VariableSource.Import && lv.Value is IPythonClassType cls && cls.IsDeclaredAfterOrAt(Location)) {
// There is a declaration with the same name, but it appears later in the module. Use the import.
if (!importedType.IsUnknown()) {
newBases.Add(importedType);
Expand Down
17 changes: 17 additions & 0 deletions src/Analysis/Ast/Test/ClassesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,23 @@ def __init__(self, value):
.Which.Should().HaveParameterAt(0).Which.Should().HaveName("self").And.HaveType("X");
}

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

class B(A): ...
";
var analysis = await GetAnalysisAsync(code);
analysis.Should().HaveClass("B")
.Which.Should().HaveMethod("__init__")
.Which.Should().HaveSingleOverload()
.Which.Should().HaveParameterAt(1)
.Which.Name.Should().Be("value");
}

[TestMethod, Priority(0)]
public async Task ClassBase2X() {
const string code = @"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Linq;
using Microsoft.Python.Analysis;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
Expand Down Expand Up @@ -56,7 +57,7 @@ public MarkupContent GetHover(string name, IMember member, IPythonType self, boo

if (includeClassInit) {
className = cls.Name;
var init = cls.GetMember<IPythonFunctionType>("__init__");
var init = cls.TryGetFunctionType();
if (init != null) {
sig = GetSignatureString(init, null, out var _, 0, "", true);
}
Expand Down