1717using System ;
1818using System . Collections . Generic ;
1919using System . Linq ;
20+ using System . Text ;
2021using Microsoft . PythonTools . Analysis . Infrastructure ;
2122using Microsoft . PythonTools . Interpreter ;
2223using Microsoft . PythonTools . Parsing ;
@@ -389,9 +390,32 @@ class TupleProtocol : IterableProtocol {
389390
390391 public TupleProtocol ( ProtocolInfo self , IEnumerable < IAnalysisSet > values ) : base ( self , AnalysisSet . UnionAll ( values ) ) {
391392 _values = values . Select ( s => s . AsUnion ( 1 ) ) . ToArray ( ) ;
393+ Name = GetNameFromValues ( ) ;
394+ }
395+
396+ private string GetNameFromValues ( ) {
397+ // Enumerate manually since SelectMany drops empty/unknown values
398+ var sb = new StringBuilder ( "tuple[" ) ;
399+ for ( var i = 0 ; i < _values . Length ; i ++ ) {
400+ sb . AppendIf ( i > 0 , ", " ) ;
401+ AppendParameterString ( sb , _values [ i ] . ToArray ( ) ) ;
402+ }
403+ sb . Append ( ']' ) ;
404+ return sb . ToString ( ) ;
405+ }
406+
407+ private void AppendParameterString ( StringBuilder sb , AnalysisValue [ ] sets ) {
408+ if ( sets . Length == 0 ) {
409+ sb . Append ( '?' ) ;
410+ return ;
411+ }
392412
393- var argTypes = _values . SelectMany ( e => e . Select ( v => v is IHasQualifiedName qn ? qn . FullyQualifiedName : v . ShortDescription ) ) ;
394- Name = "tuple[{0}]" . FormatInvariant ( string . Join ( ", " , argTypes ) ) ;
413+ sb . AppendIf ( sets . Length > 1 , "[" ) ;
414+ for ( var i = 0 ; i < sets . Length ; i ++ ) {
415+ sb . AppendIf ( i > 0 , ", " ) ;
416+ sb . Append ( sets [ i ] is IHasQualifiedName qn ? qn . FullyQualifiedName : sets [ i ] . ShortDescription ) ;
417+ }
418+ sb . AppendIf ( sets . Length > 1 , "]" ) ;
395419 }
396420
397421 protected override void EnsureMembers ( IDictionary < string , IAnalysisSet > members ) {
@@ -419,6 +443,7 @@ public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet
419443 }
420444
421445 public override string Name { get ; }
446+ public override BuiltinTypeId TypeId => BuiltinTypeId . Tuple ;
422447
423448 public override IEnumerable < KeyValuePair < string , string > > GetRichDescription ( ) {
424449 if ( _values . Any ( ) ) {
@@ -437,7 +462,7 @@ public override IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
437462 }
438463 }
439464
440- protected override bool Equals ( Protocol other ) =>
465+ protected override bool Equals ( Protocol other ) =>
441466 other is TupleProtocol tp &&
442467 _values . Zip ( tp . _values , ( x , y ) => x . SetEquals ( y ) ) . All ( b => b ) ;
443468
@@ -524,6 +549,7 @@ public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet
524549 }
525550
526551 public override string Name => "dict" ;
552+ public override BuiltinTypeId TypeId => BuiltinTypeId . Dict ;
527553
528554 public override IEnumerable < KeyValuePair < string , string > > GetRichDescription ( ) {
529555 if ( _valueType . Any ( ) ) {
@@ -576,14 +602,13 @@ protected override void EnsureMembers(IDictionary<string, IAnalysisSet> members)
576602 }
577603
578604 public override string Name => "generator" ;
579-
605+ public override BuiltinTypeId TypeId => BuiltinTypeId . Generator ;
606+
580607 public IAnalysisSet Yielded => _yielded ;
581608 public IAnalysisSet Sent { get ; }
582609 public IAnalysisSet Returned { get ; }
583610
584- public override IAnalysisSet GetReturnForYieldFrom ( Node node , AnalysisUnit unit ) {
585- return Returned ;
586- }
611+ public override IAnalysisSet GetReturnForYieldFrom ( Node node , AnalysisUnit unit ) => Returned ;
587612
588613 public override IEnumerable < KeyValuePair < string , string > > GetRichDescription ( ) {
589614 if ( _yielded . Any ( ) || Sent . Any ( ) || Returned . Any ( ) ) {
0 commit comments