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

Make sure tuple protocol name does not ignore unknown parameter types #206

Merged
merged 5 commits into from
Oct 5, 2018
Merged
Changes from 3 commits
Commits
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
48 changes: 41 additions & 7 deletions src/Analysis/Engine/Impl/Values/Protocols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.PythonTools.Analysis.Infrastructure;
using Microsoft.PythonTools.Interpreter;
using Microsoft.PythonTools.Parsing;
Expand Down Expand Up @@ -389,9 +390,41 @@ class TupleProtocol : IterableProtocol {

public TupleProtocol(ProtocolInfo self, IEnumerable<IAnalysisSet> values) : base(self, AnalysisSet.UnionAll(values)) {
_values = values.Select(s => s.AsUnion(1)).ToArray();
Name = GetNameFromValues();
}

var argTypes = _values.SelectMany(e => e.Select(v => v is IHasQualifiedName qn ? qn.FullyQualifiedName : v.ShortDescription));
Name = "tuple[{0}]".FormatInvariant(string.Join(", ", argTypes));
private string GetNameFromValues() {
// Enumerate manually since SelectMany drops empty/unknown values
var sb = new StringBuilder("tuple[");
for (var i = 0; i < _values.Length; i++) {
if (i > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

sb.Append(", ");
}
sb.Append(GetParameterString(_values[i].ToArray()));
}
sb.Append(']');
return sb.ToString();
}

private string GetParameterString(AnalysisValue[] sets) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you can reuse the same StringBuilder here.

Copy link
Author

Choose a reason for hiding this comment

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

Good point

if (sets.Length == 0) {
return "?";
}
var sb = new StringBuilder();
if (sets.Length > 1) {
Copy link
Contributor

@AlexanderSher AlexanderSher Oct 5, 2018

Choose a reason for hiding this comment

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

This can be simplified:

If (sets.Length == 1) {
    sb.Append(sets[0] is IHasQualifiedName qn ? qn.FullyQualifiedName : sets[0].ShortDescription)
} else {
    // remaining code
}

sb.Append('[');
}
for (var i = 0; i < sets.Length; i++) {
if (i > 0) {
sb.Append(", ");
Copy link
Contributor

Choose a reason for hiding this comment

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

", " or ","?

Copy link
Author

Choose a reason for hiding this comment

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

", " more readable

}
var desc = sets[i] is IHasQualifiedName qn ? qn.FullyQualifiedName : sets[i].ShortDescription;
sb.Append(desc);
}
if (sets.Length > 1) {
sb.Append(']');
}
return sb.ToString();
}

protected override void EnsureMembers(IDictionary<string, IAnalysisSet> members) {
Expand Down Expand Up @@ -419,6 +452,7 @@ public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet
}

public override string Name { get; }
public override BuiltinTypeId TypeId => BuiltinTypeId.Tuple;

public override IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
if (_values.Any()) {
Expand All @@ -437,7 +471,7 @@ public override IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
}
}

protected override bool Equals(Protocol other) =>
protected override bool Equals(Protocol other) =>
other is TupleProtocol tp &&
_values.Zip(tp._values, (x, y) => x.SetEquals(y)).All(b => b);

Expand Down Expand Up @@ -524,6 +558,7 @@ public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet
}

public override string Name => "dict";
public override BuiltinTypeId TypeId => BuiltinTypeId.Dict;

public override IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
if (_valueType.Any()) {
Expand Down Expand Up @@ -576,14 +611,13 @@ protected override void EnsureMembers(IDictionary<string, IAnalysisSet> members)
}

public override string Name => "generator";

public override BuiltinTypeId TypeId => BuiltinTypeId.Generator;

public IAnalysisSet Yielded => _yielded;
public IAnalysisSet Sent { get; }
public IAnalysisSet Returned { get; }

public override IAnalysisSet GetReturnForYieldFrom(Node node, AnalysisUnit unit) {
return Returned;
}
public override IAnalysisSet GetReturnForYieldFrom(Node node, AnalysisUnit unit) => Returned;

public override IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
if (_yielded.Any() || Sent.Any() || Returned.Any()) {
Expand Down