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
Make sure tuple protocol name does not ignore unknown parameter types #206
Merged
Merged
Changes from 3 commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) { | ||
sb.Append(", "); | ||
} | ||
sb.Append(GetParameterString(_values[i].ToArray())); | ||
} | ||
sb.Append(']'); | ||
return sb.ToString(); | ||
} | ||
|
||
private string GetParameterString(AnalysisValue[] sets) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can reuse the same There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(", "); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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()) { | ||
|
@@ -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); | ||
|
||
|
@@ -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()) { | ||
|
@@ -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()) { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can have an extension method for this:
https://github.com/Microsoft/RTVS/blob/master/src/Common/Core/Impl/Extensions/StringBuilderExtensions.cs