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

Add persistence tests for 60+ system modules #1429

Merged
merged 216 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
216 commits
Select commit Hold shift + click to select a range
b605b76
Remove old qualified name
May 17, 2019
5471ce4
Node storage
May 17, 2019
8a8d847
Class and scope to use AST map
May 17, 2019
89ce2ca
Library analysis
May 17, 2019
7a93bf7
Fix SO
May 18, 2019
2a2bdf8
Keep small AST with imports
May 18, 2019
3edaa73
AST reduction
May 18, 2019
73371e1
Final field
May 18, 2019
7d60edc
Initial
May 20, 2019
02475f4
Reload
May 22, 2019
835eeac
Merge master
May 25, 2019
6b6e928
Ignore post-final requests
May 25, 2019
9692397
Drop AST
May 25, 2019
080beab
Remove local variables
May 25, 2019
93a6915
Test fixes
May 26, 2019
cadd7ce
Fix overload match
May 26, 2019
f61b1a7
Tests
May 26, 2019
1b20326
Add locks
May 27, 2019
d7efdac
Remove local variables
May 25, 2019
98934d4
Drop file content to save memory
May 27, 2019
417ae03
Cache PEP hints
May 28, 2019
ec5605f
Recreate AST
May 28, 2019
713d87f
Fix specialization
May 28, 2019
50e63e6
Fix locations
May 28, 2019
fcd0c06
usings
May 28, 2019
f6a992b
Test fixes
May 28, 2019
acad202
Add options to keep data in memory
May 28, 2019
f176b2f
Merge branch 'master' of https://github.com/microsoft/python-language…
May 28, 2019
4225337
Merge master
May 28, 2019
86e36a6
Fix test
May 28, 2019
ba97e9f
Fix lambda parameters
May 28, 2019
ffed87e
Fix argument set
May 29, 2019
186a9c6
Fix overload doc
May 29, 2019
4196699
Fix stub merge errors
May 29, 2019
93249d4
Fix async issues
May 29, 2019
5e61392
Undo some changes
May 29, 2019
130b95d
Fix test
May 29, 2019
a6676f1
Fix race condition
May 30, 2019
18b61ad
Merge master
Jun 1, 2019
0850544
Merge master
Jun 3, 2019
7d0bf5b
Merge branch 'proxymod' into db
Jun 4, 2019
663dc8f
Partial
Jun 4, 2019
86544a6
Models and views
Jun 4, 2019
67b06c2
Merge branch 'master' of https://github.com/microsoft/python-language…
Jun 4, 2019
9daf4a9
Merge master
Jun 4, 2019
90318e9
Restore log null checks
Jun 4, 2019
b79d918
Merge master
Jun 4, 2019
a02c6f3
Fix merge conflict
Jun 4, 2019
8a6b055
Merge master
Jun 5, 2019
57358eb
Fix merge issue
Jun 5, 2019
41ef00f
Merge branch 'master' of https://github.com/microsoft/python-language…
Jun 5, 2019
814cd64
Merge master
Jun 6, 2019
6ceb9b8
Null check
Jun 6, 2019
74bb061
Merge branch 'noast5' into db
Jun 6, 2019
478ce37
Partial
Jun 6, 2019
cb46e68
Partial
Jun 6, 2019
ef2981c
Partial
Jun 6, 2019
2164ad5
Fix test
Jun 6, 2019
1a48790
Merge branch 'noast5' into db
Jun 6, 2019
319c416
Partial
Jun 6, 2019
86b0ee6
Partial
Jun 7, 2019
1670c9d
First test
Jun 7, 2019
ab69cfd
Baseline comparison
Jun 7, 2019
e00c197
Builtins
Jun 7, 2019
4e1657c
Partial
Jun 7, 2019
ade00f4
Type fixes
Jun 10, 2019
e2fc221
Fix type names, part I
Jun 11, 2019
aaf40bb
Qualified name
Jun 11, 2019
a5b3b20
Properly write variables
Jun 11, 2019
f23a487
Partial
Jun 11, 2019
e6373a7
Construct module from model
Jun 12, 2019
6f6737d
Test
Jun 12, 2019
081f475
Variable creations
Jun 12, 2019
42fa4dc
Factories
Jun 13, 2019
2f03cb9
Factories
Jun 13, 2019
1dc3339
Split construction
Jun 13, 2019
9535bad
Restore
Jun 13, 2019
bec2a82
Save builtins
Jun 13, 2019
80c9b1c
Test passes
Jun 13, 2019
5b372f7
Qualified name
Jun 13, 2019
3066554
Better export detection
Jun 14, 2019
91491d4
Test fixes
Jun 14, 2019
bc4f587
More consistent qualified names
Jun 14, 2019
af69880
Sys test
Jun 14, 2019
5c1a0c3
Demo
Jun 14, 2019
4146690
Complete sys write/read
Jun 17, 2019
48763aa
Partial
Jun 17, 2019
93bece3
Partial
Jun 18, 2019
55b6a3f
Test staility
Jun 18, 2019
5e9bf8e
Perf bug
Jun 18, 2019
c152e67
Merge master
Jun 18, 2019
eadd622
Baseline, remove debug code, deactivate db
Jun 18, 2019
ba87581
Test fixes
Jun 18, 2019
a797593
Test fix
Jun 18, 2019
db6c7fc
Simplify a bit
Jun 19, 2019
f73fed0
Baselines and use : separator
Jun 19, 2019
4054d5a
Baselines
Jun 19, 2019
4ef96d8
PR feedback
Jun 19, 2019
c7436ed
Merge branch 'master' of https://github.com/microsoft/python-language…
Jun 19, 2019
f3860e6
Merge master
Jun 19, 2019
cab0fce
Remove registry reference
Jun 19, 2019
b9c1e14
PR feedback
Jun 19, 2019
4fbdcb9
Merge master
Jul 11, 2019
c7b1a5d
Merge db
Jul 11, 2019
90d1429
PR feedback
Jul 11, 2019
10289c3
Merge branch 'master' of https://github.com/microsoft/python-language…
Jul 12, 2019
facb062
Restore persistence + update test
Jul 12, 2019
d80604a
Better handle persistent module in dependencies
Jul 14, 2019
0705812
Merge master
Jul 15, 2019
96a5b82
Merge master
Jul 15, 2019
360f922
Undo
Jul 15, 2019
1015c1b
Add location converter abstraction
Jul 15, 2019
2ab07d6
Store member location
Jul 15, 2019
57c24c5
Fix merge issue
Jul 15, 2019
4779813
Basic locations test
Jul 15, 2019
7e6fb63
Navigation
Jul 15, 2019
9ee9bfe
Add test
Jul 16, 2019
fc66756
Update baselines
Jul 16, 2019
3b0fb23
Type restore - initial
Jul 17, 2019
8192ff2
Remove debug code
Jul 17, 2019
e27218f
Partial
Jul 17, 2019
ae4e178
Fix stub merge
Jul 18, 2019
2209ec1
Various model fixes
Jul 19, 2019
73498eb
Improve module handling
Jul 19, 2019
fcfb2bb
Qualified name improvements
Jul 23, 2019
54fa335
Fix unbound case
Jul 23, 2019
bb0b415
Improve stub merge
Jul 23, 2019
3b213bc
Fix qualified names of typing
Jul 23, 2019
eaa9e37
Handle stub-only modules
Jul 23, 2019
05eb692
Merge branch 'db' of https://github.com/microsoft/python-language-ser…
Jul 23, 2019
5193858
Merge branch 'dbloc' into dbtype
Jul 23, 2019
b888b2a
Add tests for io, re and sys
Jul 24, 2019
40554b0
Better handle named tuple
Jul 24, 2019
fcd2bd5
Handle module circular references
Jul 24, 2019
28f5b6c
Handle else in platform and version clauses + handle it in symbol col…
Jul 24, 2019
8fdf769
Formatting
Jul 24, 2019
08b5971
Fix walk of multi-level if statement
Jul 24, 2019
67fed10
Unify package search in imports
Jul 24, 2019
247a8c3
Fix tests
Jul 25, 2019
2bd4749
Undo change
Jul 25, 2019
5ead9ec
Port changes from dbtype
Jul 25, 2019
4090ca0
Merge fixes from upstream
Jul 25, 2019
97f1716
Merge master
Jul 25, 2019
59f4b83
Partial
Jul 26, 2019
91a0bb5
Named tuple support
Jul 29, 2019
362800c
Baseline updates
Jul 29, 2019
32e8bfc
Debug code
Jul 29, 2019
0d65468
Support types
Jul 30, 2019
f42d453
Merge branch 'master' into dbtype
Jul 30, 2019
3a1fd40
Merge upstream
Jul 30, 2019
9e3011d
Properly compare class member declaring type
Jul 30, 2019
eba1f45
Nested classes and functions persistence
Jul 30, 2019
12b0269
Undo debug
Jul 30, 2019
234390d
Fix numpy restore
Jul 30, 2019
263eabf
Baselines
Jul 30, 2019
f065110
Fix tests
Jul 31, 2019
b2b0501
Update AnyStr test reflecting changes to AnyStr behavior
Jul 31, 2019
28fa723
Exclude baselines from git diff
Jul 31, 2019
b58e893
Fix gitattr
Jul 31, 2019
f110a53
Move git setting to root
Jul 31, 2019
21c1660
Try no path
Jul 31, 2019
96d38b2
Test fixes
Jul 31, 2019
7ee6322
Undo change
Jul 31, 2019
db4aab1
Additional stub merge fixes
Aug 2, 2019
f72818e
Baseline updates
Aug 2, 2019
ed1a728
Fix goto def
Aug 4, 2019
8ee1fd0
Protect specific type creation
Aug 5, 2019
0ce7bad
Track documentaton source
Aug 5, 2019
f9aff94
Merge
Aug 5, 2019
088ce1b
More reliable tests + simplification
Aug 5, 2019
c1b93d8
Typo
Aug 5, 2019
62ac1c2
Cleanup
Aug 6, 2019
b719956
Basic classification
Aug 6, 2019
14671ae
Merge #1340
Aug 6, 2019
7983c75
Merge master
Aug 6, 2019
6b207ae
Merge master
Aug 6, 2019
a9b7ff0
Fix merge error
Aug 6, 2019
ac9d1cb
Module unique id fixes
Aug 6, 2019
23a9b81
Stricted check to save analysis
Aug 6, 2019
1d86086
Revert "Fix tests"
Aug 7, 2019
67350e3
Revert "Unify package search in imports"
Aug 7, 2019
01d7f2f
Merge branch '1376' into dbtype
Aug 7, 2019
f687dc8
Don't clear scope variables with inner classes
Aug 7, 2019
68b479b
Fix typo
Aug 7, 2019
eac2d40
Many new tests
Aug 7, 2019
f3261a3
Fix collections test
Aug 7, 2019
9825e3f
Fix CTypes
Aug 8, 2019
570a925
Initial
Aug 10, 2019
318b3cd
Update test
Aug 10, 2019
06112c6
Merge master
Aug 10, 2019
e0a37a9
Merge master
Aug 10, 2019
517a004
Merge master
Aug 11, 2019
f7717ac
Merge issues
Aug 11, 2019
4e4215c
Fix CTypes again
Aug 11, 2019
d6fa183
Merge branch '1231a' into dbsystest
Aug 11, 2019
2cbb89f
Fix null bases
Aug 11, 2019
58ae484
Tell between class members with/without self better
Aug 12, 2019
53f3af4
TypeVar support
Aug 12, 2019
11cb4b5
Add bound/covariant
Aug 12, 2019
9a92494
Fix Enum reassignments
Aug 12, 2019
e72285c
Fix Random
Aug 12, 2019
a26822a
Fix import * over local declarations (Socket)
Aug 13, 2019
c924622
Move interface
Aug 13, 2019
a473a98
Fix reference search
Aug 13, 2019
3c9a912
Enable asyncio test
Aug 13, 2019
8406a62
Merge db
Aug 13, 2019
4853e98
Merge hover fixes
Aug 13, 2019
3eb2f41
More tests
Aug 13, 2019
3d10d45
Merge branch 'db' of https://github.com/microsoft/python-language-ser…
Aug 13, 2019
4dcdb25
Enable dataclasses
Aug 13, 2019
a5c3af6
Add inspect and gzip
Aug 13, 2019
6fff95d
More tests
Aug 13, 2019
7847eb0
Add setting
Aug 13, 2019
e36ede9
Add handling of import position relative to the variable
Aug 13, 2019
2270c3b
Caching level changes
Aug 13, 2019
359329f
Update baselines
Aug 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ public void DeclareVariable(string name, IMember value, VariableSource source)
public void DeclareVariable(string name, IMember value, VariableSource source, IPythonModule module)
=> DeclareVariable(name, value, source, new Location(module));

public void DeclareVariable(string name, IMember value, VariableSource source, Node location, bool overwrite = false)
public void DeclareVariable(string name, IMember value, VariableSource source, Node location, bool overwrite = true)
=> DeclareVariable(name, value, source, GetLocationOfName(location), overwrite);

public void DeclareVariable(string name, IMember value, VariableSource source, Location location, bool overwrite = false) {
public void DeclareVariable(string name, IMember value, VariableSource source, Location location, bool overwrite = true) {
var member = GetInScope(name);
if (member != null && !overwrite) {
return;
}
if (source == VariableSource.Import && value is IVariable v) {
CurrentScope.LinkVariable(name, v, location);
return;
}
var member = GetInScope(name);
if (member != null) {
if (!value.IsUnknown()) {
CurrentScope.DeclareVariable(name, value, source, location);
Expand Down
15 changes: 11 additions & 4 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/AssignmentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,25 @@ 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;
}

var m = Eval.LookupNameInScopes(ne.Name, out scope);
if(m?.MemberType == PythonMemberType.Class) {
// Ignore assignments to classes: enum.py does Enum = None
// which prevents persistence from restoring proper type from enum:Enum.
continue;
}

var source = value.IsGeneric() ? VariableSource.Generic : VariableSource.Declaration;
Eval.DeclareVariable(ne.Name, value ?? Module.Interpreter.UnknownType, source, Eval.GetLocationOfName(ne));
}
Expand Down Expand Up @@ -97,7 +104,7 @@ private void TryHandleClassVariable(AssignmentStatement node, IMember value) {
var cls = m.GetPythonType<IPythonClassType>();
if (cls != null) {
using (Eval.OpenScope(Eval.Module, cls.ClassDefinition, out _)) {
Eval.DeclareVariable(mex.Name, value, VariableSource.Declaration, Eval.GetLocationOfName(mex), true);
Eval.DeclareVariable(mex.Name, value, VariableSource.Declaration, Eval.GetLocationOfName(mex));
}
}
}
Expand Down
33 changes: 28 additions & 5 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/FromImportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void AssignVariables(FromImportStatement node, IImportSearchResult impor
// TODO: warn this is not a good style per
// TODO: https://docs.python.org/3/faq/programming.html#what-are-the-best-practices-for-using-import-in-a-module
// TODO: warn this is invalid if not in the global scope.
HandleModuleImportStar(variableModule, imports is ImplicitPackageImport);
HandleModuleImportStar(variableModule, imports is ImplicitPackageImport, node.StartIndex);
return;
}

Expand All @@ -68,14 +68,16 @@ private void AssignVariables(FromImportStatement node, IImportSearchResult impor
if (!string.IsNullOrEmpty(memberName)) {
var nameExpression = asNames[i] ?? names[i];
var variableName = nameExpression?.Name ?? memberName;
var exported = variableModule.Analysis?.GlobalScope.Variables[memberName] ?? variableModule.GetMember(memberName);
var variable = variableModule.Analysis?.GlobalScope?.Variables[memberName];
var exported = variable ?? variableModule.GetMember(memberName);
var value = exported ?? GetValueFromImports(variableModule, imports as IImportChildrenSource, memberName);
Eval.DeclareVariable(variableName, value, VariableSource.Import, nameExpression);
// Do not allow imported variables to override local declarations
Eval.DeclareVariable(variableName, value, VariableSource.Import, nameExpression, CanOverwriteVariable(variableName, node.StartIndex));
}
}
}

private void HandleModuleImportStar(PythonVariableModule variableModule, bool isImplicitPackage) {
private void HandleModuleImportStar(PythonVariableModule variableModule, bool isImplicitPackage, int importPosition) {
if (variableModule.Module == Module) {
// from self import * won't define any new members
return;
Expand All @@ -100,10 +102,31 @@ private void HandleModuleImportStar(PythonVariableModule variableModule, bool is
}

var variable = variableModule.Analysis?.GlobalScope?.Variables[memberName];
Eval.DeclareVariable(memberName, variable ?? member, VariableSource.Import);
// Do not allow imported variables to override local declarations
Eval.DeclareVariable(memberName, variable ?? member, VariableSource.Import, Eval.DefaultLocation, CanOverwriteVariable(memberName, importPosition));
}
}

private bool CanOverwriteVariable(string name, int importPosition) {
var v = Eval.CurrentScope.Variables[name];
if(v == null) {
return true; // Variable does not exist
}
// Allow overwrite if import is below the variable. Consider
// x = 1
// x = 2
// from A import * # brings another x
// x = 3
var references = v.References.Where(r => r.DocumentUri == Module.Uri).ToArray();
if(references.Length == 0) {
// No references to the variable in this file - the variable
// is imported from another module. OK to overwrite.
return true;
}
var firstAssignmentPosition = references.Min(r => r.Span.ToIndexSpan(Ast).Start);
return firstAssignmentPosition < importPosition;
}

private IMember GetValueFromImports(PythonVariableModule parentModule, IImportChildrenSource childrenSource, string memberName) {
if (childrenSource == null || !childrenSource.TryGetChildImport(memberName, out var childImport)) {
return Interpreter.UnknownType;
Expand Down
6 changes: 5 additions & 1 deletion src/Analysis/Ast/Impl/Analyzer/ModuleWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private void MergeStub() {
switch (sourceType) {
case null:
// Nothing in sources, but there is type in the stub. Declare it.
if (v.Source == VariableSource.Declaration) {
if (v.Source == VariableSource.Declaration || v.Source == VariableSource.Generic) {
Eval.DeclareVariable(v.Name, v.Value, v.Source);
}
break;
Expand Down Expand Up @@ -347,6 +347,10 @@ private static bool IsStubBetterType(IPythonType sourceType, IPythonType stubTyp
if (sourceType.MemberType == PythonMemberType.Function && stubType.MemberType == PythonMemberType.Class) {
return true;
}
// Random replaces method (variable) by a function.
if (sourceType.MemberType == PythonMemberType.Method && stubType.MemberType == PythonMemberType.Function) {
return true;
}
return sourceType.MemberType == stubType.MemberType;
}

Expand Down
24 changes: 24 additions & 0 deletions src/Analysis/Ast/Impl/Definitions/AnalysisOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
// permissions and limitations under the License.

namespace Microsoft.Python.Analysis {
public enum AnalysisCachingLevel {
/// <summary>
/// No caching of analysis.
/// </summary>
None,

/// <summary>
/// Cache analysis results of system (language) modules.
/// Do not cache user-installed modules or site-packages.
/// </summary>
System,

/// <summary>
/// Full caching, includes system and library modules.
/// Does not enable caching of user code analysis.
/// </summary>
Library
}

public class AnalysisOptions {
public bool LintingEnabled { get; set; }

Expand All @@ -29,5 +48,10 @@ public class AnalysisOptions {
/// improve performance when library code has to be re-analyzed.
/// </summary>
public bool KeepLibraryAst { get; set; }

/// <summary>
/// Defines level of caching analysis engine will maintain.
/// </summary>
public AnalysisCachingLevel AnalysisCachingLevel { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ public interface IGenericTypeParameter: IPythonType, IEquatable<IGenericTypePara
/// </summary>
/// <remarks>See 'https://docs.python.org/3/library/typing.html#typing.TypeVar'</remarks>
IReadOnlyList<IPythonType> Constraints { get; }
IPythonType Bound { get; }
IPythonType Covariant { get; }
IPythonType Contravariant { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,33 @@

namespace Microsoft.Python.Analysis.Specializations.Typing.Types {
internal sealed class GenericTypeParameter : PythonType, IGenericTypeParameter {
public GenericTypeParameter(string name, IPythonModule declaringModule, IReadOnlyList<IPythonType> constraints, string documentation, IndexSpan location)
: base(name, new Location(declaringModule), documentation) {
public GenericTypeParameter(
string name,
IPythonModule declaringModule,
IReadOnlyList<IPythonType> constraints,
IPythonType bound,
IPythonType covariant,
IPythonType contravariant,
Location location)
: base(name, location, GetDocumentation(name, constraints, bound, covariant, contravariant)) {
Constraints = constraints ?? Array.Empty<IPythonType>();
Bound = bound;
Covariant = covariant;
Contravariant = contravariant;
}

#region IGenericTypeParameter
public IReadOnlyList<IPythonType> Constraints { get; }
public IPythonType Bound { get; }
public IPythonType Covariant { get; }
public IPythonType Contravariant { get; }
#endregion

#region IPythonType
public override BuiltinTypeId TypeId => BuiltinTypeId.Type;
public override PythonMemberType MemberType => PythonMemberType.Generic;
public override bool IsSpecialized => true;
#endregion

private static bool TypeVarArgumentsValid(IArgumentSet argSet) {
var args = argSet.Arguments;
Expand Down Expand Up @@ -90,23 +108,29 @@ public static IPythonType FromTypeVar(IArgumentSet argSet, IPythonModule declari
// Type constraints may be specified as type name strings.
var typeString = (a as IPythonConstant)?.GetString();
return !string.IsNullOrEmpty(typeString) ? argSet.Eval.GetTypeFromString(typeString) : a.GetPythonType();
}).ToArray() ?? Array.Empty<IPythonType>();
}).ToArray();

var bound = args.Where(a => a.Name == "bound").Select(a => a.Value as IPythonType).FirstOrDefault();
var covariant = args.Where(a => a.Name == "covariant").Select(a => a.Value as IPythonType).FirstOrDefault();
var contravariant = args.Where(a => a.Name == "contravariant").Select(a => a.Value as IPythonType).FirstOrDefault();

var documentation = GetDocumentation(args, constraints);
return new GenericTypeParameter(name, declaringModule, constraints, documentation, location);
return new GenericTypeParameter(name, declaringModule, constraints, bound, covariant, contravariant, new Location(declaringModule, location));
}

private static string GetDocumentation(IReadOnlyList<IArgument> args, IReadOnlyList<IPythonType> constraints) {
var name = (args[0].Value as IPythonConstant).GetString();
var keyWordArgs = args.Skip(1)
.Where(x => !x.ValueIsDefault)
.Select(x => $"{x.Name}={(x.Value as IPythonConstant)?.Value}");
private static string GetDocumentation(string name, IReadOnlyList<IPythonType> constraints, IPythonType bound, IPythonType covariant, IPythonType contravariant) {
var constaintStrings = constraints != null ? constraints.Select(c => c.IsUnknown() ? "?" : c.Name) : Enumerable.Empty<string>();
var boundStrings = bound.IsUnknown() ? Enumerable.Empty<string>() : Enumerable.Repeat($"bound={bound.Name}", 1);
var covariantStrings = covariant.IsUnknown() ? Enumerable.Empty<string>() : Enumerable.Repeat($"covariant={covariant.Name}", 1);
var contravariantStrings = contravariant.IsUnknown() ? Enumerable.Empty<string>() : Enumerable.Repeat($"contravariant={contravariant.Name}", 1);

var docArgs = Enumerable.Repeat($"'{name}'", 1)
.Concat(constaintStrings).Concat(boundStrings).Concat(covariantStrings).Concat(contravariantStrings);

var docArgs = constraints.Select(c => c.IsUnknown() ? "?" : c.Name).Concat(keyWordArgs).Prepend($"'{name}'");
var documentation = CodeFormatter.FormatSequence("TypeVar", '(', docArgs);
return documentation;
}

public bool Equals(IGenericTypeParameter other) => Name.Equals(other.Name);
public bool Equals(IGenericTypeParameter other) => Name.Equals(other?.Name);
}

}
2 changes: 2 additions & 0 deletions src/Analysis/Ast/Impl/Specializations/Typing/TypingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ private void SpecializeMembers() {
_members["ValuesView"] = new SpecializedGenericType("ValuesView", CreateValuesViewType, this);
_members["ItemsView"] = new SpecializedGenericType("ItemsView", CreateItemsViewType, this);

_members["AbstractSet"] = new SpecializedGenericType("AbstractSet",
typeArgs => CreateListType("AbstractSet", BuiltinTypeId.Set, typeArgs, true), this);
_members["Set"] = new SpecializedGenericType("Set",
typeArgs => CreateListType("Set", BuiltinTypeId.Set, typeArgs, true), this);
_members["MutableSet"] = new SpecializedGenericType("MutableSet",
Expand Down
10 changes: 6 additions & 4 deletions src/Analysis/Ast/Impl/Types/PythonClassType.Generics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Python.Analysis.Specializations.Typing;
using Microsoft.Python.Analysis.Utilities;
Expand All @@ -23,13 +24,14 @@

namespace Microsoft.Python.Analysis.Types {
internal partial class PythonClassType {
private readonly object _genericParameterLock = new object();
private readonly ReentrancyGuard<IPythonClassType> _genericSpecializationGuard = new ReentrancyGuard<IPythonClassType>();
private readonly ReentrancyGuard<IPythonClassType> _genericResolutionGuard = new ReentrancyGuard<IPythonClassType>();

private bool _isGeneric;
private object _genericParameterLock = new object();
private Dictionary<string, PythonClassType> _specificTypeCache;
private Dictionary<IGenericTypeParameter, IPythonType> _genericParameters;
private IReadOnlyList<IGenericTypeParameter> _parameters = new List<IGenericTypeParameter>();
private ReentrancyGuard<IPythonClassType> _genericSpecializationGuard = new ReentrancyGuard<IPythonClassType>();
private ReentrancyGuard<IPythonClassType> _genericResolutionGuard = new ReentrancyGuard<IPythonClassType>();

#region IGenericType
/// <summary>
Expand Down Expand Up @@ -66,7 +68,7 @@ public IPythonType CreateSpecificType(IArgumentSet args) {
// type parameter T -> int, U -> str, etc.
var genericTypeToSpecificType = GetSpecificTypes(args, genericTypeParameters, newBases);

PythonClassType classType = new PythonClassType(BaseName, new Location(DeclaringModule));
var classType = new PythonClassType(BaseName, new Location(DeclaringModule));
// Storing generic parameters allows methods returning generic types
// to know what type parameter returns what specific type
StoreGenericParameters(classType, genericTypeParameters, genericTypeToSpecificType);
Expand Down
6 changes: 3 additions & 3 deletions src/Analysis/Ast/Impl/Types/PythonClassType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal enum ClassDocumentationSource {

private readonly ReentrancyGuard<IPythonClassType> _memberGuard = new ReentrancyGuard<IPythonClassType>();
private string _genericName;
private List<IPythonType> _bases;
private List<IPythonType> _bases = new List<IPythonType>();
private IReadOnlyList<IPythonType> _mro;
private string _documentation;

Expand Down Expand Up @@ -187,7 +187,7 @@ public IReadOnlyList<IPythonType> Mro {
if (_mro != null) {
return _mro;
}
if (_bases == null) {
if (_bases.Count == 0) {
return new IPythonType[] { this };
}
_mro = new IPythonType[] { this };
Expand All @@ -211,7 +211,7 @@ public IReadOnlyList<IPythonType> Mro {
internal override void SetDocumentation(string documentation) => _documentation = documentation;

internal void SetBases(IEnumerable<IPythonType> bases) {
if (_bases != null) {
if (_bases.Count > 0) {
return; // Already set
}

Expand Down
3 changes: 1 addition & 2 deletions src/Analysis/Ast/Impl/Types/PythonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ public override void AddReference(Location location) {

#region IPythonType

public virtual string Name => TypeId == BuiltinTypeId.Ellipsis ? "..." : BaseName;

public virtual string QualifiedName
=> DeclaringModule.ModuleType == ModuleType.Builtins
? TypeId == BuiltinTypeId.Ellipsis ? "ellipsis" : Name
: this.GetQualifiedName();

public virtual string Name => TypeId == BuiltinTypeId.Ellipsis ? "..." : BaseName;
public virtual string Documentation { get; private set; }
public virtual BuiltinTypeId TypeId => _typeId;
public string BaseName { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Ast/Impl/Utilities/ReentrancyGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IDisposable Push(T t, out bool reentered) {

public void Pop() {
_stack.Value.Pop();
if(_stack.Value.Count == 0) {
if (_stack.Value.Count == 0) {
_stack.Value = null;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Caching/Impl/Extensions/IndexSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Diagnostics;
using Microsoft.Python.Analysis.Caching.Models;
using Microsoft.Python.Core.Text;

namespace Microsoft.Python.Analysis.Caching {
internal static class IndexSpanExtensions {
[DebuggerStepThrough]
public static IndexSpanModel ToModel(this IndexSpan span) => new IndexSpanModel {
Start = span.Start,
Length = span.Length
Expand Down
Loading