示例#1
0
        private void AssignVariables(FromImportStatement node, IImportSearchResult imports, PythonVariableModule variableModule)
        {
            if (variableModule == null)
            {
                return;
            }

            var names   = node.Names;
            var asNames = node.AsNames;

            if (names.Count == 1 && names[0].Name == "*")
            {
                // 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);
                return;
            }

            for (var i = 0; i < names.Count; i++)
            {
                var memberName = names[i].Name;
                if (!string.IsNullOrEmpty(memberName))
                {
                    var variableName = asNames[i]?.Name ?? memberName;
                    var value        = variableModule.GetMember(memberName) ?? GetValueFromImports(variableModule, imports as IImportChildrenSource, memberName);

                    Eval.DeclareVariable(variableName, value, VariableSource.Import, names[i]);
                }
            }
        }
示例#2
0
        private void AssignVariables(FromImportStatement node, IImportSearchResult imports, PythonVariableModule variableModule)
        {
            if (variableModule == null)
            {
                return;
            }

            var names   = node.Names;
            var asNames = node.AsNames;

            if (names.Count == 1 && names[0].Name == "*")
            {
                // 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, node.StartIndex);
                return;
            }

            for (var i = 0; i < names.Count; i++)
            {
                var memberName = names[i].Name;
                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));
                }
            }
        }
示例#3
0
        private void AssignVariables(FromImportStatement node, IImportSearchResult imports, PythonVariableModule variableModule)
        {
            if (variableModule == null)
            {
                return;
            }

            var names   = node.Names;
            var asNames = node.AsNames;

            if (names.Count == 1 && names[0].Name == "*")
            {
                // 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, node.StartIndex, names[0]);
                return;
            }

            for (var i = 0; i < names.Count; i++)
            {
                var memberName = names[i].Name;
                if (!string.IsNullOrEmpty(memberName))
                {
                    var nameExpression = asNames[i] ?? names[i];
                    var variableName   = nameExpression?.Name ?? memberName;
                    if (!string.IsNullOrEmpty(variableName))
                    {
                        DeclareVariable(variableModule, memberName, imports, variableName, node.StartIndex, nameExpression);
                    }
                }
            }
        }
            public bool TryGetChildImport(string name, out IImportSearchResult child)
            {
                foreach (var edge in _edges)
                {
                    var index = edge.End.GetChildIndex(name);
                    if (index == -1)
                    {
                        continue;
                    }

                    var childEdge = edge.Append(index);
                    if (_snapshot.TryCreateModuleImport(childEdge, out var moduleImport))
                    {
                        child = moduleImport;
                    }
                    else
                    {
                        child = new ImplicitPackageImport(new ChildrenSource(_snapshot, childEdge), childEdge.End.Name, childEdge.End.FullModuleName);
                    }

                    return(true);
                }

                child = default;
                return(false);
            }
        private void HandleSearchResults(IImportSearchResult searchResult)
        {
            switch (searchResult)
            {
            case ModuleImport moduleImport when !Ignore(_moduleResolution, moduleImport.FullName, moduleImport.ModulePath):
                Dependencies.Add(new AnalysisModuleKey(moduleImport.FullName, moduleImport.ModulePath, _isTypeshed));
                return;

            case PossibleModuleImport possibleModuleImport when !Ignore(_moduleResolution, possibleModuleImport.PrecedingModuleFullName, possibleModuleImport.PrecedingModulePath):
                Dependencies.Add(new AnalysisModuleKey(possibleModuleImport.PrecedingModuleFullName, possibleModuleImport.PrecedingModulePath, _isTypeshed));
                return;

            default:
                return;
            }
        }
示例#6
0
        private void HandleModuleImportStar(PythonVariableModule variableModule, IImportSearchResult imports, int importPosition, NameExpression nameExpression)
        {
            if (variableModule.Module == Module)
            {
                // from self import * won't define any new members
                return;
            }
            // If __all__ is present, take it, otherwise declare all members from the module that do not begin with an underscore.
            var memberNames = imports is ImplicitPackageImport
                ? variableModule.GetMemberNames()
                : variableModule.Analysis.StarImportMemberNames ?? variableModule.GetMemberNames().Where(s => !s.StartsWithOrdinal("_"));

            foreach (var memberName in memberNames)
            {
                DeclareVariable(variableModule, memberName, imports, memberName, importPosition, nameExpression);
            }
        }
        private void AssignVariables(FromImportStatement node, IImportSearchResult imports, PythonVariableModule variableModule)
        {
            var names   = node.Names;
            var asNames = node.AsNames;

            if (variableModule != null && names.Count == 1 && names[0].Name == "*")
            {
                // 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, node.StartIndex, names[0]);
                return;
            }

            for (var i = 0; i < names.Count; i++)
            {
                var memberName = names[i].Name;
                if (string.IsNullOrEmpty(memberName))
                {
                    continue;
                }

                var nameExpression = asNames[i] ?? names[i];
                var variableName   = nameExpression?.Name ?? memberName;
                if (!string.IsNullOrEmpty(variableName))
                {
                    DeclareVariable(variableModule, memberName, imports, variableName, node.StartIndex, nameExpression);
                }

                if (imports is IImportChildrenSource cs &&
                    cs.TryGetChildImport(memberName, out var csr) &&
                    HandleImportSearchResult(csr, variableModule, null, names[i], out var childModule))
                {
                    _importedVariableHandler.EnsureModule(childModule);
                }
            }
        }
示例#8
0
        private static CompletionResult GetResultFromImportSearch(IImportSearchResult importSearchResult, CompletionContext context, bool prependStar, SourceSpan?applicableSpan = null)
        {
            var document = context.Analysis.Document;
            var mres     = document.Interpreter.ModuleResolution;

            IPythonModule module;

            switch (importSearchResult)
            {
            case ModuleImport moduleImports:
                module = mres.GetImportedModule(moduleImports.FullName);
                break;

            case PossibleModuleImport possibleModuleImport:
                module = mres.GetImportedModule(possibleModuleImport.PossibleModuleFullName);
                break;

            case ImplicitPackageImport _:
                module = null;
                break;

            default:
                return(CompletionResult.Empty);
            }

            var completions = new List <CompletionItem>();

            if (prependStar)
            {
                completions.Add(CompletionItemSource.Star);
            }

            if (module != null)
            {
                completions.AddRange(module.GetMemberNames()
                                     .Where(n => !string.IsNullOrEmpty(n))
                                     .Select(n => context.ItemSource.CreateCompletionItem(n, module.GetMember(n))));
            }

            if (importSearchResult is IImportChildrenSource children)
            {
                foreach (var childName in children.GetChildrenNames())
                {
                    if (!children.TryGetChildImport(childName, out var imports))
                    {
                        continue;
                    }

                    switch (imports)
                    {
                    case ImplicitPackageImport packageImport:
                        completions.Add(CompletionItemSource.CreateCompletionItem(packageImport.Name, CompletionItemKind.Module));
                        break;

                    case ModuleImport moduleImport when !moduleImport.ModulePath.PathEquals(document.FilePath):
                        completions.Add(CompletionItemSource.CreateCompletionItem(moduleImport.Name, CompletionItemKind.Module));
                        break;
                    }
                }
            }

            return(new CompletionResult(completions, applicableSpan));
        }
        /// <summary>
        /// Determines value of the variable and declares it. Value depends if source module has submodule
        /// that is named the same as the variable and/or it has internal variables named same as the submodule.
        /// </summary>
        /// <example>'from a.b import c' when 'c' is both submodule of 'b' and a variable declared inside 'b'.</example>
        /// <param name="variableModule">Source module of the variable such as 'a.b' in 'from a.b import c as d'. May be null if the module was not found.</param>
        /// <param name="memberName">Module member name such as 'c' in 'from a.b import c as d'.</param>
        /// <param name="imports">Import search result.</param>
        /// <param name="variableName">Name of the variable to declare, such as 'd' in 'from a.b import c as d'.</param>
        /// <param name="importPosition">Position of the import statement.</param>
        /// <param name="nameLocation">Location of the variable name expression.</param>
        private void DeclareVariable(PythonVariableModule variableModule, string memberName, IImportSearchResult imports, string variableName, int importPosition, Node nameLocation)
        {
            IMember value = Eval.UnknownType;

            if (variableModule != null)
            {
                // First try imports since child modules should win, i.e. in 'from a.b import c'
                // 'c' should be a submodule if 'b' has one, even if 'b' also declares 'c = 1'.
                value = GetValueFromImports(variableModule, imports as IImportChildrenSource, memberName);

                // First try exported or child submodules.
                var member = variableModule.GetMember(memberName);

                // Value may be variable or submodule. If it is variable, we need it in order to add reference.
                var variable = _importedVariableHandler.GetVariable(variableModule, memberName);

                if (member is PythonVariableModule vm && vm.Equals(variable?.Value))
                {
                    // If member is submodule, use actual variable so it can be linked through for goto definition.
                    value = variable;
                }
                else if (value == null)
                {
                    if (member is PythonVariableModule)
                    {
                        // If member is submodule, use it.
                        value = member;
                    }
                    else if (variable?.Value != null)
                    {
                        // Otherwise use variable, if available so references can be linked.
                        value = variable;
                    }
                    else if (member != null)
                    {
                        value = member;
                    }
                    else
                    {
                        value = Eval.UnknownType;
                    }
                }
            }
示例#10
0
        /// <summary>
        /// Determines value of the variable and declares it. Value depends if source module has submodule
        /// that is named the same as the variable and/or it has internal variables named same as the submodule.
        /// </summary>
        /// <example>'from a.b import c' when 'c' is both submodule of 'b' and a variable declared inside 'b'.</example>
        /// <param name="variableModule">Source module of the variable such as 'a.b' in 'from a.b import c as d'.</param>
        /// <param name="memberName">Module member name such as 'c' in 'from a.b import c as d'.</param>
        /// <param name="imports">Import search result.</param>
        /// <param name="variableName">Name of the variable to declare, such as 'd' in 'from a.b import c as d'.</param>
        /// <param name="importPosition">Position of the import statement.</param>
        /// <param name="nameLocation">Location of the variable name expression.</param>
        private void DeclareVariable(PythonVariableModule variableModule, string memberName, IImportSearchResult imports, string variableName, int importPosition, Node nameLocation)
        {
            // First try imports since child modules should win, i.e. in 'from a.b import c'
            // 'c' should be a submodule if 'b' has one, even if 'b' also declares 'c = 1'.
            var value = GetValueFromImports(variableModule, imports as IImportChildrenSource, memberName);

            // First try exported or child submodules.
            value = value ?? variableModule.GetMember(memberName);

            // Value may be variable or submodule. If it is variable, we need it in order to add reference.
            var variable = variableModule.Analysis?.GlobalScope?.Variables[memberName];

            value = variable?.Value?.Equals(value) == true ? variable : value;

            // If nothing is exported, variables are still accessible.
            value = value ?? variableModule.Analysis?.GlobalScope?.Variables[memberName]?.Value ?? Eval.UnknownType;

            // Do not allow imported variables to override local declarations
            var canOverwrite = CanOverwriteVariable(variableName, importPosition, value);

            // Do not declare references to '*'
            var locationExpression = nameLocation is NameExpression nex && nex.Name == "*" ? null : nameLocation;

            Eval.DeclareVariable(variableName, value, VariableSource.Import, locationExpression, canOverwrite);

            // Make sure module is loaded and analyzed.
            if (value is IPythonModule m)
            {
                ModuleResolution.GetOrLoadModule(m.Name);
            }
        }
示例#11
0
        private static void HandleSearchResults(bool isTypeshed, HashSet <AnalysisModuleKey> dependencies, IModuleManagement moduleResolution, IImportSearchResult searchResult)
        {
            switch (searchResult)
            {
            case ModuleImport moduleImport when !Ignore(moduleResolution, moduleImport.FullName):
                dependencies.Add(new AnalysisModuleKey(moduleImport.FullName, moduleImport.ModulePath, isTypeshed));
                return;

            case PossibleModuleImport possibleModuleImport when !Ignore(moduleResolution, possibleModuleImport.PrecedingModuleFullName):
                dependencies.Add(new AnalysisModuleKey(possibleModuleImport.PrecedingModuleFullName, possibleModuleImport.PrecedingModulePath, isTypeshed));
                return;

            default:
                return;
            }
        }
示例#12
0
 public bool TryGetChildImport(string name, out IImportSearchResult child) => _childrenSource.TryGetChildImport(name, out child);
 public bool TryGetChildImport(string name, out IImportSearchResult child)
 {
     child = default;
     return(false);
 }