Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit deb04da

Browse files
authored
Switches indexing from using an object parameter to an argument set parameter (microsoft#1266)
* Switching out index calls to use ArgumentSet instead of object as index parameter
1 parent c2c97ef commit deb04da

24 files changed

+58
-35
lines changed

src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Collections.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public IMember GetValueFromIndex(IndexExpression expr) {
4949
}
5050
var index = GetValueFromExpression(expr.Index);
5151
if (index != null) {
52-
return type.Index(instance, index);
52+
return type.Index(instance, new ArgumentSet(new []{index}, expr, this));
5353
}
5454
}
5555

src/Analysis/Ast/Impl/Modules/PythonModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ internal PythonModule(ModuleCreationOptions creationOptions, IServiceContainer s
125125
public IMember CreateInstance(string typeName, IArgumentSet args) => this;
126126
public override PythonMemberType MemberType => PythonMemberType.Module;
127127
public IMember Call(IPythonInstance instance, string memberName, IArgumentSet args) => GetMember(memberName);
128-
public IMember Index(IPythonInstance instance, object index) => Interpreter.UnknownType;
128+
public IMember Index(IPythonInstance instance, IArgumentSet args) => Interpreter.UnknownType;
129129

130130
public virtual string Documentation {
131131
get {

src/Analysis/Ast/Impl/Modules/PythonVariableModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public PythonVariableModule(IPythonModule module): base(module) {
6767
public IEnumerable<string> GetMemberNames() => Module != null ? Module.GetMemberNames().Concat(_children.Keys) : _children.Keys;
6868

6969
public IMember Call(IPythonInstance instance, string memberName, IArgumentSet args) => GetMember(memberName);
70-
public IMember Index(IPythonInstance instance, object index) => Interpreter.UnknownType;
70+
public IMember Index(IPythonInstance instance, IArgumentSet args) => Interpreter.UnknownType;
7171
public IMember CreateInstance(string typeName = null, IArgumentSet args = null) => this;
7272

7373
public bool Equals(IPythonModule other) => other is PythonVariableModule module && Name.EqualsOrdinal(module.Name);

src/Analysis/Ast/Impl/Specializations/Typing/Types/AnyType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public IMember Call(IPythonInstance instance, string memberName, IArgumentSet ar
3737
public IMember GetMember(string name) => null;
3838
public IEnumerable<string> GetMemberNames() => Array.Empty<string>();
3939

40-
public IMember Index(IPythonInstance instance, object index)
40+
public IMember Index(IPythonInstance instance, IArgumentSet args)
4141
=> DeclaringModule.Interpreter.UnknownType;
4242
}
4343
}

src/Analysis/Ast/Impl/Specializations/Typing/Types/GenericType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public IMember CreateInstance(string typeName, IArgumentSet args) {
100100
}
101101

102102
public virtual IMember Call(IPythonInstance instance, string memberName, IArgumentSet args) => DeclaringModule.Interpreter.UnknownType;
103-
public virtual IMember Index(IPythonInstance instance, object index) => DeclaringModule.Interpreter.UnknownType;
103+
public virtual IMember Index(IPythonInstance instance, IArgumentSet args) => DeclaringModule.Interpreter.UnknownType;
104104

105105
public IPythonType CreateSpecificType(IArgumentSet typeArguments)
106106
=> CreateSpecificType(typeArguments.Arguments.Select(a => a.Value).OfType<IPythonType>().ToArray());

src/Analysis/Ast/Impl/Specializations/Typing/Types/TypingDictionaryType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public TypingDictionaryType(string name, IPythonType keyType, IPythonType valueT
4747
public override string Name { get; }
4848

4949
public override IMember CreateInstance(string typeName, IArgumentSet args) => new TypingDictionary(this);
50-
public override IMember Index(IPythonInstance instance, object index) => new PythonInstance(ValueType);
50+
public override IMember Index(IPythonInstance instance, IArgumentSet args) => new PythonInstance(ValueType);
5151
public override bool IsSpecialized => true;
5252

5353
private TypingTupleType CreateItemType() {

src/Analysis/Ast/Impl/Specializations/Typing/Types/TypingListType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public TypingListType(string typeName, BuiltinTypeId typeId, IPythonType itemTyp
5353
public override IMember CreateInstance(string typeName, IArgumentSet args) => new TypingList(this);
5454
public IPythonType ItemType { get; }
5555

56-
public override IMember Index(IPythonInstance instance, object index) => new PythonInstance(ItemType);
56+
public override IMember Index(IPythonInstance instance, IArgumentSet args) => new PythonInstance(ItemType);
5757

5858
public override bool Equals(object obj) {
5959
if (!(obj is TypingListType other)) {

src/Analysis/Ast/Impl/Specializations/Typing/Types/TypingTupleType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public TypingTupleType(IReadOnlyList<IPythonType> itemTypes, IPythonInterpreter
4444
public override IMember CreateInstance(string typeName, IArgumentSet args)
4545
=> new TypingTuple(this);
4646

47-
public override IMember Index(IPythonInstance instance, object index) {
48-
var n = PythonCollection.GetIndex(index);
47+
public override IMember Index(IPythonInstance instance, IArgumentSet args) {
48+
var n = PythonCollection.GetIndex(args);
4949
if (n < 0) {
5050
n = ItemTypes.Count + n; // -1 means last, etc.
5151
}

src/Analysis/Ast/Impl/Specializations/Typing/Values/TypingDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override IPythonIterator GetIterator() {
3737
return new TypingIterator(iteratorType, this);
3838
}
3939

40-
public override IMember Index(object key) => new PythonInstance(_dictType.ValueType);
40+
public override IMember Index(IArgumentSet args) => new PythonInstance(_dictType.ValueType);
4141

4242
public override IMember Call(string memberName, IArgumentSet args) {
4343
// Specializations

src/Analysis/Ast/Impl/Specializations/Typing/Values/TypingList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public override IPythonIterator GetIterator() {
3434
return new TypingIterator(iteratorType, this);
3535
}
3636

37-
public override IMember Index(object index) => _collectionType.Index(this, index);
37+
public override IMember Index(IArgumentSet args) => _collectionType.Index(this, args);
3838
}
3939
}

src/Analysis/Ast/Impl/Specializations/Typing/Values/TypingTuple.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override IPythonIterator GetIterator() {
3232
return new TypingIterator(iteratorType, this);
3333
}
3434

35-
public override IMember Index(object index)
36-
=> _collectionType.Index(this, index).GetPythonType().CreateInstance(null, ArgumentSet.WithoutContext);
35+
public override IMember Index(IArgumentSet args)
36+
=> _collectionType.Index(this, args).GetPythonType().CreateInstance(null, ArgumentSet.Empty(args.Expression, args.Eval));
3737
}
3838
}

src/Analysis/Ast/Impl/Specializations/Typing/Values/TypingType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public TypingType(IPythonModule declaringModule, IPythonType type): base(declari
4242
public IMember CreateInstance(string typeName, IArgumentSet args) => _type;
4343
public IMember GetMember(string name) => _type.GetMember(name);
4444
public IEnumerable<string> GetMemberNames() => _type.GetMemberNames();
45-
public IMember Index(IPythonInstance instance, object index) => _type.Index(instance, index);
45+
public IMember Index(IPythonInstance instance, IArgumentSet args) => _type.Index(instance, args);
4646
}
4747
}

src/Analysis/Ast/Impl/Types/Collections/PythonCollectionType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public override IMember CreateInstance(string typeName, IArgumentSet args)
7878
public override IMember Call(IPythonInstance instance, string memberName, IArgumentSet args)
7979
=> DeclaringModule.Interpreter.GetBuiltinType(TypeId)?.Call(instance, memberName, args);
8080

81-
public override IMember Index(IPythonInstance instance, object index)
82-
=> (instance as IPythonCollection)?.Index(index) ?? UnknownType;
81+
public override IMember Index(IPythonInstance instance, IArgumentSet args)
82+
=> (instance as IPythonCollection)?.Index(args) ?? UnknownType;
8383
#endregion
8484

8585

src/Analysis/Ast/Impl/Types/Definitions/IPythonType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ public interface IPythonType : ILocatedMember, IMemberContainer {
7272
/// </summary>
7373
/// <param name="instance">Instance of the type.</param>
7474
/// <param name="index">Index arguments.</param>
75-
IMember Index(IPythonInstance instance, object index);
75+
IMember Index(IPythonInstance instance, IArgumentSet args);
7676
}
7777
}

src/Analysis/Ast/Impl/Types/PythonClassType.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ public override IMember CreateInstance(string typeName, IArgumentSet args) {
151151
return new PythonInstance(this);
152152
}
153153

154-
public override IMember Index(IPythonInstance instance, object index) {
155-
var defaultReturn = base.Index(instance, index);
154+
public override IMember Index(IPythonInstance instance, IArgumentSet args) {
155+
var defaultReturn = base.Index(instance, args);
156156
var fromBases = Bases
157157
.MaybeEnumerate()
158-
.Select(b => b.Index(instance, index))
158+
.Select(b => b.Index(instance, args))
159159
.Except(new[] { defaultReturn, UnknownType })
160160
.FirstOrDefault();
161161

src/Analysis/Ast/Impl/Types/PythonType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public virtual IMember Call(IPythonInstance instance, string memberName, IArgume
8484
/// </summary>
8585
/// <param name="instance">Instance of the type.</param>
8686
/// <param name="index">Index arguments.</param>
87-
public virtual IMember Index(IPythonInstance instance, object index) => instance?.Index(index) ?? UnknownType;
87+
public virtual IMember Index(IPythonInstance instance, IArgumentSet args) => instance?.Index(args) ?? UnknownType;
8888
#endregion
8989

9090
#region IMemberContainer

src/Analysis/Ast/Impl/Types/PythonTypeWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public virtual IMember CreateInstance(string typeName, IArgumentSet args)
6969
=> IsAbstract ? null : InnerType.CreateInstance(typeName, args);
7070
public virtual IMember Call(IPythonInstance instance, string memberName, IArgumentSet args)
7171
=> InnerType.Call(instance, memberName, args);
72-
public virtual IMember Index(IPythonInstance instance, object index)
73-
=> InnerType.Index(instance, index);
72+
public virtual IMember Index(IPythonInstance instance, IArgumentSet args)
73+
=> InnerType.Index(instance, args);
7474
#endregion
7575

7676
#region ILocatedMember

src/Analysis/Ast/Impl/Types/PythonUnionType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ public IMember Call(IPythonInstance instance, string memberName, IArgumentSet ar
7777
return result ?? DeclaringModule.Interpreter.UnknownType;
7878
}
7979

80-
public IMember Index(IPythonInstance instance, object index) {
80+
public IMember Index(IPythonInstance instance, IArgumentSet args) {
8181
IPythonType[] types;
8282
lock (_lock) {
8383
types = _types.ToArray();
8484
}
8585
// Check if any types support indexing
8686
var result = types
87-
.Select(t => t.Index(instance, index))
87+
.Select(t => t.Index(instance, args))
8888
.FirstOrDefault(r => !r.IsUnknown() && r.GetPythonType() != this);
8989
return result ?? DeclaringModule.Interpreter.UnknownType;
9090
}

src/Analysis/Ast/Impl/Values/Collections/PythonCollection.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public PythonCollection(
4646
/// <summary>
4747
/// Invokes indexer the instance.
4848
/// </summary>
49-
public override IMember Index(object index) {
50-
var n = GetIndex(index);
49+
public override IMember Index(IArgumentSet args) {
50+
var n = GetIndex(args);
5151
if (n < 0) {
5252
n = Contents.Count + n; // -1 means last, etc.
5353
}
@@ -60,8 +60,14 @@ public override IMember Index(object index) {
6060
public IReadOnlyList<IMember> Contents { get; protected set; }
6161
public override IPythonIterator GetIterator() => new PythonIterator(BuiltinTypeId.ListIterator, this);
6262

63-
public static int GetIndex(object index) {
64-
switch (index) {
63+
public static int GetIndex(IArgumentSet args) {
64+
// syntax error
65+
if (args.Arguments.Count != 1) {
66+
return 0;
67+
}
68+
69+
var arg = args.Arguments[0].Value;
70+
switch (arg) {
6571
case IPythonConstant c when c.Type.TypeId == BuiltinTypeId.Int || c.Type.TypeId == BuiltinTypeId.Long:
6672
return (int)c.Value;
6773
case int i:

src/Analysis/Ast/Impl/Values/Collections/PythonDictionary.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,24 @@ public IReadOnlyList<IPythonCollection> Items
6464
public override IPythonIterator GetIterator() =>
6565
Call(@"iterkeys", ArgumentSet.WithoutContext) as IPythonIterator ?? new EmptyIterator(Type.DeclaringModule.Interpreter.UnknownType);
6666

67-
public override IMember Index(object key) => key is IMember m ? this[m] : UnknownType;
67+
public override IMember Index(IArgumentSet args) {
68+
if (args.Arguments.Count == 1) {
69+
return args.Arguments[0].Value is IMember m ? this[m] : UnknownType;
70+
}
71+
return UnknownType;
72+
}
6873

6974
public override IMember Call(string memberName, IArgumentSet args) {
7075
// Specializations
7176
switch (memberName) {
7277
case @"get":
73-
return args.Arguments.Count > 1 ? Index(args.Arguments[1].Value) : _interpreter.UnknownType;
78+
// d = {}
79+
// d.get("test", 3.14), 3.14 is the default value so we infer the type of the return from it
80+
if (args.Arguments.Count > 1) {
81+
var defaultArg = args.Arguments[1].Value as IMember;
82+
return Index(new ArgumentSet(new List<IMember>() { defaultArg }, args.Expression, args.Eval));
83+
}
84+
return _interpreter.UnknownType;
7485
case @"items":
7586
return CreateList(Items);
7687
case @"keys":

src/Analysis/Ast/Impl/Values/Collections/PythonInstanceIterator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public EmptyIterator(IPythonType unknownType): base(PythonMemberType.Class) {
5353
public IPythonIterator GetIterator() => this;
5454
public IPythonType Type { get; }
5555
public IMember Call(string memberName, IArgumentSet args) => Type;
56-
public IMember Index(object index) => Type;
56+
public IMember Index(IArgumentSet args) => Type;
5757
public IMember Next => Type;
5858
}
5959
}

src/Analysis/Ast/Impl/Values/Collections/PythonIterator.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// permissions and limitations under the License.
1515

1616
using Microsoft.Python.Analysis.Types;
17+
using System.Collections.Generic;
1718

1819
namespace Microsoft.Python.Analysis.Values.Collections {
1920
/// <summary>
@@ -33,7 +34,12 @@ public PythonIterator(IPythonType iteratorType, IPythonCollection collection) :
3334
Collection = collection;
3435
}
3536

36-
public virtual IMember Next => Collection.Index(_index++) ?? UnknownType;
37+
public virtual IMember Next => Collection.Index(GetArgSet(_index++)) ?? UnknownType;
38+
39+
private IArgumentSet GetArgSet(int index) {
40+
var newArg = new PythonConstant(index, Type.DeclaringModule.Interpreter.GetBuiltinType(BuiltinTypeId.Int));
41+
return new ArgumentSet(new List<IMember>() { newArg }, null, null);
42+
}
3743

3844
public override IMember Call(string memberName, IArgumentSet args) {
3945
// Specializations

src/Analysis/Ast/Impl/Values/Definitions/IPythonInstance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public interface IPythonInstance : IMember, IPythonIterable {
3535
/// <summary>
3636
/// Invokes indexer the instance.
3737
/// </summary>
38-
IMember Index(object index);
38+
IMember Index(IArgumentSet args);
3939
}
4040
}

src/Analysis/Ast/Impl/Values/PythonInstance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public virtual IMember Call(string memberName, IArgumentSet args) {
4949
return null;
5050
}
5151

52-
public virtual IMember Index(object index) => this; // Helps with str slicing
52+
public virtual IMember Index(IArgumentSet args) => this; // Helps with str slicing
5353

5454
protected IMember UnknownType => Type.DeclaringModule.Interpreter.UnknownType;
5555

0 commit comments

Comments
 (0)