Skip to content

Commit 6dbc4aa

Browse files
committed
Adding error message on no arguments to Generic
1 parent a1c14a7 commit 6dbc4aa

4 files changed

Lines changed: 57 additions & 22 deletions

File tree

src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Generics.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,24 @@ private IMember GetValueFromGeneric(IMember target, Expression expr) {
6565
/// <summary>
6666
/// Returns whether the arguments to Generic are valid
6767
/// </summary>
68-
private bool GenericClassParameterValid(IGenericTypeDefinition[] genericTypeArgs, IReadOnlyList<IMember> indices, Expression expr) {
69-
if (genericTypeArgs.Length == 0) {
68+
private bool GenericClassParameterValid(IReadOnlyList<IGenericTypeDefinition> genericTypeArgs, IReadOnlyList<IMember> indices, Expression expr) {
69+
// Cannot have Generic[]
70+
if (genericTypeArgs.Count == 0) {
71+
ReportDiagnostics(Module.Uri, new DiagnosticsEntry(
72+
Resources.GenericTooFewArguments,
73+
GetLocation(expr).Span,
74+
ErrorCodes.GenericArguments,
75+
Severity.Error,
76+
DiagnosticSource.Analysis));
77+
7078
return false;
7179
}
7280

7381
// All arguments to Generic must be type parameters
7482
// e.g. Generic[T, str] throws a runtime error
75-
if (genericTypeArgs.Length != indices.Count) {
83+
if (genericTypeArgs.Count != indices.Count) {
7684
ReportDiagnostics(Module.Uri, new DiagnosticsEntry(
77-
Resources.GenericArgumentsNotAllTypeParameters,
85+
Resources.GenericNotAllTypeParameters,
7886
GetLocation(expr).Span,
7987
ErrorCodes.GenericArguments,
8088
Severity.Error,
@@ -83,9 +91,9 @@ private bool GenericClassParameterValid(IGenericTypeDefinition[] genericTypeArgs
8391
}
8492

8593
// All arguments to Generic must be distinct
86-
if (genericTypeArgs.Distinct().Count() != genericTypeArgs.Length) {
94+
if (genericTypeArgs.Distinct().Count() != genericTypeArgs.Count) {
8795
ReportDiagnostics(Module.Uri, new DiagnosticsEntry(
88-
Resources.GenericArgumentsNotAllUnique,
96+
Resources.GenericNotAllUnique,
8997
GetLocation(expr).Span,
9098
ErrorCodes.GenericArguments,
9199
Severity.Error,

src/Analysis/Ast/Impl/Resources.Designer.cs

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analysis/Ast/Impl/Resources.resx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,13 @@
189189
<data name="UnableToDetermineCachePathException" xml:space="preserve">
190190
<value>Unable to determine analysis cache path. Exception: {0}. Using default '{1}'.</value>
191191
</data>
192-
<data name="GenericArgumentsNotAllTypeParameters" xml:space="preserve">
192+
<data name="GenericNotAllTypeParameters" xml:space="preserve">
193193
<value>Arguments to Generic must all be type parameters.</value>
194194
</data>
195-
<data name="GenericArgumentsNotAllUnique" xml:space="preserve">
195+
<data name="GenericNotAllUnique" xml:space="preserve">
196196
<value>Arguments to Generic must all be unique.</value>
197197
</data>
198+
<data name="GenericTooFewArguments" xml:space="preserve">
199+
<value>Not enough arguments for Generic.</value>
200+
</data>
198201
</root>

src/Analysis/Ast/Test/LintGenericTests.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
33
using System.Linq;
4-
using System.Text;
54
using System.Threading.Tasks;
6-
using FluentAssertions;
7-
using Microsoft.VisualStudio.TestTools.UnitTesting;
8-
using NSubstitute;
95
using TestUtilities;
106

117
namespace Microsoft.Python.Analysis.Tests {
@@ -27,6 +23,25 @@ public void TestInitialize()
2723
[TestCleanup]
2824
public void Cleanup() => TestEnvironmentImpl.TestCleanup();
2925

26+
[DataRow(GenericSetup + @"
27+
class Map(Generic[]):
28+
def hello():
29+
pass
30+
")]
31+
[DataRow(GenericSetup + @"
32+
G = Generic[]
33+
")]
34+
[DataTestMethod, Priority(0)]
35+
public async Task GenericTooFewArguments(string code) {
36+
var analysis = await GetAnalysisAsync(code);
37+
analysis.Diagnostics.Should().HaveCount(1);
38+
39+
var diagnostic = analysis.Diagnostics.ElementAt(0);
40+
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.GenericArguments);
41+
diagnostic.Message.Should().Be(Resources.GenericTooFewArguments);
42+
}
43+
44+
3045
[DataRow(GenericSetup + @"
3146
class Map(Generic[T, str]):
3247
def hello():
@@ -43,13 +58,13 @@ def hello():
4358
pass
4459
")]
4560
[DataTestMethod, Priority(0)]
46-
public async Task GenericArgumentsNotAllTypeParameters(string code) {
61+
public async Task GenericNotALlTypParameters(string code) {
4762
var analysis = await GetAnalysisAsync(code);
4863
analysis.Diagnostics.Should().HaveCount(1);
4964

5065
var diagnostic = analysis.Diagnostics.ElementAt(0);
5166
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.GenericArguments);
52-
diagnostic.Message.Should().Be(Resources.GenericArgumentsNotAllTypeParameters);
67+
diagnostic.Message.Should().Be(Resources.GenericNotAllTypeParameters);
5368
}
5469

5570
[DataRow(GenericSetup + @"
@@ -82,13 +97,13 @@ def hello():
8297
pass
8398
")]
8499
[DataTestMethod, Priority(0)]
85-
public async Task GenericArgumentsDuplicate(string code) {
100+
public async Task GenericDuplicateArguments(string code) {
86101
var analysis = await GetAnalysisAsync(code);
87102
analysis.Diagnostics.Should().HaveCount(1);
88103

89104
var diagnostic = analysis.Diagnostics.ElementAt(0);
90105
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.GenericArguments);
91-
diagnostic.Message.Should().Be(Resources.GenericArgumentsNotAllUnique);
106+
diagnostic.Message.Should().Be(Resources.GenericNotAllUnique);
92107
}
93108

94109
[DataRow(GenericSetup + @"

0 commit comments

Comments
 (0)