public async Task ForwardRefMethod()
        {
            const string code     = @"
class Box():
    def get(self) -> 'int':
        return self.v

boxedint = Box()
x = boxedint.get()

def get() -> 'int':
    pass

y = get()
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(7, 18));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("get() -> int");

            sig = src.GetSignature(analysis, new SourceLocation(12, 9));
            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("get() -> int");
        }
        public async Task GenericClassMethod()
        {
            const string code     = @"
from typing import TypeVar, Generic

_T = TypeVar('_T')

class Box(Generic[_T]):
    def get(self) -> _T:
        return self.v

boxedint = Box(1234)
x = boxedint.get()

boxedstr = Box('str')
y = boxedstr.get()
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(11, 18));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("get() -> int");

            sig = src.GetSignature(analysis, new SourceLocation(14, 18));
            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("get() -> str");
        }
        public async Task MethodSignature(bool clientSupportsParameterLabelOffsets)
        {
            const string code     = @"
class C:
    def method(self, a:int, b) -> float:
        return 1.0

C().method()
";
            var          analysis = await GetAnalysisAsync(code);

            var src = new SignatureSource(new PlainTextDocumentationSource(), clientSupportsParameterLabelOffsets);

            var sig = src.GetSignature(analysis, new SourceLocation(6, 12));

            sig.activeSignature.Should().Be(0);
            sig.activeParameter.Should().Be(0);
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("method(a: int, b) -> float");

            var labels = sig.signatures[0].parameters.Select(p => p.label);

            if (clientSupportsParameterLabelOffsets)
            {
                var parameterSpans = labels.OfType <int[]>().SelectMany(x => x);
                parameterSpans.Should().ContainInOrder(7, 8, 15, 16);
            }
            else
            {
                var parameterSpans = labels.OfType <string>();
                parameterSpans.Should().ContainInOrder("a", "b");
            }
        }
Example #4
0
        public async Task <SignatureHelp> SignatureHelp(TextDocumentPositionParams @params, CancellationToken cancellationToken)
        {
            var uri = @params.textDocument.uri;

            _log?.Log(TraceEventType.Verbose, $"Signatures in {uri} at {@params.position}");

            var analysis = await Document.GetAnalysisAsync(uri, Services, CompletionAnalysisTimeout, cancellationToken);

            if (analysis != null)
            {
                return(_signatureSource.GetSignature(analysis, @params.position));
            }
            return(null);
        }
        public async Task PositionalOnly()
        {
            const string code     = @"
def f(a, b, /, c, d, *, e, f):
    pass

f()
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(5, 3));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("f(a, b, /, c, d, *, e, f)");
        }
        public async Task UnknownForwardRefParam()
        {
            const string code     = @"
def get(v: 'tmp[unknown_type]') -> 'unknown_type':
    pass

y = get()
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(5, 9));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("get(v) -> 'unknown_type'");
        }
        public async Task ForwardRefUnboundFunction()
        {
            const string code     = @"
def get() -> 'int':
    pass

y = get()
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(5, 9));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("get() -> int");
        }
        public async Task GenericUnboundForwardRefFunction()
        {
            const string code     = @"
from typing import Dict
def tmp(v: 'Dict[str, int]') -> 'Dict[str, int]':
    pass
h = tmp()
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(5, 9));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("tmp(v: Dict[str, int]) -> Dict[str, int]");
        }
Example #9
0
        public async Task DefaultValues()
        {
            const string code     = @"
def hello(a, b=100, c={'a':4}):
    return a + b

hello()
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(5, 7));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("hello(a, b=100, c={'a':4})");
        }
        public async Task ClassInitializer()
        {
            const string code     = @"
class C:
    def __init__(self, a:int, b):
        pass

C()
";
            var          analysis = await GetAnalysisAsync(code);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(6, 3));

            sig.activeSignature.Should().Be(0);
            sig.activeParameter.Should().Be(0);
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("C(a: int, b)");
        }
        public async Task MethodSignature()
        {
            const string code     = @"
class C:
    def method(self, a:int, b) -> float:
        return 1.0

C().method()
";
            var          analysis = await GetAnalysisAsync(code);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(6, 12));

            sig.activeSignature.Should().Be(0);
            sig.activeParameter.Should().Be(0);
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("method(a: int, b) -> float");
        }
        public async Task ImportedClassInitializer()
        {
            const string module1Code = @"
class C:
    def __init__(self, a:int, b):
        pass
";

            const string appCode    = @"
import module1

module1.C()
";
            var          module1Uri = TestData.GetTestSpecificUri("module1.py");
            var          appUri     = TestData.GetTestSpecificUri("app.py");

            var root = Path.GetDirectoryName(appUri.AbsolutePath);

            await CreateServicesAsync(root, PythonVersions.LatestAvailable3X);

            var rdt      = Services.GetService <IRunningDocumentTable>();
            var analyzer = Services.GetService <IPythonAnalyzer>();

            rdt.OpenDocument(module1Uri, module1Code);

            var app = rdt.OpenDocument(appUri, appCode);
            await analyzer.WaitForCompleteAnalysisAsync();

            var analysis = await app.GetAnalysisAsync(-1);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(4, 11));

            sig.activeSignature.Should().Be(0);
            sig.activeParameter.Should().Be(0);
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("C(a: int, b)");
        }
        public async Task ForwardRefNestedQuotedGenericReturnValue()
        {
            const string code     = @"
from typing import List, Sequence

class Box():
    def get(self, x: 'Sequence[List[int]]') -> 'Sequence[List[int]]':
        return self.v

boxedint = Box()
x = boxedint.get(5)
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(9, 19));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("get(x: Sequence[List[int]]) -> Sequence[List[int]]");
        }
        public async Task ForwardRefQuotedGenericTypeParameter()
        {
            const string code     = @"
from typing import List

class X: ...

class Box():
    def get(self) -> List['X']:
        return self.v

boxedint = Box()
x = boxedint.get()
";
            var          analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);

            var src = new SignatureSource(new PlainTextDocumentationSource());

            var sig = src.GetSignature(analysis, new SourceLocation(11, 18));

            sig.signatures.Should().NotBeNull();
            sig.signatures.Length.Should().Be(1);
            sig.signatures[0].label.Should().Be("get() -> List[X]");
        }