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

Commit 1be750c

Browse files
author
Mikhail Arkhipov
committed
More fixes
1 parent 07ec4d4 commit 1be750c

16 files changed

+111
-146
lines changed

src/Analysis/Engine/Impl/AnalysisValueSetExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ internal static AnalysisValue GetUnionType(this IAnalysisSet types) {
218218
/// Gets instance representations of all members of the set.
219219
/// </summary>
220220
public static IAnalysisSet GetInstanceType(this IAnalysisSet types)
221-
=> AnalysisSet.Create(types.SelectMany(ns => ns.PythonType.IsTypeFactory ? ns : ns.GetInstanceType()));
221+
=> AnalysisSet.Create(types.SelectMany(ns => ns.PythonType?.IsTypeFactory == true ? ns : ns.GetInstanceType()));
222222

223223
public static bool IsUnknown(this IAnalysisSet res) {
224224
return res == null ||

src/Analysis/Engine/Impl/EmptyBuiltinModule.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,23 @@ public string Documentation {
5252

5353
#endregion
5454

55-
#region IMemberContainer Members
56-
57-
public IMember GetMember(IModuleContext context, string name) {
58-
return null;
59-
}
55+
#region IPythonType
56+
public IPythonModule DeclaringModule => null;
57+
public BuiltinTypeId TypeId => BuiltinTypeId.Module;
58+
public bool IsBuiltIn => true;
59+
public bool IsTypeFactory => false;
60+
public IPythonFunction GetConstructors() => null;
61+
#endregion
6062

63+
#region IMemberContainer
64+
public IMember GetMember(IModuleContext context, string name) => null;
6165
public IEnumerable<string> GetMemberNames(IModuleContext moduleContext) {
6266
yield break;
6367
}
64-
6568
#endregion
6669

67-
#region IMember Members
68-
69-
public PythonMemberType MemberType {
70-
get { return PythonMemberType.Module; }
71-
}
72-
70+
#region IMember
71+
public PythonMemberType MemberType => PythonMemberType.Module;
7372
#endregion
74-
7573
}
7674
}

src/Analysis/Engine/Impl/Interpreter/Ast/AstAnalysisWalker.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,9 @@ private static string GetDoc(SuiteStatement node) {
390390
private AstPythonClass CreateClass(ClassDefinition node) {
391391
node = node ?? throw new ArgumentNullException(nameof(node));
392392
return new AstPythonClass(node, _module,
393-
GetDoc(node.Body as SuiteStatement), GetLoc(node),
394-
BuiltinTypeId.Unknown, CreateBuiltinTypes);
393+
GetDoc(node.Body as SuiteStatement), GetLoc(node),
394+
CreateBuiltinTypes ? BuiltinTypeId.Unknown : BuiltinTypeId.Class, // built-ins set type later
395+
CreateBuiltinTypes);
395396
}
396397

397398
private void CollectTopLevelDefinitions() {

src/Analysis/Engine/Impl/Interpreter/Ast/AstNestedPythonModule.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
using Microsoft.PythonTools.Analysis.Infrastructure;
2424

2525
namespace Microsoft.PythonTools.Interpreter.Ast {
26-
class AstNestedPythonModule : IPythonModule, ILocatedMember {
27-
private string _name;
26+
class AstNestedPythonModule : PythonModuleType, IPythonModule, ILocatedMember {
2827
private IPythonModule _module;
2928
private readonly IPythonInterpreter _interpreter;
3029
private readonly IReadOnlyList<string> _importNames;
@@ -33,15 +32,12 @@ public AstNestedPythonModule(
3332
IPythonInterpreter interpreter,
3433
string name,
3534
IReadOnlyList<string> importNames
36-
) {
35+
) : base(name) {
3736
_interpreter = interpreter ?? throw new ArgumentNullException(nameof(interpreter));
38-
_name = name ?? throw new ArgumentNullException(nameof(name));
3937
_importNames = importNames ?? throw new ArgumentNullException(nameof(importNames));
4038
}
4139

42-
public string Name => MaybeModule?.Name ?? _name;
43-
public string Documentation => MaybeModule?.Documentation ?? string.Empty;
44-
public PythonMemberType MemberType => PythonMemberType.Module;
40+
public override string Documentation => MaybeModule?.Documentation ?? string.Empty;
4541
public IEnumerable<ILocationInfo> Locations => ((MaybeModule as ILocatedMember)?.Locations).MaybeEnumerate();
4642

4743
public bool IsLoaded => MaybeModule != null;
@@ -69,9 +65,10 @@ private IPythonModule GetModule() {
6965

7066
public IEnumerable<string> GetChildrenModules() => GetModule().GetChildrenModules();
7167

72-
public IMember GetMember(IModuleContext context, string name) => GetModule().GetMember(context, name);
68+
public override IMember GetMember(IModuleContext context, string name)
69+
=> GetModule().GetMember(context, name);
7370

74-
public IEnumerable<string> GetMemberNames(IModuleContext context) =>
71+
public override IEnumerable<string> GetMemberNames(IModuleContext context) =>
7572
// TODO: Make GetMemberNames() faster than Imported()
7673
GetModule().GetMemberNames(context);
7774

src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonModule.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,20 @@
2525
using Microsoft.PythonTools.Parsing.Ast;
2626

2727
namespace Microsoft.PythonTools.Interpreter.Ast {
28-
sealed class AstPythonModule : IPythonModule, IProjectEntry, ILocatedMember {
28+
sealed class AstPythonModule : PythonModuleType, IPythonModule, IProjectEntry, ILocatedMember {
2929
private readonly IPythonInterpreter _interpreter;
3030
private readonly List<string> _childModules = new List<string>();
3131
private readonly Dictionary<string, IMember> _members = new Dictionary<string, IMember>();
3232
private bool _foundChildModules;
3333
private string _documentation = string.Empty;
3434

35-
internal AstPythonModule() {
36-
Name = string.Empty;
37-
FilePath = string.Empty;
35+
internal AstPythonModule(): base(string.Empty) {
36+
FilePath = string.Empty;
3837
_foundChildModules = true;
3938
}
4039

41-
internal AstPythonModule(string moduleName, IPythonInterpreter interpreter, PythonAst ast, string filePath, IEnumerable<string> parseErrors) {
42-
Name = moduleName;
40+
internal AstPythonModule(string moduleName, IPythonInterpreter interpreter, PythonAst ast, string filePath, IEnumerable<string> parseErrors)
41+
: base(moduleName) {
4342
_documentation = ast.Documentation;
4443
FilePath = filePath;
4544
DocumentUri = ProjectEntry.MakeDocumentUri(FilePath);
@@ -73,8 +72,7 @@ internal void AddChildModule(string name, IPythonModule module) {
7372

7473
public void Dispose() { }
7574

76-
public string Name { get; }
77-
public string Documentation {
75+
public override string Documentation {
7876
get {
7977
if (_documentation == null) {
8078
_members.TryGetValue("__doc__", out var m);
@@ -92,7 +90,6 @@ public string Documentation {
9290
}
9391
public string FilePath { get; }
9492
public Uri DocumentUri { get; }
95-
public PythonMemberType MemberType => PythonMemberType.Module;
9693
public Dictionary<object, object> Properties { get; } = new Dictionary<object, object>();
9794
public IEnumerable<ILocationInfo> Locations { get; }
9895

@@ -136,7 +133,7 @@ public IEnumerable<string> GetChildrenModules() {
136133
}
137134
}
138135

139-
public IMember GetMember(IModuleContext context, string name) {
136+
public override IMember GetMember(IModuleContext context, string name) {
140137
IMember member = null;
141138
lock (_members) {
142139
_members.TryGetValue(name, out member);
@@ -150,7 +147,7 @@ public IMember GetMember(IModuleContext context, string name) {
150147
return member;
151148
}
152149

153-
public IEnumerable<string> GetMemberNames(IModuleContext moduleContext) {
150+
public override IEnumerable<string> GetMemberNames(IModuleContext moduleContext) {
154151
lock (_members) {
155152
return _members.Keys.ToArray();
156153
}

src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonMultipleMembers.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ public MultipleModuleMembers(IMember[] members) : base(members) { }
215215
public IEnumerable<string> GetMemberNames(IModuleContext moduleContext) => Modules.SelectMany(m => m.GetMemberNames(moduleContext)).Distinct();
216216
public override PythonMemberType MemberType => PythonMemberType.Module;
217217

218+
public IPythonModule DeclaringModule => null;
219+
public BuiltinTypeId TypeId => BuiltinTypeId.Module;
220+
public bool IsBuiltIn => true;
221+
public bool IsTypeFactory => false;
222+
218223
public void Imported(IModuleContext context) {
219224
List<Exception> exceptions = null;
220225
foreach (var m in Modules) {
@@ -229,6 +234,8 @@ public void Imported(IModuleContext context) {
229234
throw new AggregateException(exceptions);
230235
}
231236
}
237+
238+
public IPythonFunction GetConstructors() => null;
232239
}
233240

234241
class MultipleTypeMembers : AstPythonMultipleMembers, IPythonType {

src/Analysis/Engine/Impl/Interpreter/Ast/AstScrapedPythonModule.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
1010
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
1111
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12-
// MERCHANTABLITY OR NON-INFRINGEMENT.
12+
// MERCHANTABILITY OR NON-INFRINGEMENT.
1313
//
1414
// See the Apache Version 2.0 License for specific language governing
1515
// permissions and limitations under the License.
@@ -27,7 +27,7 @@
2727
using Microsoft.PythonTools.Parsing.Ast;
2828

2929
namespace Microsoft.PythonTools.Interpreter.Ast {
30-
class AstScrapedPythonModule : IPythonModule
30+
class AstScrapedPythonModule : PythonModuleType, IPythonModule
3131
#if DEBUG
3232
// In debug builds we let you F12 to the scraped file
3333
, ILocatedMember
@@ -37,28 +37,23 @@ class AstScrapedPythonModule : IPythonModule
3737
protected readonly Dictionary<string, IMember> _members;
3838
private bool _scraped;
3939

40-
public AstScrapedPythonModule(string name, string filePath) {
41-
Name = name ?? throw new ArgumentNullException(nameof(name));
40+
public AstScrapedPythonModule(string name, string filePath): base(name) {
4241
ParseErrors = Enumerable.Empty<string>();
4342
_filePath = filePath;
4443
_members = new Dictionary<string, IMember>();
4544
_scraped = false;
4645
}
4746

48-
public string Name { get; }
49-
50-
public string Documentation {
47+
public override string Documentation {
5148
get {
5249
var m = GetMember(null, "__doc__") as AstPythonStringLiteral;
5350
return m != null ? m.Value : string.Empty;
5451
}
5552
}
5653

57-
public PythonMemberType MemberType => PythonMemberType.Module;
58-
5954
public IEnumerable<string> GetChildrenModules() => Enumerable.Empty<string>();
6055

61-
public virtual IMember GetMember(IModuleContext context, string name) {
56+
public override IMember GetMember(IModuleContext context, string name) {
6257
IMember m;
6358
if (!_scraped) {
6459
Imported(context);
@@ -75,7 +70,7 @@ public virtual IMember GetMember(IModuleContext context, string name) {
7570
return m;
7671
}
7772

78-
public virtual IEnumerable<string> GetMemberNames(IModuleContext moduleContext) {
73+
public override IEnumerable<string> GetMemberNames(IModuleContext moduleContext) {
7974
if (!_scraped) {
8075
Imported(moduleContext);
8176
}

src/Analysis/Engine/Impl/Interpreter/Ast/AstTypeAnnotationConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public ModuleType(IPythonModule module):
221221

222222
private class UnionType : AstPythonType, IPythonMultipleMembers {
223223
public UnionType(IReadOnlyList<IPythonType> types):
224-
base("Any", null, null, null) {
224+
base("Any", types.Select(t => t.DeclaringModule).ExcludeDefault().FirstOrDefault(), null, null) {
225225
Types = types;
226226
}
227227

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Python Tools for Visual Studio
2+
// Copyright(c) Microsoft Corporation
3+
// All rights reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the License); you may not use
6+
// this file except in compliance with the License. You may obtain a copy of the
7+
// License at http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
10+
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
11+
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12+
// MERCHANTABILITY OR NON-INFRINGEMENT.
13+
//
14+
// See the Apache Version 2.0 License for specific language governing
15+
// permissions and limitations under the License.
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
21+
namespace Microsoft.PythonTools.Interpreter.Ast {
22+
class PythonModuleType: IPythonType {
23+
public PythonModuleType(string name) {
24+
Name = name ?? throw new ArgumentNullException(nameof(name));
25+
}
26+
27+
#region IPythonType
28+
public string Name { get; }
29+
public virtual string Documentation { get; }
30+
31+
public virtual IPythonModule DeclaringModule => null;
32+
public BuiltinTypeId TypeId => BuiltinTypeId.Module;
33+
public bool IsBuiltIn => true;
34+
public bool IsTypeFactory => false;
35+
public IPythonFunction GetConstructors() => null;
36+
#endregion
37+
38+
#region IMember
39+
public PythonMemberType MemberType => PythonMemberType.Module;
40+
#endregion
41+
42+
#region IMemberContainer
43+
public virtual IMember GetMember(IModuleContext context, string name) => null;
44+
public virtual IEnumerable<string> GetMemberNames(IModuleContext moduleContext) => Enumerable.Empty<string>();
45+
#endregion
46+
}
47+
}

src/Analysis/Engine/Impl/Interpreter/Definitions/IPythonModule.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
1010
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
1111
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12-
// MERCHANTABLITY OR NON-INFRINGEMENT.
12+
// MERCHANTABILITY OR NON-INFRINGEMENT.
1313
//
1414
// See the Apache Version 2.0 License for specific language governing
1515
// permissions and limitations under the License.
@@ -20,22 +20,9 @@ namespace Microsoft.PythonTools.Interpreter {
2020
/// <summary>
2121
/// Represents a Python module which members can be imported from.
2222
/// </summary>
23-
public interface IPythonModule : IMemberContainer, IMember {
24-
string Name {
25-
get;
26-
}
27-
23+
public interface IPythonModule : IPythonType {
2824
IEnumerable<string> GetChildrenModules();
2925

3026
void Imported(IModuleContext context);
31-
32-
/// <summary>
33-
/// The documentation of the module
34-
///
35-
/// New in 1.1.
36-
/// </summary>
37-
string Documentation {
38-
get;
39-
}
4027
}
4128
}

0 commit comments

Comments
 (0)