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
Give Diagnostic messages on improper usage of Generic #1248
Merged
CTrando
merged 10 commits into
microsoft:master
from
CTrando:scratch/ExpressionEvalGeneric
Jul 2, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a5faba0
Adding diagnostic message for improperly using Generic
CTrando 49adedb
Adding tests to make sure no diagnostics on valid uses of Generic
CTrando e150ff7
Adding error message on no arguments to Generic
CTrando 1256e51
Revert "Adding error message on no arguments to Generic"
CTrando 0fbd6c7
Case of Generic[int] where there are no generic type parameters now w…
CTrando 1650173
Prefixing error code with typing
CTrando 3a63bb8
Adding span check in test
CTrando 441532a
Fixing imports in tests
CTrando 6735762
Removing generic error msg
CTrando 83e4aff
Removing .orig files
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,86 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.Python.Analysis.Tests.FluentAssertions; | ||
using Microsoft.Python.Parsing.Tests; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using TestUtilities; | ||
|
||
|
||
namespace Microsoft.Python.Analysis.Tests { | ||
|
||
[TestClass] | ||
public class LintGenericTests : AnalysisTestBase { | ||
|
||
public const string GenericSetup = @" | ||
from typing import Generic, TypeVar | ||
T = TypeVar('T', int, str) | ||
T1 = TypeVar('T1', int, str) | ||
|
||
_X = TypeVar('_X', str, int) | ||
_T = _X | ||
"; | ||
|
||
public TestContext TestContext { get; set; } | ||
|
||
[TestInitialize] | ||
public void TestInitialize() | ||
=> TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}"); | ||
|
||
[TestCleanup] | ||
public void Cleanup() => TestEnvironmentImpl.TestCleanup(); | ||
|
||
[DataRow("x = Generic[T, str]")] | ||
[DataRow("x = Generic[T, T1, int]")] | ||
[DataRow("x = Generic[T, str, int, T1]")] | ||
[DataRow("x = Generic[str, int]")] | ||
[DataRow("x = Generic[str]")] | ||
[DataTestMethod, Priority(0)] | ||
public async Task GenericNotAllTypeParameters(string decl) { | ||
string code = GenericSetup + decl; | ||
|
||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(1); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
var start = decl.IndexOf("Generic") + 1; | ||
// adding 1 because SourceSpan.End is exclusive and another 1 because SourceSpan is 1-indexed | ||
var end = decl.IndexOf("]", start) + 2; | ||
|
||
diagnostic.SourceSpan.Should().Be(8, start, 8, end); | ||
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.TypingGenericArguments); | ||
diagnostic.Message.Should().Be(Resources.GenericNotAllTypeParameters); | ||
} | ||
|
||
[DataRow("x = Generic[_T, _X]")] | ||
[DataRow("x = Generic[_T, T, T1, _X]")] | ||
[DataRow("x = Generic[_T,_T, T]")] | ||
[DataRow("x = Generic[T,T]")] | ||
[DataTestMethod, Priority(0)] | ||
public async Task GenericDuplicateArguments(string decl) { | ||
string code = GenericSetup + decl; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(1); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
var start = decl.IndexOf("Generic") + 1; | ||
// adding 1 because SourceSpan.End is exclusive and another 1 because SourceSpan is 1-indexed | ||
var end = decl.IndexOf("]", start) + 2; | ||
diagnostic.SourceSpan.Should().Be(8, start, 8, end); | ||
|
||
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.TypingGenericArguments); | ||
diagnostic.Message.Should().Be(Resources.GenericNotAllUnique); | ||
CTrando marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
[DataRow("x = Generic[_X, T]")] | ||
[DataRow("x = Generic[T1, T]")] | ||
[DataRow("x = Generic[T]")] | ||
[DataRow("x = Generic[T,T1, _X]")] | ||
[DataTestMethod, Priority(0)] | ||
public async Task GenericArgumentsNoDiagnosticOnValid(string decl) { | ||
string code = GenericSetup + decl; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(0); | ||
} | ||
} | ||
} |
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.