Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
8bca56b
Fix #668 (partial)
Mar 1, 2019
a731c2a
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 1, 2019
069910b
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
7ffc9db
Tests
Mar 5, 2019
6303c45
Revert "Tests"
Mar 5, 2019
945451c
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
034e437
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 5, 2019
f997d68
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 6, 2019
0e4ff95
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 11, 2019
878c8c5
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 13, 2019
35e1033
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 13, 2019
1073711
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 14, 2019
86028da
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 14, 2019
4d4bca7
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 18, 2019
ac04f76
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 18, 2019
bab03da
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 20, 2019
85ec685
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 27, 2019
be7466d
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 28, 2019
01781c0
Exp
Mar 27, 2019
f544f8a
Limit concurrency
Mar 28, 2019
01bd719
Concurrency limit
Mar 28, 2019
2f8e564
Drop cache after analysis
Mar 28, 2019
9a933bd
Merge branch 'master' of https://github.com/Microsoft/python-language…
Mar 28, 2019
4ad56bb
Fix regression
Mar 28, 2019
c280c55
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 3, 2019
b21b8b8
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 7, 2019
babaee4
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 8, 2019
092efd6
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 8, 2019
b97594d
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 10, 2019
2a50eb9
Fix test
Apr 10, 2019
9c7f58d
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 10, 2019
00faa5f
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 12, 2019
4c8aa92
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 12, 2019
1a67bc8
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 12, 2019
7a13215
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 16, 2019
3d907d8
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 16, 2019
94a274f
Merge branch 'master' of https://github.com/Microsoft/python-language…
Apr 29, 2019
b341d5a
Optimize variable collection
May 5, 2019
1ca3b08
Clean
May 5, 2019
81dbd3c
Special type of imported variable
May 5, 2019
cc93a0e
Merge master
May 6, 2019
b9da9a5
Merge issues
May 6, 2019
a71d0e8
Merge fixes
May 6, 2019
561f0eb
Fix test
May 6, 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
3 changes: 1 addition & 2 deletions src/Analysis/Ast/Impl/Modules/PythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ private enum State {
private object AnalysisLock { get; } = new object();
private State ContentState { get; set; } = State.None;

protected PythonModule(string name, ModuleType moduleType, IServiceContainer services)
: base(PythonMemberType.Module) {
protected PythonModule(string name, ModuleType moduleType, IServiceContainer services) : base(null) {
Name = name ?? throw new ArgumentNullException(nameof(name));
Services = services ?? throw new ArgumentNullException(nameof(services));
ModuleType = moduleType;
Expand Down
6 changes: 3 additions & 3 deletions src/Analysis/Ast/Impl/Modules/PythonVariableModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ internal sealed class PythonVariableModule : LocatedMember, IPythonModule, IEqua
public IGlobalScope GlobalScope => Module?.GlobalScope;
public BuiltinTypeId TypeId => BuiltinTypeId.Module;
public Uri Uri => Module?.Uri;
public override PythonMemberType MemberType => PythonMemberType.Module;

public PythonVariableModule(string name, IPythonInterpreter interpreter)
: base(PythonMemberType.Module) {
public PythonVariableModule(string name, IPythonInterpreter interpreter): base(null) {
Name = name;
Interpreter = interpreter;
SetDeclaringModule(this);
}

public PythonVariableModule(IPythonModule module): base(PythonMemberType.Module, module) {
public PythonVariableModule(IPythonModule module): base(module) {
Name = module.Name;
Interpreter = module.Interpreter;
Module = module;
Expand Down
4 changes: 2 additions & 2 deletions src/Analysis/Ast/Impl/Specializations/Typing/Types/AnyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

namespace Microsoft.Python.Analysis.Specializations.Typing.Types {
internal sealed class AnyType : LocatedMember, IPythonType {
public AnyType(IPythonModule declaringModule)
: base(PythonMemberType.Class, declaringModule) { }
public AnyType(IPythonModule declaringModule) : base(declaringModule) { }

public override PythonMemberType MemberType => PythonMemberType.Class;
public string Name => "Any";
public BuiltinTypeId TypeId => BuiltinTypeId.Type;
public string Documentation => Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ public GenericType(
Parameters = parameters ?? Array.Empty<IGenericTypeDefinition>();
}

private GenericType(string name, IPythonModule declaringModule)
: base(PythonMemberType.Generic, declaringModule) {
private GenericType(string name, IPythonModule declaringModule) : base(declaringModule) {
Name = name ?? throw new ArgumentNullException(nameof(name));
}

public override PythonMemberType MemberType => PythonMemberType.Generic;

/// <summary>
/// Type parameters such as in Tuple[T1, T2. ...] or
/// Generic[_T1, _T2, ...] as returned by TypeVar.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ namespace Microsoft.Python.Analysis.Specializations.Typing.Values {
internal sealed class TypingType : LocatedMember, IPythonType {
private readonly IPythonType _type;

public TypingType(IPythonModule declaringModule, IPythonType type)
: base(PythonMemberType.Class, declaringModule) {
public TypingType(IPythonModule declaringModule, IPythonType type): base(declaringModule) {
_type = type ?? throw new ArgumentNullException(nameof(type));
Name = $"Type[{_type.Name}]";
}

public override PythonMemberType MemberType => PythonMemberType.Class;
public string Name { get; }
public BuiltinTypeId TypeId => BuiltinTypeId.Type;
public string Documentation => Name;
Expand Down
26 changes: 26 additions & 0 deletions src/Analysis/Ast/Impl/Types/Definitions/IImportedMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.

namespace Microsoft.Python.Analysis.Types {
/// <summary>
/// Imported member that has link to its parent in another module.
/// </summary>
public interface IImportedMember : ILocatedMember {
/// <summary>
/// Link to the primary definition such as when variable is imported from another file.
/// </summary>
ILocatedMember Parent { get; }
}
}
5 changes: 0 additions & 5 deletions src/Analysis/Ast/Impl/Types/Definitions/ILocatedMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public interface ILocatedMember: IMember {
/// </summary>
LocationInfo Definition { get; }

/// <summary>
/// Link to the primary definition such as when variable is imported from another file.
/// </summary>
ILocatedMember Parent { get; }

/// <summary>
/// List of references to the member.
/// </summary>
Expand Down
20 changes: 3 additions & 17 deletions src/Analysis/Ast/Impl/Types/LocatedMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,18 @@ namespace Microsoft.Python.Analysis.Types {
internal abstract class LocatedMember : ILocatedMember {
private HashSet<Location> _references;

protected LocatedMember(PythonMemberType memberType) {
MemberType = memberType;
}

protected LocatedMember(PythonMemberType memberType, IPythonModule module)
: this(memberType, new Location(module, default)) { }

protected LocatedMember(PythonMemberType memberType, Location location, ILocatedMember parent = null)
: this(location, parent) {
MemberType = memberType;
}
protected LocatedMember(IPythonModule module) : this(new Location(module, default)) { }

private LocatedMember(Location location, ILocatedMember parent = null) {
Parent = parent;
Parent?.AddReference(location);
protected LocatedMember(Location location) {
Location = location;
}

public virtual PythonMemberType MemberType { get; } = PythonMemberType.Unknown;
public abstract PythonMemberType MemberType { get; }

public virtual IPythonModule DeclaringModule => Location.Module;

public virtual LocationInfo Definition => Location.LocationInfo;

public ILocatedMember Parent { get; }

public virtual IReadOnlyList<LocationInfo> References {
get {
Expand Down Expand Up @@ -98,7 +85,6 @@ protected EmptyLocatedMember(PythonMemberType memberType) {
public PythonMemberType MemberType { get; }
public IPythonModule DeclaringModule => null;
public LocationInfo Definition => LocationInfo.Empty;
public ILocatedMember Parent => null;
public IReadOnlyList<LocationInfo> References => Array.Empty<LocationInfo>();
public void AddReference(Location location) { }
public void RemoveReferences(IPythonModule module) { }
Expand Down
8 changes: 5 additions & 3 deletions src/Analysis/Ast/Impl/Types/PythonFunctionOverload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ public PythonFunctionOverload(FunctionDefinition fd, IPythonClassMember classMem
_returnDocumentation = ast != null ? fd.ReturnAnnotation?.ToCodeString(ast) : null;
}

public PythonFunctionOverload(string name, Location location)
: base(PythonMemberType.Function, location) {
public PythonFunctionOverload(string name, Location location) : base(location) {
Name = name ?? throw new ArgumentNullException(nameof(name));
}

#region ILocatedMember
public override PythonMemberType MemberType => PythonMemberType.Function;
#endregion

internal void SetParameters(IReadOnlyList<IParameterInfo> parameters) => Parameters = parameters;

internal void SetDocumentationProvider(Func<string, string> documentationProvider)
Expand Down Expand Up @@ -147,7 +150,6 @@ public string GetReturnDocumentation(IPythonType self = null) {
}

public IReadOnlyList<IParameterInfo> Parameters { get; private set; } = Array.Empty<IParameterInfo>();
public override PythonMemberType MemberType => PythonMemberType.Function;
public IMember StaticReturnValue { get; private set; }

public IMember Call(IArgumentSet args, IPythonType self, Node callLocation = null) {
Expand Down
7 changes: 5 additions & 2 deletions src/Analysis/Ast/Impl/Types/PythonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ public PythonType(
_documentationProvider = documentationProvider;
}

private PythonType(string name, Location location, BuiltinTypeId typeId)
: base(typeId.GetMemberId(), location) {
private PythonType(string name, Location location, BuiltinTypeId typeId) : base(location) {
Check.ArgumentNotNull(nameof(location), location.Module);
_name = name ?? throw new ArgumentNullException(nameof(name));
_typeId = typeId;
}

#region ILocatedMember
public override PythonMemberType MemberType => _typeId.GetMemberId();
#endregion

#region IPythonType

public virtual string Name => TypeId == BuiltinTypeId.Ellipsis ? "..." : _name;
Expand Down
1 change: 0 additions & 1 deletion src/Analysis/Ast/Impl/Types/PythonTypeWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public virtual IMember Index(IPythonInstance instance, object index)
#region ILocatedMember
public Location Location => InnerType?.Location ?? default;
public LocationInfo Definition => InnerType?.Definition ?? LocationInfo.Empty;
public ILocatedMember Parent => InnerType?.Parent;
public IReadOnlyList<LocationInfo> References => InnerType?.References ?? Array.Empty<LocationInfo>();
public void AddReference(Location location) => InnerType?.AddReference(location);
public void RemoveReferences(IPythonModule module) => InnerType?.RemoveReferences(module);
Expand Down
8 changes: 4 additions & 4 deletions src/Analysis/Ast/Impl/Types/PythonUnionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ internal sealed class PythonUnionType : LocatedMember, IPythonUnionType {
private readonly HashSet<IPythonType> _types = new HashSet<IPythonType>(PythonTypeComparer.Instance);
private readonly object _lock = new object();

public PythonUnionType(IEnumerable<IPythonType> types, IPythonModule declaringModule)
: base(PythonMemberType.Union, declaringModule) {
public PythonUnionType(IEnumerable<IPythonType> types, IPythonModule declaringModule) : base(declaringModule) {
_types.UnionWith(types);
}

private PythonUnionType(IPythonType x, IPythonType y)
: base(PythonMemberType.Union, x.DeclaringModule) {
private PythonUnionType(IPythonType x, IPythonType y) : base(x.DeclaringModule) {
Check.Argument(nameof(x), () => !(x is IPythonUnionType));
Check.Argument(nameof(y), () => !(y is IPythonUnionType));
_types.Add(x);
_types.Add(y);
}

public override PythonMemberType MemberType => PythonMemberType.Union;

#region IPythonType

public string Name {
Expand Down
14 changes: 7 additions & 7 deletions src/Analysis/Ast/Impl/Values/GlobalScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ private void DeclareBuiltinVariables() {
var listType = Module.Interpreter.GetBuiltinType(BuiltinTypeId.List);
var dictType = Module.Interpreter.GetBuiltinType(BuiltinTypeId.Dict);

VariableCollection.DeclareVariable("__debug__", boolType, VariableSource.Builtin);
VariableCollection.DeclareVariable("__doc__", strType, VariableSource.Builtin);
VariableCollection.DeclareVariable("__file__", strType, VariableSource.Builtin);
VariableCollection.DeclareVariable("__name__", strType, VariableSource.Builtin);
VariableCollection.DeclareVariable("__package__", strType, VariableSource.Builtin);
VariableCollection.DeclareVariable("__path__", listType, VariableSource.Builtin);
VariableCollection.DeclareVariable("__dict__", dictType, VariableSource.Builtin);
DeclareVariable("__debug__", boolType, VariableSource.Builtin);
DeclareVariable("__doc__", strType, VariableSource.Builtin);
DeclareVariable("__file__", strType, VariableSource.Builtin);
DeclareVariable("__name__", strType, VariableSource.Builtin);
DeclareVariable("__package__", strType, VariableSource.Builtin);
DeclareVariable("__path__", listType, VariableSource.Builtin);
DeclareVariable("__dict__", dictType, VariableSource.Builtin);
}
}
}
102 changes: 102 additions & 0 deletions src/Analysis/Ast/Impl/Values/ImportedVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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.Diagnostics;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Core;

namespace Microsoft.Python.Analysis.Values {
[DebuggerDisplay("{DebuggerDisplay}")]
internal sealed class ImportedVariable : Variable, IImportedMember {
public ImportedVariable(string name, IVariable parent, Location location)
: base(name, parent.Value, VariableSource.Import, location) {
Parent = parent;
Parent?.AddReference(location);
}

#region IImportedMember
public ILocatedMember Parent { get; }
#endregion

#region ILocatedMember
public override LocationInfo Definition {
get {
if (!Location.IsValid && Parent != null) {
return Parent?.Definition ?? LocationInfo.Empty;
}
return base.Definition;
}
}

public override IReadOnlyList<LocationInfo> References {
get {
if (!Location.IsValid && Parent != null) {
return Parent?.References ?? Array.Empty<LocationInfo>();
}
return base.References;
}
}

protected override bool AddOrRemoveReference(Action<ILocatedMember> action) {
// Variable can be
// a) Declared locally in the module. In this case it has non-default
// definition location and no parent (link).
// b) Imported from another module via 'from module import X'.
// In this case it has non-default definition location and non-null parent (link).
// c) Imported from another module via 'from module import *'.
// In this case it has default location (which means it is not explicitly declared)
// and the non-null parent (link).
var explicitlyDeclared = Location.IsValid;
if (!explicitlyDeclared && Parent != null) {
action(Parent);
return true;
}
// Explicitly declared.
// Values:
// a) If value is not a located member, then add reference to the variable.
// b) If variable name is the same as the value member name, then the variable
// is implicit declaration (like declared function or a class) and we need
// to add reference to the actual type instead.
var lmv = GetImplicitlyDeclaredValue();
if (lmv != null) {
// Variable is not user-declared and rather is holder of a function or class definition.
action(lmv);
return true;
}
action(Parent);
return false;
}

#endregion

private ILocatedMember GetImplicitlyDeclaredValue()
=> Value is ILocatedMember lm && Name.EqualsOrdinal(lm.GetPythonType()?.Name) && !Location.IsValid ? lm : null;

private string DebuggerDisplay {
get {
switch (Value) {
case IPythonInstance pi:
return $"{Name} : instance of {pi.Type.Name}";
case IPythonType pt:
return $"{Name} : typeInfo of {pt.Name}";
default:
return $"{Name} : member {Value?.MemberType}";
}
}
}
}
}
7 changes: 4 additions & 3 deletions src/Analysis/Ast/Impl/Values/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ namespace Microsoft.Python.Analysis.Values {
/// Represents scope where variables can be declared.
/// </summary>
internal class Scope : IScope {
private VariableCollection _variables;
private VariableCollection _nonLocals;
private VariableCollection _globals;
private List<Scope> _childScopes;

protected VariableCollection VariableCollection { get; } = new VariableCollection();

public Scope(ScopeStatement node, IScope outerScope, IPythonModule module) {
Node = node;
OuterScope = outerScope;
Expand All @@ -47,7 +46,7 @@ public Scope(ScopeStatement node, IScope outerScope, IPythonModule module) {
public IPythonModule Module { get; }

public IReadOnlyList<IScope> Children => _childScopes?.ToArray() ?? Array.Empty<IScope>();
public IVariableCollection Variables => VariableCollection;
public IVariableCollection Variables => _variables ?? VariableCollection.Empty;
public IVariableCollection NonLocals => _nonLocals ?? VariableCollection.Empty;
public IVariableCollection Globals => _globals ?? VariableCollection.Empty;

Expand Down Expand Up @@ -89,6 +88,8 @@ public void DeclareGlobal(string name, Location location)

internal void AddChildScope(Scope s) => (_childScopes ?? (_childScopes = new List<Scope>())).Add(s);

private VariableCollection VariableCollection => _variables ?? (_variables = new VariableCollection());

private void DeclareBuiltinVariables() {
if (Node == null || Module.ModuleType != ModuleType.User || this is IGlobalScope) {
return;
Expand Down
Loading