Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit ce50093

Browse files
author
Mikhail Arkhipov
authored
Merge pull request microsoft#115 from AlexanderSher/master
Port PR from PTVS: microsoft/PTVS#4716
2 parents f338b0c + 2d8be3f commit ce50093

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

src/Analysis/Engine/Impl/Analyzer/DDG.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,13 @@ private bool TryImportModule(string modName, bool forceAbsolute, out ModuleRefer
454454
return false;
455455
}
456456

457-
internal List<AnalysisValue> LookupBaseMethods(string name, IEnumerable<IAnalysisSet> bases, Node node, AnalysisUnit unit) {
457+
internal List<AnalysisValue> LookupBaseMethods(string name, IEnumerable<IAnalysisSet> mro, Node node, AnalysisUnit unit) {
458458
var result = new List<AnalysisValue>();
459-
foreach (var b in bases) {
460-
foreach (var curType in b) {
461-
var klass = curType as BuiltinClassInfo;
462-
if (klass != null) {
463-
var value = klass.GetMember(node, unit, name);
459+
foreach (var @class in mro.Skip(1)) {
460+
foreach (var curType in @class) {
461+
bool isClass = curType is ClassInfo || curType is BuiltinClassInfo;
462+
if (isClass) {
463+
var value = curType.GetMember(node, unit, name);
464464
if (value != null) {
465465
result.AddRange(value);
466466
}
@@ -470,7 +470,6 @@ internal List<AnalysisValue> LookupBaseMethods(string name, IEnumerable<IAnalysi
470470
return result;
471471
}
472472

473-
474473
public override bool Walk(FunctionDefinition node) {
475474
InterpreterScope funcScope;
476475
if (_unit.InterpreterScope.TryGetNodeScope(node, out funcScope)) {
@@ -572,9 +571,28 @@ public override bool Walk(ImportStatement node) {
572571

573572
public override bool Walk(ReturnStatement node) {
574573
var fnScope = CurrentFunction;
575-
if (node.Expression != null && fnScope != null) {
576-
var lookupRes = _eval.Evaluate(node.Expression);
577-
fnScope.AddReturnTypes(node, _unit, lookupRes);
574+
if (fnScope == null || node.Expression == null) {
575+
return true;
576+
}
577+
578+
var lookupRes = _eval.Evaluate(node.Expression);
579+
fnScope.AddReturnTypes(node, _unit, lookupRes);
580+
581+
var function = fnScope.Function;
582+
var analysisUnit = (FunctionAnalysisUnit)function.AnalysisUnit;
583+
584+
if (Scope.OuterScope is ClassScope curClass) {
585+
var bases = LookupBaseMethods(
586+
analysisUnit.Ast.Name,
587+
curClass.Class.Mro,
588+
analysisUnit.Ast,
589+
analysisUnit
590+
);
591+
592+
foreach (FunctionInfo baseFunction in bases.OfType<FunctionInfo>()) {
593+
var baseAnalysisUnit = (FunctionAnalysisUnit)baseFunction.AnalysisUnit;
594+
baseAnalysisUnit.ReturnValue.AddTypes(_unit, lookupRes);
595+
}
578596
}
579597
return true;
580598
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Microsoft.Python.LanguageServer.Implementation;
6+
using Microsoft.PythonTools.Analysis.FluentAssertions;
7+
using Microsoft.PythonTools.Interpreter;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using TestUtilities;
10+
11+
namespace Microsoft.PythonTools.Analysis {
12+
[TestClass]
13+
public class InheritanceTests {
14+
public TestContext TestContext { get; set; }
15+
16+
[TestInitialize]
17+
public void TestInitialize() => TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}");
18+
19+
[TestCleanup]
20+
public void TestCleanup() => TestEnvironmentImpl.TestCleanup();
21+
22+
[TestMethod]
23+
public async Task AbstractMethodReturnTypeIgnored() {
24+
var code = @"import abc
25+
class A:
26+
@abc.abstractmethod
27+
def virt():
28+
pass
29+
class B(A):
30+
def virt():
31+
return 42
32+
a = A()
33+
b = a.virt()";
34+
35+
using (var server = await new Server().InitializeAsync(PythonVersions.Required_Python36X)) {
36+
var analysis = await server.OpenDefaultDocumentAndGetAnalysisAsync(code);
37+
38+
analysis.Should().HaveVariable("b").OfType(BuiltinTypeId.Int);
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)