Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Arkhipov committed Nov 12, 2019
2 parents f74d5b6 + 2b1bd33 commit 0b28fa4
Show file tree
Hide file tree
Showing 61 changed files with 701 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public IMember GetValueFromCallable(CallExpression expr, LookupOptions lookupOpt
value = GetValueFromInstanceCall(pi, expr);
break;
case IPythonFunctionType ft: // Standalone function or a class method call.
var instance = ft.DeclaringType?.CreateInstance(args);
var instance = ft.DeclaringType?.CreateInstance(args) as IPythonInstance;
value = GetValueFromFunctionType(ft, instance, expr);
break;
case IPythonClassType cls:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public IMember GetValueFromIndex(IndexExpression expr, LookupOptions lookupOptio
var type = target.GetPythonType();
if (type != null) {
if (!(target is IPythonInstance instance)) {
instance = type.CreateInstance(ArgumentSet.Empty(expr, this));
instance = type.CreateInstance(ArgumentSet.Empty(expr, this)) as IPythonInstance;
}
var index = GetValueFromExpression(expr.Index, lookupOptions);
if (index != null) {
return type.Index(instance, new ArgumentSet(new[] { index }, expr, this));
if (instance != null) {
var index = GetValueFromExpression(expr.Index, lookupOptions);
if (index != null) {
return type.Index(instance, new ArgumentSet(new[] {index}, expr, this));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace Microsoft.Python.Analysis.Analyzer.Evaluation {
internal sealed partial class ExpressionEval {
public IPythonInstance GetConstantFromLiteral(Expression expr) {
public IMember GetConstantFromLiteral(Expression expr) {
if (expr is ConstantExpression ce) {
switch (ce.Value) {
case string s:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private IMember GetValueFromMember(MemberExpression expr, LookupOptions lookupOp
f.AddReference(GetLocationOfName(expr));
return f.ToUnbound();
}
instance = type.CreateInstance(ArgumentSet.Empty(expr, this));
instance = type.CreateInstance(ArgumentSet.Empty(expr, this)) as IPythonInstance;
}

instance = instance ?? m as IPythonInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public IMember EvaluateCall(IArgumentSet args) {
// Open scope and declare parameters
using (_eval.OpenScope(_declaringModule, _function, out _)) {
args.DeclareParametersInScope(_eval);
_function.Body.Walk(this);
_function.Body?.Walk(this);
}
return _result;
}
Expand Down
7 changes: 0 additions & 7 deletions src/Analysis/Ast/Impl/Analyzer/Symbols/ClassEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ private bool IsValidBase(Arg a, LookupOptions lookupOptions) {
return true;
}

// Allow extensions from specialized functions
// We specialized type to be a function even though it is a class, so this allows extension of type
// TODO handle module specialization better: https://github.com/microsoft/python-language-server/issues/1367
if (m is IPythonType t && t.IsSpecialized) {
return true;
}

switch (m.MemberType) {
// Inheriting from these members is invalid
case PythonMemberType.Method:
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Ast/Impl/Microsoft.Python.Analysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.0.0" />
</ItemGroup>
<ItemGroup>
<Content Include="get_search_paths.py">
Expand Down
27 changes: 15 additions & 12 deletions src/Analysis/Ast/Impl/Modules/BuiltinsPythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.Python.Analysis.Specializations;
using Microsoft.Python.Analysis.Specializations.Typing;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Core.IO;
using Microsoft.Python.Core.Logging;
using Microsoft.Python.Parsing;
using Microsoft.Python.Parsing.Ast;
using Type = Microsoft.Python.Analysis.Specializations.Type;

namespace Microsoft.Python.Analysis.Modules {
/// <summary>
Expand Down Expand Up @@ -171,12 +173,13 @@ private void SpecializeFunctions() {
Analysis.SpecializeFunction("pow", BuiltinsSpecializations.Identity);
Analysis.SpecializeFunction("range", BuiltinsSpecializations.Range);
Analysis.SpecializeFunction("sum", BuiltinsSpecializations.CollectionItem);
Analysis.SpecializeFunction("type", BuiltinsSpecializations.TypeInfo);
Analysis.SpecializeFunction("vars", BuiltinsSpecializations.DictStringToObject);

Analysis.SpecializeFunction("super", BuiltinsSpecializations.Super);
//SpecializeFunction(_builtinName, "range", RangeConstructor);
//SpecializeFunction(_builtinName, "sorted", ReturnsListOfInputIterable);

Analysis.GlobalScope.DeclareVariable("type", new Type(Analysis.Document), VariableSource.Builtin);
}

private IReadOnlyList<ParameterInfo> OpenConstructor() {
Expand All @@ -186,18 +189,18 @@ private IReadOnlyList<ParameterInfo> OpenConstructor() {
new ParameterInfo("mode", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant("r", Interpreter.GetBuiltinType(BuiltinTypeId.Str))),
new ParameterInfo("buffering", Interpreter.GetBuiltinType(BuiltinTypeId.Int), ParameterKind.Normal, new PythonConstant(-1, Interpreter.GetBuiltinType(BuiltinTypeId.Int))),
};
} else {
return new[] {
new ParameterInfo("file", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, null),
new ParameterInfo("mode", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant("r", Interpreter.GetBuiltinType(BuiltinTypeId.Str))),
new ParameterInfo("buffering", Interpreter.GetBuiltinType(BuiltinTypeId.Int), ParameterKind.Normal, new PythonConstant(-1, Interpreter.GetBuiltinType(BuiltinTypeId.Int))),
new ParameterInfo("encoding", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.None))),
new ParameterInfo("errors", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.None))),
new ParameterInfo("newline", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.None))),
new ParameterInfo("closefd", Interpreter.GetBuiltinType(BuiltinTypeId.Bool), ParameterKind.Normal, new PythonConstant(true, Interpreter.GetBuiltinType(BuiltinTypeId.Bool))),
new ParameterInfo("opener", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.Str)))
};
}

return new[] {
new ParameterInfo("file", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, null),
new ParameterInfo("mode", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant("r", Interpreter.GetBuiltinType(BuiltinTypeId.Str))),
new ParameterInfo("buffering", Interpreter.GetBuiltinType(BuiltinTypeId.Int), ParameterKind.Normal, new PythonConstant(-1, Interpreter.GetBuiltinType(BuiltinTypeId.Int))),
new ParameterInfo("encoding", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.None))),
new ParameterInfo("errors", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.None))),
new ParameterInfo("newline", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.None))),
new ParameterInfo("closefd", Interpreter.GetBuiltinType(BuiltinTypeId.Bool), ParameterKind.Normal, new PythonConstant(true, Interpreter.GetBuiltinType(BuiltinTypeId.Bool))),
new ParameterInfo("opener", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.Str)))
};
}
}
}
13 changes: 11 additions & 2 deletions src/Analysis/Ast/Impl/Modules/PythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal PythonModule(ModuleCreationOptions creationOptions, IServiceContainer s
public bool IsAbstract => false;
public virtual bool IsSpecialized => false;

public IPythonInstance CreateInstance(IArgumentSet args) => new PythonInstance(this);
public IMember CreateInstance(IArgumentSet args) => new PythonInstance(this);
public override PythonMemberType MemberType => PythonMemberType.Module;
public IMember Call(IPythonInstance instance, string memberName, IArgumentSet args) => GetMember(memberName);
public IMember Index(IPythonInstance instance, IArgumentSet args) => Interpreter.UnknownType;
Expand Down Expand Up @@ -313,7 +313,16 @@ private void Parse() {
_linkedParseCts = CancellationTokenSource.CreateLinkedTokenSource(_disposeToken.CancellationToken, _parseCts.Token);

ContentState = State.Parsing;
_parsingTask = Task.Run(() => Parse(_linkedParseCts.Token), _linkedParseCts.Token);
_parsingTask = Task.Run(() => ParseAndLogExceptions(_linkedParseCts.Token), _linkedParseCts.Token);
}

private void ParseAndLogExceptions(CancellationToken cancellationToken) {
try {
Parse(cancellationToken);
} catch (Exception ex) when (!(ex is OperationCanceledException)) {
Log?.Log(TraceEventType.Warning, $"Exception while parsing {FilePath}: {ex}");
throw;
}
}

private void Parse(CancellationToken cancellationToken) {
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Ast/Impl/Modules/PythonVariableModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public PythonVariableModule(IPythonModule module): base(module) {

public IMember Call(IPythonInstance instance, string memberName, IArgumentSet args) => GetMember(memberName);
public IMember Index(IPythonInstance instance, IArgumentSet args) => Interpreter.UnknownType;
public IPythonInstance CreateInstance(IArgumentSet args = null) => new PythonInstance(this);
public IMember CreateInstance(IArgumentSet args = null) => new PythonInstance(this);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
using System.Linq;
using Microsoft.Python.Analysis.Specializations.Typing.Types;
using Microsoft.Python.Analysis.Specializations.Typing.Values;
Expand All @@ -31,12 +30,6 @@ public static IMember Identity(IPythonModule module, IPythonFunctionOverload ove
return args.Count > 0 ? args.FirstOrDefault(a => !a.IsUnknown()) ?? args[0] : null;
}

public static IMember TypeInfo(IPythonModule module, IPythonFunctionOverload overload, IArgumentSet argSet, IndexSpan indexSpan) {
var args = argSet.Values<IMember>();
var t = args.Count > 0 ? args[0].GetPythonType() : module.Interpreter.GetBuiltinType(BuiltinTypeId.Type);
return t.ToBound();
}

public static IMember Iterator(IPythonModule module, IPythonFunctionOverload overload, IArgumentSet argSet, IndexSpan indexSpan) {
var args = argSet.Values<IMember>();
if (args.Count > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AnyType(IPythonModule declaringModule) : base(declaringModule) { }

public IMember Call(IPythonInstance instance, string memberName, IArgumentSet args)
=> DeclaringModule.Interpreter.UnknownType;
public IPythonInstance CreateInstance(IArgumentSet args) => new PythonInstance(this);
public IMember CreateInstance(IArgumentSet args) => new PythonInstance(this);

public IMember GetMember(string name) => null;
public IEnumerable<string> GetMemberNames() => Array.Empty<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private SpecializedGenericType(string name, string qualifiedName, IPythonModule
public bool IsAbstract => true;
public bool IsSpecialized => true;

public IPythonInstance CreateInstance(IArgumentSet args) {
public IMember CreateInstance(IArgumentSet args) {
var types = GetTypesFromValues(args.Arguments);
if (types.Count != args.Arguments.Count) {
throw new ArgumentException(@"Generic type instance construction arguments must be all of IPythonType", nameof(args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static bool TypeVarArgumentsValid(IArgumentSet argSet) {
}

// Report diagnostic if user passed in a value for name and it is not a string
var name = (args[0].Value as IPythonConstant)?.GetString();
var name = (args[1].Value as IPythonConstant)?.GetString();
if (string.IsNullOrEmpty(name)) {
eval.ReportDiagnostics(
eval.Module.Uri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public NamedTupleType(string tupleName, IReadOnlyList<string> itemNames, IReadOn
public override void RemoveReferences(IPythonModule module) => _locatedMember.RemoveReferences(module);
#endregion

public override IPythonInstance CreateInstance(IArgumentSet args) => new TypingTuple(this);
public override IMember CreateInstance(IArgumentSet args) => new TypingTuple(this);

// NamedTuple does not create instances, it defines a type.
public override IMember Call(IPythonInstance instance, string memberName, IArgumentSet args) => this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IEnumerator<IPythonType> GetEnumerator()
public IPythonUnionType Add(IPythonType t) => this;
public IPythonUnionType Add(IPythonUnionType types) => this;

public override IPythonInstance CreateInstance(IArgumentSet args)
public override IMember CreateInstance(IArgumentSet args)
=> InnerType.CreateInstance(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public TypeAlias(string name, IPythonType type) : base(type) {
public override string QualifiedName => $"typing:{Name}";
public override bool IsSpecialized => true;

public override IPythonInstance CreateInstance(IArgumentSet args) => new PythonInstance(this);
public override IMember CreateInstance(IArgumentSet args) => new PythonInstance(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TypingDictionaryType(string name, IPythonType keyType, IPythonType valueT
public override string Name { get; }
public override string QualifiedName { get; }

public override IPythonInstance CreateInstance(IArgumentSet args) => new TypingDictionary(this);
public override IMember CreateInstance(IArgumentSet args) => new TypingDictionary(this);
public override IMember Index(IPythonInstance instance, IArgumentSet args) => ValueType.CreateInstance(args);
public override bool IsSpecialized => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TypingListType(string typeName, BuiltinTypeId typeId, IPythonType itemTyp
public override bool IsAbstract => false;
public override bool IsSpecialized => true;

public override IPythonInstance CreateInstance(IArgumentSet args) => new TypingList(this);
public override IMember CreateInstance(IArgumentSet args) => new TypingList(this);
public IPythonType ItemType { get; }

public override IMember Index(IPythonInstance instance, IArgumentSet args) => ItemType.CreateInstance(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TypingTupleType(IReadOnlyList<IPythonType> itemTypes, IPythonModule decla
public override bool IsAbstract => false;
public override bool IsSpecialized => true;

public override IPythonInstance CreateInstance(IArgumentSet args)
public override IMember CreateInstance(IArgumentSet args)
=> new TypingTuple(this);

public override IMember Index(IPythonInstance instance, IArgumentSet args) {
Expand Down
Loading

0 comments on commit 0b28fa4

Please sign in to comment.