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 NewType is called and the first argument is not a string, make a diagnostic message #1260
Merged
CTrando
merged 5 commits into
microsoft:master
from
CTrando:scratch/NewTypeFirstArgNotString
Jul 3, 2019
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b25869a
Adding diagnostic error if the user calls NewType with the first arg …
CTrando 2980368
Cleaning up imports
CTrando 1e6b669
Editing error messages adding null checks
CTrando 3d8dda6
Adding source span check to tests
CTrando 1eed94a
Merge branch 'master' of github.com:microsoft/python-language-server …
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
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,128 @@ | ||
// 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.Core; | ||
using Microsoft.Python.Parsing.Tests; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using TestUtilities; | ||
|
||
namespace Microsoft.Python.Analysis.Tests { | ||
[TestClass] | ||
public class LintNewTypeTests : 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 NewTypeIntFirstArg() { | ||
const string code = @" | ||
from typing import NewType | ||
|
||
T = NewType(5, int) | ||
|
||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(1); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.NewTypeArguments); | ||
diagnostic.Message.Should().Be(Resources.NewTypeFirstArgNotString.FormatInvariant("int")); | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
[DataRow("float", "float")] | ||
[DataRow("int", "int")] | ||
[DataRow("complex", "str")] | ||
[DataTestMethod, Priority(0)] | ||
public async Task DifferentTypesFirstArg(string nameType, string type) { | ||
string code = $@" | ||
from typing import NewType | ||
|
||
T = NewType({nameType}(10), {type}) | ||
|
||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(1); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.NewTypeArguments); | ||
diagnostic.Message.Should().Be(Resources.NewTypeFirstArgNotString.FormatInvariant(nameType)); | ||
} | ||
|
||
[TestMethod, Priority(0)] | ||
public async Task ObjectFirstArg() { | ||
string code = $@" | ||
from typing import NewType | ||
|
||
class X: | ||
def hello(): | ||
pass | ||
|
||
h = X() | ||
|
||
T = NewType(h, int) | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(1); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.NewTypeArguments); | ||
diagnostic.Message.Should().Be(Resources.NewTypeFirstArgNotString.FormatInvariant("X")); | ||
} | ||
|
||
[TestMethod, Priority(0)] | ||
public async Task GenericFirstArg() { | ||
string code = $@" | ||
from typing import NewType, Generic, TypeVar | ||
|
||
T = TypeVar('T', str, int) | ||
|
||
class X(Generic[T]): | ||
def __init__(self, p: T): | ||
self.x = p | ||
|
||
h = X(5) | ||
T = NewType(h, int) | ||
"; | ||
var analysis = await GetAnalysisAsync(code); | ||
analysis.Diagnostics.Should().HaveCount(1); | ||
|
||
var diagnostic = analysis.Diagnostics.ElementAt(0); | ||
diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.NewTypeArguments); | ||
diagnostic.Message.Should().Be(Resources.NewTypeFirstArgNotString.FormatInvariant("X[int]")); | ||
} | ||
|
||
[DataRow("'test'", "float")] | ||
[DataRow("'testing'", "int")] | ||
[DataTestMethod, Priority(0)] | ||
public async Task NoDiagnosticOnStringFirstArg(string name, string type) { | ||
string code = $@" | ||
from typing import NewType | ||
|
||
T = NewType({name}, {type}) | ||
CTrando marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"; | ||
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.