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

Commit d364002

Browse files
author
Mikhail Arkhipov
committed
Fix fetching members from bases (#1742)
* Remove stale reference * Fix fetching ctor from bases * Test fix (cherry picked from commit c4a5c9c)
1 parent aece368 commit d364002

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ public override IEnumerable<string> GetMemberNames() {
7979
}
8080

8181
public override IMember GetMember(string name) {
82-
IMember member;
83-
8482
lock (_membersLock) {
85-
if (Members.TryGetValue(name, out member)) {
83+
if (Members.TryGetValue(name, out var member)) {
8684
return member;
8785
}
8886
}
@@ -101,12 +99,7 @@ public override IMember GetMember(string name) {
10199

102100
using (_memberGuard.Push(this, out var reentered)) {
103101
if (!reentered) {
104-
foreach (var m in Mro.Reverse()) {
105-
if (m == this) {
106-
return member;
107-
}
108-
member = member ?? m.GetMember(name);
109-
}
102+
return Mro.Skip(1).Select(c => c.GetMember(name)).ExcludeDefault().FirstOrDefault();
110103
}
111104
return null;
112105
}

src/Analysis/Ast/Test/ClassesTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,23 @@ def __init__(self, value):
270270
.Which.Should().HaveParameterAt(0).Which.Should().HaveName("self").And.HaveType("X");
271271
}
272272

273+
[TestMethod, Priority(0)]
274+
public async Task ClassBaseInit() {
275+
const string code = @"
276+
class A:
277+
def __init__(self, value):
278+
self.value = value
279+
280+
class B(A): ...
281+
";
282+
var analysis = await GetAnalysisAsync(code);
283+
analysis.Should().HaveClass("B")
284+
.Which.Should().HaveMethod("__init__")
285+
.Which.Should().HaveSingleOverload()
286+
.Which.Should().HaveParameterAt(1)
287+
.Which.Name.Should().Be("value");
288+
}
289+
273290
[TestMethod, Priority(0)]
274291
public async Task ClassBase2X() {
275292
const string code = @"

src/LanguageServer/Impl/Sources/MarkdownDocumentationSource.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the Apache Version 2.0 License for specific language governing
1414
// permissions and limitations under the License.
1515

16+
using System.Linq;
1617
using Microsoft.Python.Analysis;
1718
using Microsoft.Python.Analysis.Types;
1819
using Microsoft.Python.Analysis.Values;
@@ -56,7 +57,7 @@ public MarkupContent GetHover(string name, IMember member, IPythonType self, boo
5657

5758
if (includeClassInit) {
5859
className = cls.Name;
59-
var init = cls.GetMember<IPythonFunctionType>("__init__");
60+
var init = cls.TryGetFunctionType();
6061
if (init != null) {
6162
sig = GetSignatureString(init, null, out var _, 0, "", true);
6263
}

0 commit comments

Comments
 (0)