Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Unify package search in imports
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailArkhipov committed Jul 24, 2019
1 parent 08b5971 commit 67fed10
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/FromImportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ public bool HandleFromImport(FromImportStatement node) {
}
}

var imports = ModuleResolution.CurrentPathResolver.FindImports(Module.FilePath, node);
if (HandleImportSearchResult(imports, null, null, node.Root, out var variableModule)) {
AssignVariables(node, imports, variableModule);
}
FindModule(node.Root, null, node.ForceAbsolute, out var firstModule, out var lastModule, out _, out var imports);
AssignVariables(node, imports, lastModule);
return false;
}

Expand Down Expand Up @@ -82,7 +80,7 @@ private void HandleModuleImportStar(PythonVariableModule variableModule, bool is
}

// If __all__ is present, take it, otherwise declare all members from the module that do not begin with an underscore.
var memberNames = isImplicitPackage
var memberNames = isImplicitPackage
? variableModule.GetMemberNames()
: variableModule.Analysis.StarImportMemberNames ?? variableModule.GetMemberNames().Where(s => !s.StartsWithOrdinal("_"));

Expand Down
15 changes: 10 additions & 5 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/ImportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ public bool HandleImport(ImportStatement node) {
return false;
}

private void HandleImport(ModuleName moduleImportExpression, NameExpression asNameExpression, bool forceAbsolute) {
private void FindModule(ModuleName moduleImportExpression, NameExpression asNameExpression, bool forceAbsolute,
out PythonVariableModule firstModule, out PythonVariableModule lastModule, out ImmutableArray<string> importNames, out IImportSearchResult imports) {
// "import fob.oar.baz" means
// import_module('fob')
// import_module('fob.oar')
// import_module('fob.oar.baz')
var importNames = ImmutableArray<string>.Empty;
var lastModule = default(PythonVariableModule);
var firstModule = default(PythonVariableModule);
importNames = ImmutableArray<string>.Empty;
lastModule = default;
firstModule = default;
imports = null;
foreach (var nameExpression in moduleImportExpression.Names) {
importNames = importNames.Add(nameExpression.Name);
var imports = ModuleResolution.CurrentPathResolver.GetImportsFromAbsoluteName(Module.FilePath, importNames, forceAbsolute);
imports = ModuleResolution.CurrentPathResolver.GetImportsFromAbsoluteName(Module.FilePath, importNames, forceAbsolute);
if (!HandleImportSearchResult(imports, lastModule, asNameExpression, moduleImportExpression, out lastModule)) {
lastModule = default;
break;
Expand All @@ -69,7 +71,10 @@ private void HandleImport(ModuleName moduleImportExpression, NameExpression asNa
firstModule = lastModule;
}
}
}

private void HandleImport(ModuleName moduleImportExpression, NameExpression asNameExpression, bool forceAbsolute) {
FindModule(moduleImportExpression, asNameExpression, forceAbsolute, out var firstModule, out var lastModule, out var importNames, out _);
// "import fob.oar.baz as baz" is handled as baz = import_module('fob.oar.baz')
// "import fob.oar.baz" is handled as fob = import_module('fob')
if (!string.IsNullOrEmpty(asNameExpression?.Name) && lastModule != default) {
Expand Down

0 comments on commit 67fed10

Please sign in to comment.