Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Merge persistence work into master #1482

Merged
merged 79 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
61d60c4
Persistent analysis, part I (#1224)
Jun 20, 2019
b2c9afb
Merge master into database branch (#1312)
Jul 11, 2019
ab8ecaf
Merge master
Jul 15, 2019
4a2fd79
Treat persistent module as regular specialized (#1334)
Jul 23, 2019
da3eb70
Store location of module members in the database (#1339)
Jul 26, 2019
a502cb5
Merge master
Jul 30, 2019
2143719
Multiple fixes and additional tests for persistence (#1372)
Aug 8, 2019
ff46089
Merge master
Aug 10, 2019
78b1bcf
Merge master
Aug 13, 2019
f267b80
Add persistence tests for 60+ system modules (#1429)
Aug 15, 2019
3c53083
Merge master
Aug 16, 2019
69806e0
Test update (we no longer reassign classes)
Aug 16, 2019
356ae4b
Fix typing tests
Aug 16, 2019
36a1d72
Update baseline
Aug 16, 2019
41c94e7
Fix missing keys issue (#1451)
AlexanderSher Aug 16, 2019
6284174
Typevar fix
Aug 17, 2019
28ec49b
Merge master
Aug 20, 2019
8b4c00a
Merge mast
Aug 21, 2019
86c8426
Persistence of generics and named tuples (#1459)
Aug 21, 2019
a82c356
- Fix #1455: AF: Library module of type Stub has been analyzed alread…
AlexanderSher Aug 22, 2019
ab93845
Merge master
Aug 23, 2019
79685eb
Update baselines since __spec__ member was added
Aug 26, 2019
809dc5c
Merge master (partial)
Aug 26, 2019
0f7f7ca
Undo some of changes to match master.
Aug 26, 2019
252d303
Fix null ref
Aug 26, 2019
a013b3a
Merge branch 'master' of https://github.com/Microsoft/python-language…
Aug 27, 2019
4de0812
Merge master
Aug 28, 2019
0913ea3
Merge master
Aug 30, 2019
8adb6cc
Merge master
Aug 30, 2019
c1883b5
Merge master
Sep 3, 2019
b923349
Resolve conflicts
Sep 3, 2019
28ce8e3
Fix null ref on bases due to new AnyStr behavior
Sep 3, 2019
e7f29d7
Handling dependencies in module persistence (#1471)
Sep 5, 2019
8cf831f
Upgrade to 3.0
Sep 5, 2019
958617b
Merge branch 'net3' of https://github.com/MikhailArkhipov/python-lang…
Sep 5, 2019
54d67cb
Undo change + .NET 3
Sep 5, 2019
26ea552
Get back to 2.0
Sep 5, 2019
fff4e0c
Merge branch 'net3' of https://github.com/MikhailArkhipov/python-lang…
Sep 5, 2019
227726c
Update components
Sep 5, 2019
2dd8fcf
Display caching level
Sep 5, 2019
c0f16e6
Fix null ref due to conflicting changes
Sep 5, 2019
987a837
Null ref fix
Sep 5, 2019
6d79b49
Simplify
Sep 5, 2019
28a4ce5
Pass AST whish is required during analysis when module does not have …
Sep 5, 2019
e24a4fd
tensorflow test
Sep 6, 2019
c4404cf
Fix from import model
Sep 6, 2019
c20485b
Undo debug
Sep 6, 2019
119e1b2
Improve goto def in import and fix class base processing (#1529)
Sep 9, 2019
2313d44
Usings
Sep 10, 2019
af2d7c7
Test fix
Sep 10, 2019
8e3289c
Remove CompletionItemKind.None
Sep 10, 2019
af70958
Add info to asserts
Sep 10, 2019
83ddd00
Merge master
Sep 10, 2019
967e741
PR feedback
Sep 10, 2019
653fd80
Undo test change, enable class reassignment
Sep 10, 2019
655818e
Prevent merge of unrelated types
Sep 11, 2019
48561b8
more tests
Sep 11, 2019
61014c1
Fix variable update
Sep 11, 2019
ab29574
Null check
Sep 11, 2019
81e36ce
Baselines
Sep 11, 2019
8323cd2
Move analysis complete event
Sep 11, 2019
f44c051
More stable enumeration
Sep 11, 2019
87093f3
Improve restoring imports in tests
Sep 11, 2019
a81874e
Make extension for tests
Sep 11, 2019
6a6fbb2
Add URI null check
Sep 12, 2019
8547a9b
Fix completion typo
Sep 12, 2019
8aa373c
Add assert on unresolved modules
Sep 12, 2019
820fdd4
Add more details to assert
Sep 12, 2019
db002c4
Using
Sep 12, 2019
56b8d88
To 3.7
Sep 12, 2019
68d38e7
Implement check for big/little endian
Sep 12, 2019
8dcd2bd
Merge master
Sep 12, 2019
93be7b1
Sync
Sep 12, 2019
2bdc314
Synchronization
Sep 13, 2019
3ee351c
Add hard assert
Sep 13, 2019
55b85e4
Fix exception
Sep 14, 2019
97f280c
Merge branch 'master' of https://github.com/Microsoft/python-language…
Sep 16, 2019
4108948
Fix typo
Sep 16, 2019
d6260fe
Add lock
Sep 16, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json -diff
4 changes: 2 additions & 2 deletions build/Common.Build.Core.settings
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<!-- This file is shared across desktop .NET and .NET Core builds.
Please do not put Windows-specific or dekstop-specific settings
here (these incluse C#, C++ or VSSDK settings and targets -->
Please do not put Windows-specific or desktop-specific settings
here (these include C#, C++ or VS SDK settings and targets -->
<PropertyGroup>
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
Expand Down
7 changes: 7 additions & 0 deletions src/Analysis/Ast/Impl/Analyzer/Definitions/IAnalyzable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using Microsoft.Python.Analysis.Dependencies;

namespace Microsoft.Python.Analysis.Analyzer {
/// <summary>
/// Represents document that can be analyzed asynchronously.
/// </summary>
internal interface IAnalyzable {
/// <summary>
/// Returns object that can calculate dependencies of this entry.
/// </summary>
IDependencyProvider DependencyProvider { get; }

/// <summary>
/// Notifies document that analysis is about to begin.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.Python.Analysis.Documents;
using Microsoft.Python.Analysis.Extensions;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public IMember GetValueFromList(ListExpression expression) {
var value = GetValueFromExpression(item) ?? UnknownType;
contents.Add(value);
}
return PythonCollectionType.CreateList(Module.Interpreter, contents, exact: expression.Items.Count <= MaxCollectionSize);
return PythonCollectionType.CreateList(Module, contents, exact: expression.Items.Count <= MaxCollectionSize);
}

public IMember GetValueFromDictionary(DictionaryExpression expression) {
Expand All @@ -72,7 +72,7 @@ public IMember GetValueFromDictionary(DictionaryExpression expression) {
var value = GetValueFromExpression(item.SliceStop) ?? UnknownType;
contents[key] = value;
}
return new PythonDictionary(Interpreter, contents, exact: expression.Items.Count <= MaxCollectionSize);
return new PythonDictionary(Module, contents, exact: expression.Items.Count <= MaxCollectionSize);
}

private IMember GetValueFromTuple(TupleExpression expression) {
Expand All @@ -81,7 +81,7 @@ private IMember GetValueFromTuple(TupleExpression expression) {
var value = GetValueFromExpression(item) ?? UnknownType;
contents.Add(value);
}
return PythonCollectionType.CreateTuple(Module.Interpreter, contents, exact: expression.Items.Count <= MaxCollectionSize);
return PythonCollectionType.CreateTuple(Module, contents, exact: expression.Items.Count <= MaxCollectionSize);
}

public IMember GetValueFromSet(SetExpression expression) {
Expand All @@ -90,7 +90,7 @@ public IMember GetValueFromSet(SetExpression expression) {
var value = GetValueFromExpression(item) ?? UnknownType;
contents.Add(value);
}
return PythonCollectionType.CreateSet(Interpreter, contents, exact: expression.Items.Count <= MaxCollectionSize);
return PythonCollectionType.CreateSet(Module, contents, exact: expression.Items.Count <= MaxCollectionSize);
}

public IMember GetValueFromGenerator(GeneratorExpression expression) {
Expand All @@ -110,14 +110,14 @@ public IMember GetValueFromComprehension(Comprehension node) {
switch (node) {
case ListComprehension lc:
var v1 = GetValueFromExpression(lc.Item) ?? UnknownType;
return PythonCollectionType.CreateList(Interpreter, new[] { v1 });
return PythonCollectionType.CreateList(Module, new[] { v1 });
case SetComprehension sc:
var v2 = GetValueFromExpression(sc.Item) ?? UnknownType;
return PythonCollectionType.CreateSet(Interpreter, new[] { v2 });
return PythonCollectionType.CreateSet(Module, new[] { v2 });
case DictionaryComprehension dc:
var k = GetValueFromExpression(dc.Key) ?? UnknownType;
var v = GetValueFromExpression(dc.Value) ?? UnknownType;
return new PythonDictionary(new PythonDictionaryType(Interpreter), new Dictionary<IMember, IMember> { { k, v } });
return new PythonDictionary(new PythonDictionaryType(Interpreter.ModuleResolution.BuiltinsModule), new Dictionary<IMember, IMember> { { k, v } });
}

return UnknownType;
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, LookupOptions options) {
public IPythonInstance 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 @@ -62,11 +62,12 @@ private IMember GetValueFromGeneric(IMember target, Expression expr) {
}

/// <summary>
/// Returns whether the arguments to Generic are valid
/// Determines if arguments to Generic are valid
/// </summary>
// TODO: move check to GenericClassBase. This requires extensive changes to SpecificTypeConstructor.
private bool GenericClassParameterValid(IReadOnlyList<IGenericTypeParameter> genericTypeArgs, IReadOnlyList<IMember> args, Expression expr) {
// All arguments to Generic must be type parameters
// e.g. Generic[T, str] throws a runtime error
// All arguments to Generic must be type parameters
// e.g. Generic[T, str] throws a runtime error
if (genericTypeArgs.Count != args.Count) {
ReportDiagnostics(Module.Uri, new DiagnosticsEntry(
Resources.GenericNotAllTypeParameters,
Expand All @@ -77,14 +78,14 @@ private bool GenericClassParameterValid(IReadOnlyList<IGenericTypeParameter> gen
return false;
}

// All arguments to Generic must be distinct
// All arguments to Generic must be distinct
if (genericTypeArgs.Distinct().Count() != genericTypeArgs.Count) {
ReportDiagnostics(Module.Uri, new DiagnosticsEntry(
Resources.GenericNotAllUnique,
GetLocation(expr).Span,
ErrorCodes.TypingGenericArguments,
Severity.Warning,
DiagnosticSource.Analysis));
Resources.GenericNotAllUnique,
GetLocation(expr).Span,
ErrorCodes.TypingGenericArguments,
Severity.Warning,
DiagnosticSource.Analysis));
return false;
}

Expand All @@ -97,24 +98,19 @@ private bool GenericClassParameterValid(IReadOnlyList<IGenericTypeParameter> gen
/// (if the former) on specific type (if the latter).
/// </summary>
private IMember CreateSpecificTypeFromIndex(IGenericType gt, IReadOnlyList<IMember> args, Expression expr) {
// TODO: move check to GenericClassBase. This requires extensive changes to SpecificTypeConstructor.
if (gt.Name.EqualsOrdinal("Generic")) {
var genericTypeArgs = args.OfType<IGenericTypeParameter>().ToArray();
if (!GenericClassParameterValid(genericTypeArgs, args, expr)) {
return UnknownType;
}

// Generic[T1, T2, ...] expression. Create generic base for the class.
return new GenericClassParameter(genericTypeArgs, Module);
}

// For other types just use supplied arguments
if (args.Count > 0) {
return gt.CreateSpecificType(new ArgumentSet(args, expr, this));
// Generic[T1, T2, ...] expression. Create generic base for the class.
return new GenericClassBase(genericTypeArgs, Module.Interpreter);
}

return UnknownType;
// For other types just use supplied arguments
return args.Count > 0 ? gt.CreateSpecificType(new ArgumentSet(args, expr, this)) : UnknownType;
}

private IReadOnlyList<IMember> EvaluateIndex(IndexExpression expr) {
var indices = new List<IMember>();
if (expr.Index is TupleExpression tex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ private IMember GetValueFromBinaryOp(Expression expr) {

switch (leftTypeId) {
case BuiltinTypeId.List:
return PythonCollectionType.CreateConcatenatedList(Module.Interpreter, lc, rc);
return PythonCollectionType.CreateConcatenatedList(Module, lc, rc);
case BuiltinTypeId.Tuple:
return PythonCollectionType.CreateConcatenatedTuple(Module.Interpreter, lc, rc);
return PythonCollectionType.CreateConcatenatedTuple(Module, lc, rc);
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public ExpressionEval(IServiceContainer services, IPythonModule module, PythonAs
public ModuleSymbolTable SymbolTable { get; } = new ModuleSymbolTable();
public IPythonType UnknownType => Interpreter.UnknownType;
public Location DefaultLocation { get; }
public IPythonModule BuiltinsModule => Interpreter.ModuleResolution.BuiltinsModule;

public LocationInfo GetLocationInfo(Node node) => node?.GetLocation(this) ?? LocationInfo.Empty;

Expand Down Expand Up @@ -188,7 +189,7 @@ public IMember GetValueFromExpression(Expression expr, LookupOptions options = L
m = null;
break;
default:
m = GetValueFromBinaryOp(expr) ?? GetConstantFromLiteral(expr, options);
m = GetValueFromBinaryOp(expr) ?? GetConstantFromLiteral(expr);
break;
}
if (m == null) {
Expand Down Expand Up @@ -236,29 +237,32 @@ private IMember GetValueFromMember(MemberExpression expr) {
return null;
}

IPythonInstance instance = null;
var m = GetValueFromExpression(expr.Target);
if (m is IPythonType typeInfo) {
var member = typeInfo.GetMember(expr.Name);
// If container is class/type info rather than the instance, then the method is an unbound function.
// Example: C.f where f is a method of C. Compare to C().f where f is bound to the instance of C.
if (member is PythonFunctionType f && !f.IsStatic && !f.IsClassMethod) {
f.AddReference(GetLocationOfName(expr));
return f.ToUnbound();
}
instance = new PythonInstance(typeInfo);
if (m == null) {
return UnknownType;
}

instance = instance ?? m as IPythonInstance;
var type = m?.GetPythonType(); // Try inner type

var type = m.GetPythonType();
var value = type?.GetMember(expr.Name);
type?.AddMemberReference(expr.Name, this, GetLocationOfName(expr));

if (type is IPythonModule) {
return value;
}

IPythonInstance instance = null;
if (m == type) {
// If container is class/type info rather than the instance, then the method is an unbound function.
// Example: C.f where f is a method of C. Compare to C().f where f is bound to the instance of C.
if (value is PythonFunctionType f && f.DeclaringType != null && !f.IsStatic && !f.IsClassMethod) {
f.AddReference(GetLocationOfName(expr));
return f.ToUnbound();
}
instance = new PythonInstance(type);
}

instance = instance ?? m as IPythonInstance;

// Class type GetMember returns a type. However, class members are
// mostly instances (consider self.x = 1, x is an instance of int).
// However, it is indeed possible to have them as types, like in
Expand Down
6 changes: 3 additions & 3 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/AssignmentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public void HandleAssignment(AssignmentStatement node) {
}

foreach (var ne in node.Left.OfType<NameExpression>()) {
IScope scope;
if (Eval.CurrentScope.NonLocals[ne.Name] != null) {
Eval.LookupNameInScopes(ne.Name, out var scope, LookupOptions.Nonlocal);
Eval.LookupNameInScopes(ne.Name, out scope, LookupOptions.Nonlocal);
scope?.Variables[ne.Name].Assign(value, Eval.GetLocationOfName(ne));
continue;
}

if (Eval.CurrentScope.Globals[ne.Name] != null) {
Eval.LookupNameInScopes(ne.Name, out var scope, LookupOptions.Global);
Eval.LookupNameInScopes(ne.Name, out scope, LookupOptions.Global);
scope?.Variables[ne.Name].Assign(value, Eval.GetLocationOfName(ne));
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public ConditionalHandler(AnalysisWalker walker) : base(walker) {
}

public bool HandleIf(IfStatement node)
=> node.WalkIfWithSystemConditions(Walker, Ast.LanguageVersion, _platformService.IsWindows);
=> node.WalkIfWithSystemConditions(Walker, Ast.LanguageVersion, _platformService);
}
}
22 changes: 12 additions & 10 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/FromImportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ private void AssignVariables(FromImportStatement node, IImportSearchResult impor
if (!string.IsNullOrEmpty(memberName)) {
var nameExpression = asNames[i] ?? names[i];
var variableName = nameExpression?.Name ?? memberName;
var variable = variableModule.Analysis?.GlobalScope?.Variables[memberName];
var exported = variable ?? variableModule.GetMember(memberName);
var value = exported ?? GetValueFromImports(variableModule, imports as IImportChildrenSource, memberName);
// Do not allow imported variables to override local declarations
Eval.DeclareVariable(variableName, value, VariableSource.Import, nameExpression, CanOverwriteVariable(variableName, node.StartIndex));
if (!string.IsNullOrEmpty(variableName)) {
var variable = variableModule.Analysis?.GlobalScope?.Variables[memberName];
var exported = variable ?? variableModule.GetMember(memberName);
var value = exported ?? GetValueFromImports(variableModule, imports as IImportChildrenSource, memberName);
// Do not allow imported variables to override local declarations
Eval.DeclareVariable(variableName, value, VariableSource.Import, nameExpression, CanOverwriteVariable(variableName, node.StartIndex));
}
}
}
}
Expand All @@ -84,7 +86,7 @@ private void HandleModuleImportStar(PythonVariableModule variableModule, bool is
}

// If __all__ is present, take it, otherwise declare all members from the module that do not begin with an underscore.
var memberNames = isImplicitPackage
var memberNames = isImplicitPackage
? variableModule.GetMemberNames()
: variableModule.Analysis.StarImportMemberNames ?? variableModule.GetMemberNames().Where(s => !s.StartsWithOrdinal("_"));

Expand All @@ -109,7 +111,7 @@ private void HandleModuleImportStar(PythonVariableModule variableModule, bool is

private bool CanOverwriteVariable(string name, int importPosition) {
var v = Eval.CurrentScope.Variables[name];
if(v == null) {
if (v == null) {
return true; // Variable does not exist
}
// Allow overwrite if import is below the variable. Consider
Expand All @@ -118,10 +120,10 @@ private bool CanOverwriteVariable(string name, int importPosition) {
// from A import * # brings another x
// x = 3
var references = v.References.Where(r => r.DocumentUri == Module.Uri).ToArray();
if(references.Length == 0) {
if (references.Length == 0) {
// No references to the variable in this file - the variable
// is imported from another module. OK to overwrite.
return true;
return true;
}
var firstAssignmentPosition = references.Min(r => r.Span.ToIndexSpan(Ast).Start);
return firstAssignmentPosition < importPosition;
Expand Down Expand Up @@ -151,7 +153,7 @@ private void SpecializeFuture(FromImportStatement node) {
var printNameExpression = node.Names.FirstOrDefault(n => n?.Name == "print_function");
if (printNameExpression != null) {
var fn = new PythonFunctionType("print", new Location(Module), null, string.Empty);
var o = new PythonFunctionOverload(fn.Name, new Location(Module));
var o = new PythonFunctionOverload(fn, new Location(Module));
var parameters = new List<ParameterInfo> {
new ParameterInfo("*values", Interpreter.GetBuiltinType(BuiltinTypeId.Object), ParameterKind.List, null),
new ParameterInfo("sep", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.KeywordOnly, null),
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Ast/Impl/Analyzer/Handlers/ImportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private PythonVariableModule GetOrCreateVariableModule(in string fullName, in Py
return variableModule;
}

variableModule = new PythonVariableModule(fullName, Interpreter);
variableModule = new PythonVariableModule(fullName, Eval.Interpreter);
_variableModules[fullName] = variableModule;
parentModule?.AddChildModule(memberName, variableModule);
return variableModule;
Expand Down
3 changes: 0 additions & 3 deletions src/Analysis/Ast/Impl/Analyzer/LibraryAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.Python.Analysis.Analyzer.Evaluation;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Documents;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Diagnostics;
using Microsoft.Python.Parsing.Ast;

Expand Down
Loading