Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public IPythonType GetTypeFromPepHint(Node node) {
}

// Type alone is not a valid syntax, so we need to simulate the annotation.
var typeString = content.Substring(hintStart, i - hintStart);
var typeString = content.Substring(hintStart, i - hintStart).Trim();
return GetTypeFromString(typeString);
}

Expand All @@ -88,7 +88,8 @@ public IPythonType GetTypeFromString(string typeString) {
typeString = $"x: {typeString}";
using (var sr = new StringReader(typeString)) {
var sink = new CollectingErrorSink();
var parser = Parser.CreateParser(sr, Module.Interpreter.LanguageVersion, new ParserOptions { ErrorSink = sink });
// Always use Python 3 since expression is an annotation which is 3.x
var parser = Parser.CreateParser(sr, PythonLanguageVersion.V36, new ParserOptions { ErrorSink = sink });
var ast = parser.ParseFile();
var exprStatement = (ast?.Body as SuiteStatement)?.Statements?.FirstOrDefault() as ExpressionStatement;
if (!(Statement.GetExpression(exprStatement) is ExpressionWithAnnotation annExpr) || sink.Errors.Count > 0) {
Expand Down
14 changes: 14 additions & 0 deletions src/Analysis/Ast/Test/PepHintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.Python.Analysis.Tests.FluentAssertions;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Parsing.Tests;
using Microsoft.Python.Tests.Utilities.FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;
Expand Down Expand Up @@ -76,5 +77,18 @@ class Response: # truncated
c.Should().HaveMember<IPythonInstance>("elapsed")
.Which.Should().HaveSameMembersAs(timedelta);
}

[TestMethod, Priority(0)]
public async Task HintsInPython2() {
const string code = @"
def func(x):
y = x # type: int
";
var analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable2X);

analysis.Should().HaveFunction("func")
.Which.Should().HaveVariable("y")
.Which.Should().HaveType(BuiltinTypeId.Int);
}
}
}