Added support for @abc.abstractproperty decorator.

This commit is contained in:
Eric Traut 2019-10-25 08:55:54 -07:00
parent 7be19b6a52
commit a6bf2c0e59
3 changed files with 41 additions and 1 deletions

View File

@ -2347,7 +2347,8 @@ export class TypeAnalyzer extends ParseTreeWalker {
return inputFunctionType;
}
case 'property': {
case 'property':
case 'abstractproperty': {
if (inputFunctionType.category === TypeCategory.Function) {
// Allocate a property only during the first analysis pass.
// Otherwise the analysis won't converge if there are setters

View File

@ -0,0 +1,33 @@
# This sample tests handling of the @abc.abstractpropery decorator.
import abc
def requires_int(x: int):
pass
class Foo(abc.ABC):
@abc.abstractproperty
def x(self) -> int:
raise NotImplementedError
@x.setter
def x(self, value: int):
raise NotImplementedError
@abc.abstractproperty
def y(self) -> float:
raise NotImplementedError
a = Foo()
requires_int(a.x)
a.x = 3
# This should generate an error because a.y is not an int
requires_int(a.y)
# This should generate an error because the assigned type
# isn't compatible with the setter.
a.x = 4.5

View File

@ -283,6 +283,12 @@ test('Properties1', () => {
validateResults(analysisResults, 5);
});
test('Properties2', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['properties2.py']);
validateResults(analysisResults, 2);
});
test('Operators1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['operators1.py']);