Class NImportFrom
- java.lang.Object
-
- org.python.indexer.ast.NNode
-
- org.python.indexer.ast.NImportFrom
-
- All Implemented Interfaces:
- java.io.Serializable
public class NImportFrom extends NNode
Handles import from statements such asfrom moduleA import a, b as c, dandfrom foo.bar.moduleB import *.The indexer's implementation of import * uses different semantics from all the other forms of import. It's basically a bug, although the jury is still out as to which implementation is better.
For the others we define name bindings anywhere an actual name is introduced into the scope containing the import statement, and references to the imported module or name everywhere else. This mimics the behavior of Python at runtime, but it may be confusing to anyone with only a casual understanding of Python's data model, who might think it works more like Java.
For import * we just enter the imported names into the symbol table, which lets other code reference them, but the references "pass through" automatically to the module from which the names were imported.
To illustate the difference, consider the following four modules:
moduleA.py: a = 1 b = 2 moduleB.py: c = 3 d = 4 moduleC.py: from moduleA import a, b from moduleB import * print a # indexer finds definition of 'a' 2 lines up print b # indexer finds definition of 'b' 3 lines up print c # indexer finds definition of 'c' in moduleB print d # indexer finds definition of 'd' in moduleB moduleD.py: import moduleC print moduleC.a # indexer finds definition of 'a' in moduleC print moduleC.b # indexer finds definition of 'b' in moduleC print moduleC.c # indexer finds definition of 'c' in moduleB print moduleC.c # indexer finds definition of 'd' in moduleBTo make import * work like the others, we need only create bindings for the imported names. But where would the bindings be located? Assuming that we were to co-locate them all at the "*" name node, clicking on a reference to any of the names would jump to the "*". It's not clear that this is a better user experience.We could make the other import statement forms work like
import *, but that path is even more fraught with confusing inconsistencies.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description java.util.List<NAlias>aliasesjava.lang.StringmoduleNQnameqname
-
Constructor Summary
Constructors Constructor and Description NImportFrom(java.lang.String module, NQname qname, java.util.List<NAlias> aliases)NImportFrom(java.lang.String module, NQname qname, java.util.List<NAlias> aliases, int start, int end)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description booleanbindsName()Returnstrueif this is a name-binding node.booleanisImportStar()NTyperesolve(Scope s)Node should set the resolved type in itsNNode.typefield and also return it.java.lang.StringtoString()voidvisit(NNodeVisitor v)Visits this node and optionally its children.-
Methods inherited from class org.python.indexer.ast.NNode
addChildren, addChildren, addType, end, getAstRoot, getDeepestNodeAtOffset, getEnclosingNamespace, getFile, getParent, getTable, getType, isCall, isClassDef, isFunctionDef, isLambda, isModule, isName, length, resolveExpr, setEnd, setParent, setStart, setType, start
-
-
-
-
Field Detail
-
module
public java.lang.String module
-
qname
public NQname qname
-
aliases
public java.util.List<NAlias> aliases
-
-
Constructor Detail
-
NImportFrom
public NImportFrom(java.lang.String module, NQname qname, java.util.List<NAlias> aliases)
-
-
Method Detail
-
bindsName
public boolean bindsName()
Description copied from class:NNodeReturnstrueif this is a name-binding node. Includes functions/lambdas, function/lambda params, classes, assignments, imports, and implicit assignment via for statements and except clauses.
-
resolve
public NType resolve(Scope s) throws java.lang.Exception
Description copied from class:NNodeNode should set the resolved type in itsNNode.typefield and also return it.
-
isImportStar
public boolean isImportStar()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
visit
public void visit(NNodeVisitor v)
Description copied from class:NNodeVisits this node and optionally its children.
-
-
DMelt 3.0 © DataMelt by jWork.ORG