Thanks to visit codestin.com
Credit goes to github.com

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 @@ -13,13 +13,15 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Collections.Generic;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Types.Collections;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Parsing;
using Microsoft.Python.Parsing.Ast;
using ErrorCodes = Microsoft.Python.Analysis.Diagnostics.ErrorCodes;

namespace Microsoft.Python.Analysis.Analyzer.Evaluation {
internal sealed partial class ExpressionEval {
Expand Down Expand Up @@ -122,6 +124,9 @@ private IMember GetValueFromBinaryOp(Expression expr) {

if (leftIsSupported && rightIsSupported) {
if (TryGetValueFromBuiltinBinaryOp(op, leftTypeId, rightTypeId, Interpreter.LanguageVersion.Is3x(), out var member)) {
if (member.IsUnknown()) {
ReportOperatorDiagnostics(expr, leftType, rightType, op);
}
return member;
}
}
Expand Down Expand Up @@ -435,5 +440,14 @@ private static (string name, string swappedName) OpMethodName(PythonOperator op)

return (null, null);
}
private void ReportOperatorDiagnostics(Expression expr, IPythonType leftType, IPythonType rightType, PythonOperator op) {
ReportDiagnostics(Module.Uri, new DiagnosticsEntry(
Resources.UnsupporedOperandType.FormatInvariant(op.ToCodeString(), leftType.Name, rightType.Name),
GetLocation(expr).Span,
ErrorCodes.UnsupportedOperandType,
Severity.Error,
DiagnosticSource.Analysis));
}

}
}
1 change: 1 addition & 0 deletions src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static class ErrorCodes {
public const string UndefinedVariable = "undefined-variable";
public const string VariableNotDefinedGlobally= "variable-not-defined-globally";
public const string VariableNotDefinedNonLocal = "variable-not-defined-nonlocal";
public const string UnsupportedOperandType = "unsupported-operand-type";
public const string ReturnInInit = "return-in-init";
public const string TypingGenericArguments = "typing-generic-arguments";
}
Expand Down
9 changes: 9 additions & 0 deletions src/Analysis/Ast/Impl/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Analysis/Ast/Impl/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@
<data name="UnableToDetermineCachePathException" xml:space="preserve">
<value>Unable to determine analysis cache path. Exception: {0}. Using default '{1}'.</value>
</data>
<data name="UnsupporedOperandType" xml:space="preserve">
<value>Unsupported operand types for '{0}': '{1}' and '{2}'</value>
</data>
<data name="ReturnInInit" xml:space="preserve">
<value>Explicit return in __init__ </value>
</data>
Expand All @@ -198,4 +201,4 @@
<data name="GenericNotAllUnique" xml:space="preserve">
<value>Arguments to Generic must all be unique.</value>
</data>
</root>
</root>
86 changes: 86 additions & 0 deletions src/Analysis/Ast/Test/LintOperatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Python.Analysis.Tests.FluentAssertions;
using Microsoft.Python.Core;
using Microsoft.Python.Parsing.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;

namespace Microsoft.Python.Analysis.Tests {
[TestClass]
public class LintOperatorTests : AnalysisTestBase {

public TestContext TestContext { get; set; }

[TestInitialize]
public void TestInitialize()
=> TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}");

[TestCleanup]
public void Cleanup() => TestEnvironmentImpl.TestCleanup();

[TestMethod, Priority(0)]
public async Task IncompatibleTypesBinaryOpBasic() {
var code = $@"
a = 5 + 'str'
";

var analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);
analysis.Diagnostics.Should().HaveCount(1);

var diagnostic = analysis.Diagnostics.ElementAt(0);
diagnostic.SourceSpan.Should().Be(2, 5, 2, 14);
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.UnsupportedOperandType);
diagnostic.Message.Should().Be(Resources.UnsupporedOperandType.FormatInvariant("+", "int", "str"));
}

[DataRow("str", "int", "+")]
[DataRow("str", "int", "-")]
[DataRow("str", "int", "/")]
[DataRow("str", "float", "+")]
[DataRow("str", "float", "-")]
[DataRow("str", "float", "*")]
[DataTestMethod, Priority(0)]
public async Task IncompatibleTypesBinaryOp(string leftType, string rightType, string op) {
var code = $@"
x = 1
y = 2

z = {leftType}(x) {op} {rightType}(y)
";

var analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);
analysis.Diagnostics.Should().HaveCount(1);

var diagnostic = analysis.Diagnostics.ElementAt(0);


string line = $"z = {leftType}(x) {op} {rightType}(y)";
// source span is 1 indexed
diagnostic.SourceSpan.Should().Be(5, line.IndexOf(leftType) + 1, 5, line.IndexOf("(y)") + 4);
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.UnsupportedOperandType);
diagnostic.Message.Should().Be(Resources.UnsupporedOperandType.FormatInvariant(op, leftType, rightType));
}

[DataRow("str", "str", "+")]
[DataRow("int", "int", "-")]
[DataRow("bool", "int", "/")]
[DataRow("float", "int", "+")]
[DataRow("complex", "float", "-")]
[DataRow("str", "int", "*")]
[DataTestMethod, Priority(0)]
public async Task CompatibleTypesBinaryOp(string leftType, string rightType, string op) {
var code = $@"
x = 1
y = 2

z = {leftType}(x) {op} {rightType}(y)
";

var analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);
analysis.Diagnostics.Should().BeEmpty();
}
}

}