Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c6a9c22
Remove stale reference
Sep 30, 2019
1360827
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 1, 2019
ccaaa02
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 4, 2019
da40dcc
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 4, 2019
c348ac3
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 5, 2019
53bc044
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 7, 2019
484a92a
Merge branch 'master' of https://github.com/MikhailArkhipov/python-la…
Oct 7, 2019
e6df3aa
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 8, 2019
1d289d8
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 8, 2019
126f355
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 12, 2019
7e715f3
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 25, 2019
32923a5
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 31, 2019
1b72f4b
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 2, 2019
f74d5b6
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 7, 2019
0b28fa4
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 12, 2019
6109ac7
Don't suppress LHS diagnostics on augmented assign
Nov 12, 2019
bcfc3b7
Revert "Don't suppress LHS diagnostics on augmented assign"
Nov 12, 2019
dc286b8
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 13, 2019
0aab4f5
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 16, 2019
b953ce7
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 18, 2019
aef887d
Merge branch 'master' of https://github.com/microsoft/python-language…
Dec 10, 2019
b97b641
Merge branch 'master' of https://github.com/microsoft/python-language…
Dec 11, 2019
c16646d
Escape [ and ]
Dec 13, 2019
f3e08d5
Merge branch 'master' of https://github.com/MikhailArkhipov/python-la…
Dec 13, 2019
5700642
PR feedback
Dec 13, 2019
1d091db
Merge branch 'master' of https://github.com/microsoft/python-language…
Dec 13, 2019
b8c3615
Merge branch 'master' of https://github.com/microsoft/python-language…
Dec 16, 2019
6a88069
Merge branch 'master' of https://github.com/microsoft/python-language…
Jan 28, 2020
078c975
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 12, 2020
ac07704
Merge master
Mar 18, 2020
40be62b
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 19, 2020
b384853
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 20, 2020
a0632ef
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 26, 2020
c9b72bd
Merge branch 'master' of https://github.com/microsoft/python-language…
Mar 26, 2020
1e8291b
Merge branch 'master' of https://github.com/microsoft/python-language…
Apr 17, 2020
94db074
Improve linting of LHS expressions
Jun 9, 2020
574c565
PR feedback
Jun 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Linq;
using Microsoft.Python.Analysis.Analyzer;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Text;
using Microsoft.Python.Parsing.Ast;
using Microsoft.Python.Parsing.Extensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace Microsoft.Python.Analysis.Linting.UndefinedVariables {
internal sealed class UndefinedVariablesWalker : LinterWalker {
private readonly List<DiagnosticsEntry> _diagnostics = new List<DiagnosticsEntry>();
private bool _suppressDiagnostics;

public UndefinedVariablesWalker(IDocumentAnalysis analysis, IServiceContainer services)
: base(analysis, services) { }
Expand All @@ -53,11 +52,9 @@ public override bool Walk(SuiteStatement node) {
augs.Right?.Walk(new ExpressionWalker(this));
break;
case AssignmentStatement asst:
_suppressDiagnostics = true;
foreach (var lhs in asst.Left ?? Enumerable.Empty<Expression>()) {
foreach (var lhs in asst.Left.MaybeEnumerate().Where(x => !(x is NameExpression))) {
Copy link
Member

@jakebailey jakebailey Jun 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What affect does this have for an assignment like:

def foo():
    return 1, 2

x = 1234
x, y = foo()

Where y has not been defined but is being walked as a name expression?

I'm thinking that the original solution was "more correct" in that it's trying to avoid lvars being linted, but needed an extra case where it stored _suppressDiagnostics and made it false before doing the first member access (to check the first thing), and then restored it.

lhs?.Walk(new ExpressionWalker(this));
}
_suppressDiagnostics = false;
asst.Right?.Walk(new ExpressionWalker(this));
break;
default:
Expand All @@ -73,12 +70,10 @@ public void ReportUndefinedVariable(NameExpression node) {
ReportUndefinedVariable(node.Name, eval.GetLocation(node).Span);
}

public void ReportUndefinedVariable(string name, SourceSpan span) {
if (!_suppressDiagnostics) {
_diagnostics.Add(new DiagnosticsEntry(
Resources.UndefinedVariable.FormatInvariant(name),
span, ErrorCodes.UndefinedVariable, Severity.Warning, DiagnosticSource.Linter));
}
private void ReportUndefinedVariable(string name, SourceSpan span) {
_diagnostics.Add(new DiagnosticsEntry(
Resources.UndefinedVariable.FormatInvariant(name),
span, ErrorCodes.UndefinedVariable, Severity.Warning, DiagnosticSource.Linter));
}

private void HandleGlobal(GlobalStatement node) {
Expand Down
1 change: 0 additions & 1 deletion src/Analysis/Ast/Test/LintNoSelfArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;

Expand Down
1 change: 0 additions & 1 deletion src/Analysis/Ast/Test/LintReturnInInitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using FluentAssertions;
using Microsoft.Python.Analysis.Tests.FluentAssertions;
using Microsoft.Python.Parsing;
using Microsoft.Python.Parsing.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;

Expand Down
12 changes: 12 additions & 0 deletions src/Analysis/Ast/Test/LintUndefinedVarsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,18 @@ public async Task AugmentedAssignToUndefined() {
d[0].SourceSpan.Should().Be(2, 1, 2, 2);
}

[TestMethod, Priority(0)]
public async Task UndefinedInInit() {
const string code = @"
class TestIt():
def __init__(self):
slf.y = 6
";
var d = await LintAsync(code);
d.Should().HaveCount(1);
d[0].ErrorCode.Should().Be(ErrorCodes.UndefinedVariable);
d[0].SourceSpan.Should().Be(4, 9, 4, 12);
}

private async Task<IReadOnlyList<DiagnosticsEntry>> LintAsync(string code, InterpreterConfiguration configuration = null) {
var analysis = await GetAnalysisAsync(code, configuration ?? PythonVersions.LatestAvailable3X);
Expand Down