mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 06:44:46 +08:00
Jedi raised an error when defined_names was called on empty functions, fixes #697.
This commit is contained in:
@@ -58,6 +58,9 @@ def filter_definition_names(names, origin, position=None):
|
|||||||
Filter names that are actual definitions in a scope. Names that are just
|
Filter names that are actual definitions in a scope. Names that are just
|
||||||
used will be ignored.
|
used will be ignored.
|
||||||
"""
|
"""
|
||||||
|
if not names:
|
||||||
|
return []
|
||||||
|
|
||||||
# Just calculate the scope from the first
|
# Just calculate the scope from the first
|
||||||
stmt = names[0].get_definition()
|
stmt = names[0].get_definition()
|
||||||
scope = stmt.get_parent_scope()
|
scope = stmt.get_parent_scope()
|
||||||
|
|||||||
@@ -2,19 +2,19 @@
|
|||||||
Tests for `api.defined_names`.
|
Tests for `api.defined_names`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import textwrap
|
from textwrap import dedent
|
||||||
|
|
||||||
from jedi import api
|
from jedi import defined_names, names
|
||||||
from ..helpers import TestCase
|
from ..helpers import TestCase
|
||||||
|
|
||||||
|
|
||||||
class TestDefinedNames(TestCase):
|
class TestDefinedNames(TestCase):
|
||||||
def assert_definition_names(self, definitions, names):
|
def assert_definition_names(self, definitions, names_):
|
||||||
assert [d.name for d in definitions] == names
|
assert [d.name for d in definitions] == names_
|
||||||
|
|
||||||
def check_defined_names(self, source, names):
|
def check_defined_names(self, source, names_):
|
||||||
definitions = api.names(textwrap.dedent(source))
|
definitions = names(dedent(source))
|
||||||
self.assert_definition_names(definitions, names)
|
self.assert_definition_names(definitions, names_)
|
||||||
return definitions
|
return definitions
|
||||||
|
|
||||||
def test_get_definitions_flat(self):
|
def test_get_definitions_flat(self):
|
||||||
@@ -76,7 +76,17 @@ class TestDefinedNames(TestCase):
|
|||||||
|
|
||||||
def test_follow_imports():
|
def test_follow_imports():
|
||||||
# github issue #344
|
# github issue #344
|
||||||
imp = api.defined_names('import datetime')[0]
|
imp = defined_names('import datetime')[0]
|
||||||
assert imp.name == 'datetime'
|
assert imp.name == 'datetime'
|
||||||
datetime_names = [str(d.name) for d in imp.defined_names()]
|
datetime_names = [str(d.name) for d in imp.defined_names()]
|
||||||
assert 'timedelta' in datetime_names
|
assert 'timedelta' in datetime_names
|
||||||
|
|
||||||
|
|
||||||
|
def test_names_twice():
|
||||||
|
source = dedent('''
|
||||||
|
def lol():
|
||||||
|
pass
|
||||||
|
''')
|
||||||
|
|
||||||
|
defs = names(source=source)
|
||||||
|
assert defs[0].defined_names() == []
|
||||||
|
|||||||
Reference in New Issue
Block a user