Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Improve linting of LHS expressions #2070

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -53,9 +53,13 @@ public override bool Walk(SuiteStatement node) {
augs.Right?.Walk(new ExpressionWalker(this));
break;
case AssignmentStatement asst:
var lhs = asst.Left.MaybeEnumerate().ToArray();
if (lhs.Length > 0) {
lhs[0]?.Walk(new ExpressionWalker(this));
}
_suppressDiagnostics = true;
foreach (var lhs in asst.Left ?? Enumerable.Empty<Expression>()) {
lhs?.Walk(new ExpressionWalker(this));
foreach (var l in lhs.Skip(1)) {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure this is what I described, it's not about it being the first thing in a tuple, it's about it being the first part of a member or array access, like foo.bar or foo[bar] appearing somewhere on the left.

Copy link
Member

@jakebailey jakebailey Jun 10, 2020

Choose a reason for hiding this comment

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

Where foo is undefined, but bar is defined.

Copy link
Member

Choose a reason for hiding this comment

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

Unfortunate typo, meant foo not defined but bar defined.

Copy link
Author

Choose a reason for hiding this comment

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

That parts works OK i.e. x[y] detects undefined x or y. The remaining part of LHS is trickier since we don't know if RHS returns proper number of items.

Copy link
Member

Choose a reason for hiding this comment

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

I'll make a test for the case I'm thinking of and see if it works.

l?.Walk(new ExpressionWalker(this));
}
_suppressDiagnostics = false;
asst.Right?.Walk(new ExpressionWalker(this));
Expand All @@ -73,7 +77,7 @@ public void ReportUndefinedVariable(NameExpression node) {
ReportUndefinedVariable(node.Name, eval.GetLocation(node).Span);
}

public void ReportUndefinedVariable(string name, SourceSpan span) {
private void ReportUndefinedVariable(string name, SourceSpan span) {
if (!_suppressDiagnostics) {
_diagnostics.Add(new DiagnosticsEntry(
Resources.UndefinedVariable.FormatInvariant(name),
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
24 changes: 24 additions & 0 deletions src/Analysis/Ast/Test/LintUndefinedVarsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,30 @@ 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);
}

[TestMethod, Priority(0)]
public async Task UndefinedIndex() {
const string code = @"
x = [1, 2]
x[y] = 1
";
var d = await LintAsync(code);
d.Should().HaveCount(1);
d[0].ErrorCode.Should().Be(ErrorCodes.UndefinedVariable);
d[0].SourceSpan.Should().Be(3, 3, 3, 4);
}

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