mirror of
https://github.com/davidhalter/parso.git
synced 2026-05-25 01:38:52 +08:00
Add an option to the new get_definition.
This commit is contained in:
@@ -40,6 +40,7 @@ _GET_DEFINITION_TYPES = set([
|
|||||||
'expr_stmt', 'comp_for', 'with_stmt', 'for_stmt', 'import_name',
|
'expr_stmt', 'comp_for', 'with_stmt', 'for_stmt', 'import_name',
|
||||||
'import_from', 'param'
|
'import_from', 'param'
|
||||||
])
|
])
|
||||||
|
_IMPORTS = set(['import_name', 'import_from'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -189,9 +190,13 @@ class Name(_LeafWithoutNewlines):
|
|||||||
"""
|
"""
|
||||||
return self._get_definition() is not None
|
return self._get_definition() is not None
|
||||||
|
|
||||||
def _get_definition(self):
|
def _get_definition(self, import_name_always=False):
|
||||||
"""
|
"""
|
||||||
Returns None if there's on definition for a name.
|
Returns None if there's on definition for a name.
|
||||||
|
|
||||||
|
:param import_name_alway: Specifies if an import name is always a
|
||||||
|
definition. Normally foo in `from foo import bar` is not a
|
||||||
|
definition.
|
||||||
"""
|
"""
|
||||||
node = self.parent
|
node = self.parent
|
||||||
type_ = node.type
|
type_ = node.type
|
||||||
@@ -223,6 +228,8 @@ class Name(_LeafWithoutNewlines):
|
|||||||
if node.type in _GET_DEFINITION_TYPES:
|
if node.type in _GET_DEFINITION_TYPES:
|
||||||
if self in node.get_defined_names():
|
if self in node.get_defined_names():
|
||||||
return node
|
return node
|
||||||
|
if import_name_always and node.type in _IMPORTS:
|
||||||
|
return node
|
||||||
return None
|
return None
|
||||||
node = node.parent
|
node = node.parent
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user