This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
When returning in init method, report a diagnostic error #1261
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f1b234f
When returning in init method, report a diagnostic error
CTrando 5a3c55d
Adding check and tests for returning None before adding error for a r…
CTrando fd64805
Using BeEmpty() in tests
CTrando 0bd5e6b
Adding source span tests when adding diagnostics
CTrando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright(c) Microsoft Corporation | ||
| // All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the License); you may not use | ||
| // this file except in compliance with the License. You may obtain a copy of the | ||
| // License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
| // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY | ||
| // IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
| // MERCHANTABILITY OR NON-INFRINGEMENT. | ||
| // | ||
| // See the Apache Version 2.0 License for specific language governing | ||
| // permissions and limitations under the License. | ||
|
|
||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Microsoft.Python.Analysis.Tests.FluentAssertions; | ||
| using Microsoft.Python.Parsing; | ||
| using Microsoft.Python.Parsing.Tests; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using TestUtilities; | ||
|
|
||
| namespace Microsoft.Python.Analysis.Tests { | ||
| [TestClass] | ||
| public class LintReturnInInitTests : 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 ReturnInInit() { | ||
| const string code = @" | ||
| class Rectangle: | ||
| def __init__(self, width, height): | ||
| self.width = width | ||
| self.height = height | ||
| self.area = width * height | ||
| return self.area | ||
|
|
||
| r = Rectangle(10, 10) | ||
| "; | ||
| var analysis = await GetAnalysisAsync(code); | ||
| analysis.Diagnostics.Should().HaveCount(1); | ||
|
|
||
| var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
| diagnostic.SourceSpan.Should().Be(7, 9, 7, 25); | ||
| diagnostic.Severity.Should().Be(Severity.Warning); | ||
| diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.ReturnInInit); | ||
| diagnostic.Message.Should().Be(Resources.ReturnInInit); | ||
| } | ||
|
|
||
| [TestMethod, Priority(0)] | ||
| public async Task ReturnInitBasic() { | ||
| const string code = @" | ||
| class Rectangle: | ||
| def __init__(self, width, height): | ||
| return 2 | ||
|
|
||
| r = Rectangle(10, 10) | ||
| "; | ||
| var analysis = await GetAnalysisAsync(code); | ||
| analysis.Diagnostics.Should().HaveCount(1); | ||
|
|
||
| var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
| diagnostic.SourceSpan.Should().Be(4, 9, 4, 17); | ||
| diagnostic.Severity.Should().Be(Severity.Warning); | ||
| diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.ReturnInInit); | ||
| diagnostic.Message.Should().Be(Resources.ReturnInInit); | ||
| } | ||
|
|
||
| [TestMethod, Priority(0)] | ||
| public async Task ReturnInInitConditional() { | ||
| const string code = @" | ||
| class A: | ||
| def __init__(self, x): | ||
| self.x = x | ||
| if x > 0: | ||
| return 10 | ||
|
|
||
| a = A(1) | ||
| "; | ||
| var analysis = await GetAnalysisAsync(code); | ||
| analysis.Diagnostics.Should().HaveCount(1); | ||
|
|
||
| var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
| diagnostic.SourceSpan.Should().Be(6, 13, 6, 22); | ||
| diagnostic.Severity.Should().Be(Severity.Warning); | ||
| diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.ReturnInInit); | ||
| diagnostic.Message.Should().Be(Resources.ReturnInInit); | ||
| } | ||
|
|
||
| [TestMethod, Priority(0)] | ||
| public async Task ReturnNoneInInit() { | ||
| const string code = @" | ||
| class A: | ||
| def __init__(self, x): | ||
| self.x = x | ||
| self.x += 1 | ||
| return None | ||
|
|
||
| a = A(1) | ||
| "; | ||
| var analysis = await GetAnalysisAsync(code); | ||
| analysis.Diagnostics.Should().BeEmpty(); | ||
| } | ||
|
|
||
| [TestMethod, Priority(0)] | ||
| public async Task EmptyReturnInInit() { | ||
| const string code = @" | ||
| class A: | ||
| def __init__(self, x): | ||
| self.x = x | ||
| return | ||
| a = A(1) | ||
| "; | ||
| var analysis = await GetAnalysisAsync(code); | ||
| analysis.Diagnostics.Should().BeEmpty(); | ||
| } | ||
| } | ||
CTrando marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.