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 133
When a class inherits from something that is not a class, give a diagnostic error #1277
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
721de14
When creating a class, give diagnostic if bases are not class types
CTrando eb1d4cd
Editing comments
CTrando a67d098
Fixing whitespace and return val
CTrando b794727
minor changes, removing test
CTrando a75c63a
Fixing build error
CTrando af8d0d9
switching to var
CTrando ef5b554
Return ienumerable
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
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,173 @@ | ||
// 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.Diagnostics; | ||
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 InheritNonClassTests : 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 InheritFromRenamedBuiltin() { | ||
const string code = @" | ||
tmp = str | ||
|
||
class C(tmp): | ||
def method(self): | ||
return 'test' | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().BeEmpty(); | ||
} | ||
|
||
|
||
[TestMethod, Priority(0)] | ||
public async Task InheritFromBuiltin() { | ||
const string code = @" | ||
class C(str): | ||
def method(self): | ||
return 'test' | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().BeEmpty(); | ||
} | ||
|
||
|
||
[TestMethod, Priority(0)] | ||
public async Task InheritFromUserClass() { | ||
const string code = @" | ||
class D: | ||
def hello(self): | ||
pass | ||
|
||
class C(D): | ||
def method(self): | ||
return 'test' | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().BeEmpty(); | ||
} | ||
|
||
|
||
[TestMethod, Priority(0)] | ||
public async Task InheritFromConstant() { | ||
const string code = @" | ||
class C(5): | ||
def method(self): | ||
return 'test' | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(1); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
diagnostic.SourceSpan.Should().Be(2, 7, 2, 8); | ||
diagnostic.Message.Should().Be(Resources.InheritNonClass.FormatInvariant("5")); | ||
diagnostic.ErrorCode.Should().Be(ErrorCodes.InheritNonClass); | ||
} | ||
|
||
|
||
[TestMethod, Priority(0)] | ||
public async Task InheritFromConstantVar() { | ||
const string code = @" | ||
x = 'str' | ||
|
||
class C(x): | ||
def method(self): | ||
return 'test' | ||
|
||
x = 5 | ||
|
||
class D(x): | ||
def method(self): | ||
return 'test' | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(2); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
diagnostic.SourceSpan.Should().Be(4, 7, 4, 8); | ||
diagnostic.Message.Should().Be(Resources.InheritNonClass.FormatInvariant("str")); | ||
diagnostic.ErrorCode.Should().Be(ErrorCodes.InheritNonClass); | ||
|
||
diagnostic = analysis.Diagnostics.ElementAt(1); | ||
diagnostic.SourceSpan.Should().Be(10, 7, 10, 8); | ||
diagnostic.Message.Should().Be(Resources.InheritNonClass.FormatInvariant("5")); | ||
diagnostic.ErrorCode.Should().Be(ErrorCodes.InheritNonClass); | ||
} | ||
|
||
[Ignore] | ||
[TestMethod, Priority(0)] | ||
public async Task InheritFromBinaryOp() { | ||
CTrando marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const string code = @" | ||
x = 5 | ||
|
||
class C(x + 2): | ||
def method(self): | ||
return 'test' | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(1); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
diagnostic.SourceSpan.Should().Be(4, 7, 4, 8); | ||
diagnostic.Message.Should().Be(Resources.InheritNonClass.FormatInvariant("x + 2")); | ||
diagnostic.ErrorCode.Should().Be(ErrorCodes.InheritNonClass); | ||
} | ||
|
||
|
||
[TestMethod, Priority(0)] | ||
public async Task InheritFromOtherModule() { | ||
const string code = @" | ||
import typing | ||
|
||
class C(typing.TypeVar): | ||
def method(self): | ||
return 'test' | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().BeEmpty(); | ||
} | ||
|
||
[TestMethod, Priority(0)] | ||
public async Task InheritFromRenamedOtherModule() { | ||
const string code = @" | ||
import typing | ||
|
||
tmp = typing.TypeVar | ||
class C(tmp): | ||
def method(self): | ||
return 'test' | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().BeEmpty(); | ||
} | ||
} | ||
} |
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.