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

Commit 198e8dd

Browse files
author
Mikhail Arkhipov
authored
Merge pull request #283 from losttech/features/FunctionParameterTypePropagationToBase
Added an option to propagate parameter types to base methods
2 parents 27551ab + cea5edb commit 198e8dd

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

src/Analysis/Engine/Impl/AnalysisLimits.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ public AnalysisLimits() {
180180
/// </summary>
181181
public bool ProcessCustomDecorators { get; set; }
182182

183+
/// <summary>
184+
/// <c>True</c> to propagate parameter types to base methods.
185+
/// Parameter types always propagate to derived methods.
186+
/// </summary>
187+
public bool PropagateParameterTypeToBaseMethods { get; set; }
188+
183189
/// <summary>
184190
/// True to read information from type stub packages.
185191
/// </summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ private bool TryImportModule(string modName, bool forceAbsolute, out ModuleRefer
454454
return false;
455455
}
456456

457-
internal List<AnalysisValue> LookupBaseMethods(string name, IEnumerable<IAnalysisSet> mro, Node node, AnalysisUnit unit) {
457+
internal static List<AnalysisValue> LookupBaseMethods(string name, IEnumerable<IAnalysisSet> mro, Node node, AnalysisUnit unit) {
458458
var result = new List<AnalysisValue>();
459459
foreach (var @class in mro.Skip(1)) {
460460
foreach (var curType in @class) {

src/Analysis/Engine/Impl/Values/FunctionInfo.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] a
103103
}
104104
}
105105

106+
if (_analysisUnit.State.Limits.PropagateParameterTypeToBaseMethods
107+
&& _analysisUnit.Scope.OuterScope is ClassScope parentClass) {
108+
var baseMethods = DDG.LookupBaseMethods(Name, parentClass.Class.Mro, AnalysisUnit.Ast, AnalysisUnit);
109+
foreach (FunctionInfo baseMethod in baseMethods.OfType<FunctionInfo>()) {
110+
baseMethod.DoCall(node, unit, baseMethod._analysisUnit, callArgs);
111+
}
112+
}
113+
106114
if (_callsWithClosure != null) {
107115
var chain = new CallChain(node, unit, _callDepthLimit);
108116
var aggregate = GetAggregate(unit);

src/Analysis/Engine/Test/InheritanceTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,33 @@ def foo(self, x):
8383
analysis.Should().HaveClass("Derived").WithFunction("foo")
8484
.WithParameter("x").OfType(BuiltinTypeId.Int);
8585
}
86+
87+
[TestMethod]
88+
public async Task ParameterTypesPropagateToBaseFunctions() {
89+
var code = @"
90+
class Baze:
91+
def foo(self, x):
92+
pass
93+
94+
class Derived(Baze):
95+
def foo(self, x):
96+
pass
97+
98+
Derived().foo(42)
99+
";
100+
101+
using (var server = await new Server().InitializeAsync(PythonVersions.Required_Python36X)) {
102+
server.Analyzer.Limits.PropagateParameterTypeToBaseMethods = true;
103+
104+
var analysis = await server.OpenDefaultDocumentAndGetAnalysisAsync(code);
105+
106+
// the class, for which we know parameter type initially
107+
analysis.Should().HaveClass("Derived").WithFunction("foo")
108+
.WithParameter("x").OfType(BuiltinTypeId.Int);
109+
// its base class
110+
analysis.Should().HaveClass("Baze").WithFunction("foo")
111+
.WithParameter("x").OfType(BuiltinTypeId.Int);
112+
}
113+
}
86114
}
87115
}

0 commit comments

Comments
 (0)