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

Lazy types in caching #1757

Merged
merged 116 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
c6a9c22
Remove stale reference
Sep 30, 2019
1360827
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 1, 2019
ccaaa02
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 4, 2019
da40dcc
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 4, 2019
c348ac3
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 5, 2019
53bc044
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 7, 2019
484a92a
Merge branch 'master' of https://github.com/MikhailArkhipov/python-la…
Oct 7, 2019
e6df3aa
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 8, 2019
1d289d8
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 8, 2019
126f355
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 12, 2019
7e715f3
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 25, 2019
745a3eb
Fix saving of ALL import statements
Oct 25, 2019
2a1ca14
Attributes
Oct 26, 2019
91f3ca6
Initial
Oct 26, 2019
b2e92ff
Partial
Oct 28, 2019
32923a5
Merge branch 'master' of https://github.com/microsoft/python-language…
Oct 31, 2019
9b691e6
Part I
Nov 1, 2019
9612990
Test pass
Nov 2, 2019
1b72f4b
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 2, 2019
2fa422b
Partial proxies
Nov 4, 2019
832d981
Initial lazy types
Nov 4, 2019
d613300
Fix test
Nov 4, 2019
cdc7875
AST library test passed
Nov 4, 2019
9e6917b
Fix more tests
Nov 4, 2019
960ee87
Fix generics
Nov 5, 2019
45ed4eb
Handle specialization in dependencies
Nov 5, 2019
fd7a285
Merge master
Nov 5, 2019
68b2f3b
Merge issues
Nov 5, 2019
e68e1b6
Fix os test (posix stub)
Nov 5, 2019
800e231
Fix sys.stdin case
Nov 5, 2019
d49e246
Better handle circular references, cache models and resolve os.path
Nov 5, 2019
013df6a
Improve stub handling
Nov 5, 2019
5ebcdfe
Test fixes
Nov 6, 2019
3fff06f
Test updates
Nov 6, 2019
a928741
Fix deadlock
Nov 6, 2019
da64646
Simplify
Nov 6, 2019
a5bc47f
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 6, 2019
2b93042
Properly handle caching level setting
Nov 6, 2019
2abe2cf
Caching level fix
Nov 6, 2019
4711d90
Fix endless analysis
Nov 6, 2019
fdbf219
Fix 'missing keys' message with cached modules
Nov 6, 2019
01f763b
Optimize IO
Nov 6, 2019
d569619
More IO optimizations
Nov 7, 2019
f74d5b6
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 7, 2019
2a64510
Full restore tests
Nov 7, 2019
f2bd272
Deeper comparison in tests
Nov 8, 2019
d8b46ba
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 8, 2019
d7ca27f
Fix instance/class mismatch in operators eval
Nov 8, 2019
79e8b2d
Properly handle variables in type.AddMember
Nov 8, 2019
f197a6d
Fix function inner members restore
Nov 8, 2019
3f9ee9a
Cache models by qualified name
Nov 10, 2019
7a488b3
Don't restore all members on GetMember. Improves tensorflow by 30%
Nov 10, 2019
c5f2cb8
Turn off indexing of libraries, 25%
Nov 11, 2019
dee8420
optimize db access
Nov 11, 2019
d7a6fea
fix test
Nov 11, 2019
f109701
Remove debug code
Nov 11, 2019
c4891cf
Fix comment
Nov 11, 2019
6726924
Delete debug file
Nov 11, 2019
1d0b1ba
Bump up db version
Nov 11, 2019
36aba0b
Bump db version to 5
Nov 11, 2019
8b4942c
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 11, 2019
e155470
Test fix
Nov 11, 2019
e0de03e
Handle empty bases
Nov 11, 2019
a2caf6a
Fix one more 'missing keys' case
Nov 11, 2019
aa60787
Remove null declaring module from iterators
Nov 11, 2019
d9db6a3
Add queued cache write
Nov 11, 2019
3f9e96f
Dispose writer
Nov 11, 2019
cd60912
Formatting
Nov 11, 2019
72dd9c8
Simplify unique name
Nov 12, 2019
4541035
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 12, 2019
ac490f7
Cache unique id against multiple calls
Nov 12, 2019
053249a
Delay writing analysis into cache until done.
Nov 12, 2019
0b28fa4
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 12, 2019
6109ac7
Don't suppress LHS diagnostics on augmented assign
Nov 12, 2019
bcfc3b7
Revert "Don't suppress LHS diagnostics on augmented assign"
Nov 12, 2019
0c13e00
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 13, 2019
e8c7a9d
Baseline updates
Nov 13, 2019
883fd73
Baselines
Nov 13, 2019
2ea1631
Add option to write analysis immediately to disk for tests
Nov 13, 2019
471d1f7
Workaround for named tuples naming
Nov 13, 2019
8e766a2
Comment
Nov 13, 2019
dc286b8
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 13, 2019
1f716e8
Sync builtins creation
Nov 15, 2019
e304507
Sync builtins
Nov 15, 2019
6e1a119
Usings
Nov 15, 2019
6093070
Debug code
Nov 15, 2019
63ed836
Merge builtins fix
Nov 16, 2019
0de428d
Merge fix
Nov 16, 2019
cc909be
Merge master
Nov 16, 2019
3229082
Merge branch 'master' of https://github.com/microsoft/python-language…
Nov 19, 2019
9ca0422
Fix test code
Nov 22, 2019
c771595
Undo accidental checkin
Dec 2, 2019
f93aa44
Add handling of exceptions to indexer
Dec 2, 2019
3edce72
Update baseline
Dec 2, 2019
e397e2a
Improve synchronization of analysis sessions
Dec 3, 2019
0564a28
Tweak locks
Dec 3, 2019
cd42f79
Reduce lock
Dec 3, 2019
6aee99e
Track next session
Dec 3, 2019
cfd339a
Better next session tracking
Dec 3, 2019
a01d309
Adjust timeout for slower machines
Dec 3, 2019
fd9445d
Timeout on slower machines
Dec 3, 2019
ad53c29
Extend lock in invalidate
Dec 3, 2019
234733e
Revert "Extend lock in invalidate"
Dec 4, 2019
e22f03f
Account for modules still in parsing
Dec 4, 2019
25fb86a
Unify type member collection lock
Dec 3, 2019
2b11782
Correct AST used in stub loop analysis
Dec 5, 2019
caf77c9
Unify type member collection lock
Dec 3, 2019
10a577e
Spelling
Dec 5, 2019
4dbc1b1
Null check
Dec 5, 2019
026b40a
Comment out services dispose for test stability since when test compl…
Dec 5, 2019
e63a4ac
Remove another dispose
Dec 5, 2019
cd68be8
Merge branch 'newdb' of https://github.com/MikhailArkhipov/python-lan…
Dec 6, 2019
337ff52
Old code in comments
Dec 6, 2019
777d7ee
Relax completion checking
Dec 9, 2019
fb93a4f
Undo a bit
Dec 9, 2019
11e0f9c
Comments
Dec 9, 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
4 changes: 2 additions & 2 deletions src/Analysis/Ast/Impl/Analyzer/AnalysisModuleKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace Microsoft.Python.Analysis.Analyzer {
[DebuggerDisplay("{Name} : {FilePath}")]
internal readonly struct AnalysisModuleKey : IEquatable<AnalysisModuleKey> {
public readonly struct AnalysisModuleKey : IEquatable<AnalysisModuleKey> {
public string Name { get; }
public string FilePath { get; }
public bool IsTypeshed { get; }
Expand All @@ -34,7 +34,7 @@ public AnalysisModuleKey(IPythonModule module) : this(
module.IsTypeshed,
IsNonUserAsDocumentModule(module)) { }

public AnalysisModuleKey(string name, string filePath, bool isTypeshed)
public AnalysisModuleKey(string name, string filePath, bool isTypeshed = false)
: this(name, filePath, isTypeshed, false) { }

private AnalysisModuleKey(string name, string filePath, bool isTypeshed, bool isNonUserAsDocument) {
Expand Down
80 changes: 80 additions & 0 deletions src/Analysis/Ast/Impl/Analyzer/CachedAnalysis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
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.Utilities;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Diagnostics;
using Microsoft.Python.Parsing.Ast;

namespace Microsoft.Python.Analysis.Analyzer {
/// <summary>
/// Analysis of a module restored from database.
/// </summary>
internal sealed class CachedAnalysis : IDocumentAnalysis {
public CachedAnalysis(IDocument document, IServiceContainer services) {
Check.ArgumentNotNull(nameof(document), document);
Document = document;
ExpressionEvaluator = new ExpressionEval(services, document, AstUtilities.MakeEmptyAst(document.Uri));
}

#region IDocumentAnalysis
/// <summary>
/// Analyzed document.
/// </summary>
public IDocument Document { get; }

/// <summary>
/// Version of the analysis. Usually matches document version,
/// but can be lower when document or its dependencies were
/// updated since.
/// </summary>
public int Version => 0;

/// <summary>
/// Empty AST.
/// </summary>
public PythonAst Ast => ExpressionEvaluator.Ast;

/// <summary>
/// Document/module global scope.
/// </summary>
public IGlobalScope GlobalScope => Document.GlobalScope;

/// <summary>
/// Expression evaluator used in the analysis.
/// Only supports scope operation since there is no AST
/// when library analysis is complete.
/// </summary>
public IExpressionEvaluator ExpressionEvaluator { get; }

/// <summary>
/// Members of the module which are transferred during a star import. null means __all__ was not defined.
/// </summary>
public IReadOnlyList<string> StarImportMemberNames => Array.Empty<string>();

/// <summary>
/// Analysis diagnostics.
/// </summary>
public IEnumerable<DiagnosticsEntry> Diagnostics => Enumerable.Empty<DiagnosticsEntry>();
#endregion
}
}
5 changes: 0 additions & 5 deletions src/Analysis/Ast/Impl/Analyzer/Definitions/IAnalyzable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ namespace Microsoft.Python.Analysis.Analyzer {
/// 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
7 changes: 0 additions & 7 deletions src/Analysis/Ast/Impl/Analyzer/Definitions/IPythonAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,5 @@ public interface IPythonAnalyzer {
/// Fires when analysis is complete.
/// </summary>
event EventHandler<AnalysisCompleteEventArgs> AnalysisComplete;

/// <summary>
/// Attempts to restore modules analysis from database.
/// </summary>
/// <param name="module"></param>
/// <returns></returns>
IDocumentAnalysis TryRestoreCachedAnalysis(IPythonModule module);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ private IMember GetValueFromBinaryOp(Expression expr, LookupOptions lookupOption
if (op.IsComparison()) {
return Interpreter.GetBuiltinType(BuiltinTypeId.Bool);
}

return UnknownType;
}

var leftValue = left is IVariable v1 ? v1.Value : left;
var rightValue = right is IVariable v2 ? v2.Value : right;
var isInstance = leftValue is IPythonInstance || rightValue is IPythonInstance;

var leftType = left.GetPythonType();
var rightType = right.GetPythonType();

Expand Down Expand Up @@ -121,7 +124,7 @@ private IMember GetValueFromBinaryOp(Expression expr, LookupOptions lookupOption

if (leftIsSupported && rightIsSupported) {
if (TryGetValueFromBuiltinBinaryOp(op, leftTypeId, rightTypeId, Interpreter.LanguageVersion.Is3x(), out var member)) {
return member;
return isInstance ? new PythonInstance(member.GetPythonType()) : member;
}
}

Expand All @@ -136,11 +139,10 @@ private IMember GetValueFromBinaryOp(Expression expr, LookupOptions lookupOption
ret = CallOperator(op, left, leftType, right, rightType, expr, tryLeft: false);
}

if (!ret.IsUnknown()) {
return ret;
if (ret.IsUnknown()) {
ret = op.IsComparison() ? Interpreter.GetBuiltinType(BuiltinTypeId.Bool) : left;
}

return op.IsComparison() ? Interpreter.GetBuiltinType(BuiltinTypeId.Bool) : left;
return isInstance ? new PythonInstance(ret.GetPythonType()) : ret;
}

if (rightIsSupported) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public IDisposable OpenScope(IPythonModule module, ScopeStatement node, out Scop
// node points to global scope, it is not a function or a class.
scope = gs;
} else {
scope = outerScope.Children.OfType<Scope>().FirstOrDefault(s => s.Node == node);
scope = outerScope.GetChildScope(node) as Scope;
if (scope == null) {
scope = new Scope(node, outerScope, Module);
outerScope.AddChildScope(scope);
Expand Down
11 changes: 10 additions & 1 deletion src/Analysis/Ast/Impl/Analyzer/Handlers/AssignmentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System.Diagnostics;
using System.Linq;
using Microsoft.Python.Analysis.Specializations.Typing;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Parsing.Ast;
Expand All @@ -29,14 +30,22 @@ public void HandleAssignment(AssignmentStatement node, LookupOptions lookupOptio
}

// Filter out parenthesis expression in assignment because it makes no difference.
var lhs = node.Left.Select(s => s.RemoveParenthesis());
var lhs = node.Left.Select(s => s.RemoveParenthesis()).ToArray();

// Note that this is handling assignments of the same value to multiple variables,
// i.e. with "x = y = z = value", x/y/z are the items in lhs. If an expression looks
// like "x, y, z = value", then "x, y, z" is a *single* lhs value and its unpacking
// will be handled by AssignToExpr.
var value = ExtractRhs(node.Right, lhs.FirstOrDefault(), lookupOptions);
if (value != null) {
// Named tuple may get assigned to variable that has name different from the tuple itself.
// Then the name may end up conflicting with other types in module when stub merges with
// module types. For example, 'tokenize' stub declares _TokenInfo = NamedTuple('TokenInfo', ...)
// but there is also 'class TokenInfo(_TokenInfo)' so we have to use the variable name
// in order to avoid type naming conflicts.
if (value is ITypingNamedTupleType nt && lhs.Length == 1 && lhs[0] is NameExpression nex) {
nt.SetName(nex.Name);
}
foreach (var expr in lhs) {
AssignToExpr(expr, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void EnsureModule(in PythonVariableModule variableModule) {
EnsureModule(module);
}

public void EnsureModule(in IPythonModule module) {
private void EnsureModule(in IPythonModule module) {
if (module == null || _isCanceled()) {
return;
}
Expand All @@ -104,15 +104,9 @@ public void EnsureModule(in IPythonModule module) {
}

public ModuleWalker WalkModule(IPythonModule module, PythonAst ast) {
var analyzer = _services.GetService<IPythonAnalyzer>();
var analysis = analyzer.TryRestoreCachedAnalysis(module);
if (analysis != null) {
return null;
}

// If module has stub, make sure it is processed too.
if (module.Stub?.Analysis is EmptyAnalysis) {
WalkModule(module.Stub, module.GetAst());
if (module.Stub?.Analysis is EmptyAnalysis && module.Stub.GetAst() != null) {
WalkModule(module.Stub, module.Stub.GetAst());
}

var eval = new ExpressionEval(_services, module, ast);
Expand Down
Loading