From d6776e99b57df1be36eb86b4ba55d5b7d3db168b Mon Sep 17 00:00:00 2001 From: Cameron Trando Date: Mon, 15 Jul 2019 12:41:41 -0700 Subject: [PATCH 1/4] Revert "Adding diagnostic error on binary operations with incompatible types (#1254)" This is due to the fact that current operator type checking is too inaccurate to give diagnostics - operator overloading is a challenge to face for the future This reverts commit 753220f38648620fdcae180ac505b4e31535cdeb. --- .../Evaluation/ExpressionEval.Operators.cs | 16 +--- .../Ast/Impl/Diagnostics/ErrorCodes.cs | 1 - src/Analysis/Ast/Impl/Resources.Designer.cs | 9 -- src/Analysis/Ast/Impl/Resources.resx | 5 +- src/Analysis/Ast/Test/LintOperatorTests.cs | 86 ------------------- 5 files changed, 2 insertions(+), 115 deletions(-) delete mode 100644 src/Analysis/Ast/Test/LintOperatorTests.cs diff --git a/src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs b/src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs index 80b71ce00..d8f5c8a2f 100644 --- a/src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs +++ b/src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs @@ -13,15 +13,13 @@ // See the Apache Version 2.0 License for specific language governing // permissions and limitations under the License. -using Microsoft.Python.Analysis.Diagnostics; +using System.Collections.Generic; 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 { @@ -124,9 +122,6 @@ 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; } } @@ -440,14 +435,5 @@ 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)); - } - } } diff --git a/src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs b/src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs index b3e7b90fe..3b5338c02 100644 --- a/src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs +++ b/src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs @@ -25,7 +25,6 @@ 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 TypingNewTypeArguments = "typing-newtype-arguments"; public const string TypingGenericArguments = "typing-generic-arguments"; diff --git a/src/Analysis/Ast/Impl/Resources.Designer.cs b/src/Analysis/Ast/Impl/Resources.Designer.cs index 26b0bcdff..b8f34f0a3 100644 --- a/src/Analysis/Ast/Impl/Resources.Designer.cs +++ b/src/Analysis/Ast/Impl/Resources.Designer.cs @@ -311,14 +311,5 @@ internal static string UndefinedVariable { return ResourceManager.GetString("UndefinedVariable", resourceCulture); } } - - /// - /// Looks up a localized string similar to Unsupported operand types for '{0}': '{1}' and '{2}'. - /// - internal static string UnsupporedOperandType { - get { - return ResourceManager.GetString("UnsupporedOperandType", resourceCulture); - } - } } } diff --git a/src/Analysis/Ast/Impl/Resources.resx b/src/Analysis/Ast/Impl/Resources.resx index 9db6acd66..e92c3d3b0 100644 --- a/src/Analysis/Ast/Impl/Resources.resx +++ b/src/Analysis/Ast/Impl/Resources.resx @@ -192,9 +192,6 @@ The first argument to NewType must be a string, but it is of type '{0}'. - - Unsupported operand types for '{0}': '{1}' and '{2}' - Explicit return in __init__ @@ -204,4 +201,4 @@ Arguments to Generic must all be unique. - \ No newline at end of file + diff --git a/src/Analysis/Ast/Test/LintOperatorTests.cs b/src/Analysis/Ast/Test/LintOperatorTests.cs deleted file mode 100644 index 0578dbdbd..000000000 --- a/src/Analysis/Ast/Test/LintOperatorTests.cs +++ /dev/null @@ -1,86 +0,0 @@ -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(); - } - } - -} From 29dab6512c4afbd7f4826d7ae08b83cae8106ce0 Mon Sep 17 00:00:00 2001 From: Cameron Trando Date: Mon, 15 Jul 2019 12:44:52 -0700 Subject: [PATCH 2/4] Fixing import --- .../Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs b/src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs index d8f5c8a2f..e5dfe0330 100644 --- a/src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs +++ b/src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Operators.cs @@ -13,7 +13,6 @@ // 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.Modules; using Microsoft.Python.Analysis.Types; using Microsoft.Python.Analysis.Types.Collections; From 8112c41b7ee66e9b0547a5641c06d02653d9ae4e Mon Sep 17 00:00:00 2001 From: Cameron Trando Date: Mon, 15 Jul 2019 13:55:12 -0700 Subject: [PATCH 3/4] Checking build --- src/Analysis/Ast/Impl/Resources.resx.orig | 210 ++++++++++++++++++++++ src/Analysis/Ast/Test/LintNoQATests.cs | 11 +- 2 files changed, 215 insertions(+), 6 deletions(-) create mode 100644 src/Analysis/Ast/Impl/Resources.resx.orig diff --git a/src/Analysis/Ast/Impl/Resources.resx.orig b/src/Analysis/Ast/Impl/Resources.resx.orig new file mode 100644 index 000000000..c4de44b0c --- /dev/null +++ b/src/Analysis/Ast/Impl/Resources.resx.orig @@ -0,0 +1,210 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + '{0}' may not be callable + + + object may not be callable + + + unresolved import '{0}' + + + Relative import '{0}' beyond top-level package + + + '{0}' used before definition + + + property of type {0} + + + property of unknown type + + + Parameter {0} already specified. + + + Parameter {0} is missing. + + + Positional arguments are not allowed after keyword argument. + + + Too many function arguments. + + + Too many positional arguments before '*' argument. + + + Unknown parameter name. + + + Analyzing in background, {0} items left... + + + Undefined variable: '{0}' + + + '{0}' is not defined in the global scope + + + '{0}' is not defined in non-local scopes + + + An exception occured while discovering search paths; analysis will not be available. + + + Interpreter does not exist; analysis will not be available. + + + Analysis cache path: {0} + + + Environment variable '{0}' is not set, using the default cache location instead. + + + Path '{0}' is not rooted, using the default cache location instead. + + + Unable to determine analysis cache path. Using default '{0}'. + + + Unable to determine analysis cache path. Exception: {0}. Using default '{1}'. + + + The first argument to NewType must be a string, but it is of type '{0}'. + + + Explicit return in __init__ + + + Arguments to Generic must all be type parameters. + + + Arguments to Generic must all be unique. + +<<<<<<< HEAD +======= + + Duplicate argument name '{0}' in function definition. + +>>>>>>> master + diff --git a/src/Analysis/Ast/Test/LintNoQATests.cs b/src/Analysis/Ast/Test/LintNoQATests.cs index 92c1bf924..9de8a4d18 100644 --- a/src/Analysis/Ast/Test/LintNoQATests.cs +++ b/src/Analysis/Ast/Test/LintNoQATests.cs @@ -81,16 +81,15 @@ public async Task IgnoreNoQAUppercase(string code) { [TestMethod, Priority(0)] public async Task VarNamedNoQAStillGivesDiagnostic() { - const string code = @" -noqa = 1 + 'str' -"; + string code = GenericSetup + "NOQA = Generic[T, T]"; + var analysis = await GetAnalysisAsync(code); analysis.Diagnostics.Should().HaveCount(1); var diagnostic = analysis.Diagnostics.ElementAt(0); - diagnostic.ErrorCode.Should().Be(ErrorCodes.UnsupportedOperandType); - diagnostic.Message.Should().Be(Resources.UnsupporedOperandType.FormatInvariant("+", "int", "str")); - diagnostic.SourceSpan.Should().Be(2, 8, 2, 17); + diagnostic.Message.Should().Be(Resources.GenericNotAllUnique); + diagnostic.ErrorCode.Should().Be(ErrorCodes.TypingGenericArguments); + diagnostic.SourceSpan.Should().Be(8, 8, 8, 21); diagnostic.Severity.Should().Be(Severity.Error); } From 36c8908de4944fc9dad061c9c7ff8e57a67fb5b9 Mon Sep 17 00:00:00 2001 From: Cameron Trando Date: Mon, 15 Jul 2019 13:58:04 -0700 Subject: [PATCH 4/4] Removing orig --- src/Analysis/Ast/Impl/Resources.resx.orig | 210 ---------------------- 1 file changed, 210 deletions(-) delete mode 100644 src/Analysis/Ast/Impl/Resources.resx.orig diff --git a/src/Analysis/Ast/Impl/Resources.resx.orig b/src/Analysis/Ast/Impl/Resources.resx.orig deleted file mode 100644 index c4de44b0c..000000000 --- a/src/Analysis/Ast/Impl/Resources.resx.orig +++ /dev/null @@ -1,210 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - '{0}' may not be callable - - - object may not be callable - - - unresolved import '{0}' - - - Relative import '{0}' beyond top-level package - - - '{0}' used before definition - - - property of type {0} - - - property of unknown type - - - Parameter {0} already specified. - - - Parameter {0} is missing. - - - Positional arguments are not allowed after keyword argument. - - - Too many function arguments. - - - Too many positional arguments before '*' argument. - - - Unknown parameter name. - - - Analyzing in background, {0} items left... - - - Undefined variable: '{0}' - - - '{0}' is not defined in the global scope - - - '{0}' is not defined in non-local scopes - - - An exception occured while discovering search paths; analysis will not be available. - - - Interpreter does not exist; analysis will not be available. - - - Analysis cache path: {0} - - - Environment variable '{0}' is not set, using the default cache location instead. - - - Path '{0}' is not rooted, using the default cache location instead. - - - Unable to determine analysis cache path. Using default '{0}'. - - - Unable to determine analysis cache path. Exception: {0}. Using default '{1}'. - - - The first argument to NewType must be a string, but it is of type '{0}'. - - - Explicit return in __init__ - - - Arguments to Generic must all be type parameters. - - - Arguments to Generic must all be unique. - -<<<<<<< HEAD -======= - - Duplicate argument name '{0}' in function definition. - ->>>>>>> master -