17
17
using System ;
18
18
using System . Collections . Generic ;
19
19
using System . Linq ;
20
+ using System . Text ;
20
21
using Microsoft . PythonTools . Analysis . Infrastructure ;
21
22
using Microsoft . PythonTools . Interpreter ;
22
23
using Microsoft . PythonTools . Parsing ;
@@ -389,9 +390,32 @@ class TupleProtocol : IterableProtocol {
389
390
390
391
public TupleProtocol ( ProtocolInfo self , IEnumerable < IAnalysisSet > values ) : base ( self , AnalysisSet . UnionAll ( values ) ) {
391
392
_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
+ }
392
412
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 , "]" ) ;
395
419
}
396
420
397
421
protected override void EnsureMembers ( IDictionary < string , IAnalysisSet > members ) {
@@ -419,6 +443,7 @@ public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet
419
443
}
420
444
421
445
public override string Name { get ; }
446
+ public override BuiltinTypeId TypeId => BuiltinTypeId . Tuple ;
422
447
423
448
public override IEnumerable < KeyValuePair < string , string > > GetRichDescription ( ) {
424
449
if ( _values . Any ( ) ) {
@@ -437,7 +462,7 @@ public override IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
437
462
}
438
463
}
439
464
440
- protected override bool Equals ( Protocol other ) =>
465
+ protected override bool Equals ( Protocol other ) =>
441
466
other is TupleProtocol tp &&
442
467
_values . Zip ( tp . _values , ( x , y ) => x . SetEquals ( y ) ) . All ( b => b ) ;
443
468
@@ -524,6 +549,7 @@ public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet
524
549
}
525
550
526
551
public override string Name => "dict" ;
552
+ public override BuiltinTypeId TypeId => BuiltinTypeId . Dict ;
527
553
528
554
public override IEnumerable < KeyValuePair < string , string > > GetRichDescription ( ) {
529
555
if ( _valueType . Any ( ) ) {
@@ -576,14 +602,13 @@ protected override void EnsureMembers(IDictionary<string, IAnalysisSet> members)
576
602
}
577
603
578
604
public override string Name => "generator" ;
579
-
605
+ public override BuiltinTypeId TypeId => BuiltinTypeId . Generator ;
606
+
580
607
public IAnalysisSet Yielded => _yielded ;
581
608
public IAnalysisSet Sent { get ; }
582
609
public IAnalysisSet Returned { get ; }
583
610
584
- public override IAnalysisSet GetReturnForYieldFrom ( Node node , AnalysisUnit unit ) {
585
- return Returned ;
586
- }
611
+ public override IAnalysisSet GetReturnForYieldFrom ( Node node , AnalysisUnit unit ) => Returned ;
587
612
588
613
public override IEnumerable < KeyValuePair < string , string > > GetRichDescription ( ) {
589
614
if ( _yielded . Any ( ) || Sent . Any ( ) || Returned . Any ( ) ) {
0 commit comments