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

Commit 2cab4fd

Browse files
authored
Fallback to function when property's declaringType is unknown (#1416)
1 parent 352fecc commit 2cab4fd

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Analysis/Ast/Impl/Analyzer/Symbols/SymbolCollector.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ private PythonFunctionOverload GetOverloadFromStub(FunctionDefinition node) {
150150
}
151151

152152
private bool TryAddProperty(FunctionDefinition node, IPythonType declaringType) {
153+
// We can't add a property to an unknown type. Fallback to a regular function for now.
154+
// TOOD: Decouple declaring types from the property.
155+
if (declaringType.IsUnknown()) {
156+
return false;
157+
}
158+
153159
var dec = node.Decorators?.Decorators;
154160
var decorators = dec != null ? dec.ExcludeDefault().ToArray() : Array.Empty<Expression>();
155161

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ private void ProcessDecorators(FunctionDefinition fd) {
143143
break;
144144
case @"property":
145145
case @"abstractproperty":
146-
Debug.Assert(false, "Found property attribute while processing function. Properties should be handled in the respective class.");
146+
// Ignore property decorators if the declaring type is unknown.
147+
// TODO: Restore this to a hard failure once property can handle not having a declaring type.
148+
Debug.Assert(DeclaringType.IsUnknown(), "Found property attribute while processing function. Properties should be handled in the respective class.");
147149
break;
148150
}
149151
}

src/Analysis/Ast/Test/ClassesTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,5 +634,21 @@ def __init__(self):
634634
// Test run time: typically ~ 20 sec.
635635
sw.ElapsedMilliseconds.Should().BeLessThan(60000);
636636
}
637+
638+
[TestMethod, Priority(0)]
639+
public async Task NestedProperty() {
640+
const string code = @"
641+
class x(object):
642+
def func(self):
643+
@property
644+
def foo(*args, **kwargs):
645+
pass
646+
return 42
647+
648+
a = x().func()
649+
";
650+
var analysis = await GetAnalysisAsync(code);
651+
analysis.Should().HaveVariable("a").OfType(BuiltinTypeId.Int);
652+
}
637653
}
638654
}

0 commit comments

Comments
 (0)