mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 06:24:27 +08:00
Numpydocs and compiled objects return types
This commit is contained in:
@@ -4,14 +4,22 @@ Testing of docstring related issues and especially ``jedi.docstrings``.
|
||||
|
||||
from textwrap import dedent
|
||||
import jedi
|
||||
import pytest
|
||||
from ..helpers import unittest
|
||||
|
||||
try:
|
||||
import numpydoc
|
||||
import numpydoc # NOQA
|
||||
except ImportError:
|
||||
numpydoc_unavailable = True
|
||||
else:
|
||||
numpydoc_unavailable = False
|
||||
|
||||
try:
|
||||
import numpy
|
||||
except ImportError:
|
||||
numpy_unavailable = True
|
||||
else:
|
||||
numpy_unavailable = False
|
||||
|
||||
|
||||
class TestDocstring(unittest.TestCase):
|
||||
@@ -124,48 +132,174 @@ class TestDocstring(unittest.TestCase):
|
||||
completions = jedi.Script('assert').completions()
|
||||
self.assertIn('assert', completions[0].docstring())
|
||||
|
||||
@unittest.skipIf(numpydoc_unavailable, 'numpydoc module is unavailable')
|
||||
def test_numpydoc_docstring(self):
|
||||
s = dedent('''
|
||||
def foobar(x, y):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
x : int
|
||||
y : str
|
||||
"""
|
||||
y.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' in names
|
||||
assert 'capitalize' in names
|
||||
# ---- Numpy Style Tests ---
|
||||
|
||||
@unittest.skipIf(numpydoc_unavailable, 'numpydoc module is unavailable')
|
||||
def test_numpydoc_docstring_set_of_values(self):
|
||||
s = dedent('''
|
||||
def foobar(x, y):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
x : {'foo', 'bar', 100500}, optional
|
||||
"""
|
||||
x.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' in names
|
||||
assert 'capitalize' in names
|
||||
assert 'numerator' in names
|
||||
@pytest.mark.skipif(numpydoc_unavailable,
|
||||
reason='numpydoc module is unavailable')
|
||||
def test_numpydoc_parameters():
|
||||
s = dedent('''
|
||||
def foobar(x, y):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
x : int
|
||||
y : str
|
||||
"""
|
||||
y.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' in names
|
||||
assert 'capitalize' in names
|
||||
|
||||
@unittest.skipIf(numpydoc_unavailable, 'numpydoc module is unavailable')
|
||||
def test_numpydoc_alternative_types(self):
|
||||
s = dedent('''
|
||||
def foobar(x, y):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
x : int or str or list
|
||||
"""
|
||||
x.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' in names
|
||||
assert 'capitalize' in names
|
||||
assert 'numerator' in names
|
||||
assert 'append' in names
|
||||
@pytest.mark.skipif(numpydoc_unavailable,
|
||||
reason='numpydoc module is unavailable')
|
||||
def test_numpydoc_parameters_set_of_values():
|
||||
s = dedent('''
|
||||
def foobar(x, y):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
x : {'foo', 'bar', 100500}, optional
|
||||
"""
|
||||
x.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' in names
|
||||
assert 'capitalize' in names
|
||||
assert 'numerator' in names
|
||||
|
||||
@pytest.mark.skipif(numpydoc_unavailable,
|
||||
reason='numpydoc module is unavailable')
|
||||
def test_numpydoc_parameters_alternative_types():
|
||||
s = dedent('''
|
||||
def foobar(x, y):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
x : int or str or list
|
||||
"""
|
||||
x.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' in names
|
||||
assert 'capitalize' in names
|
||||
assert 'numerator' in names
|
||||
assert 'append' in names
|
||||
|
||||
def test_numpydoc_returns():
|
||||
s = dedent('''
|
||||
def foobar():
|
||||
"""
|
||||
Returns
|
||||
----------
|
||||
x : int
|
||||
y : str
|
||||
"""
|
||||
return x
|
||||
|
||||
def bazbiz():
|
||||
z = foobar()
|
||||
z.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' in names
|
||||
assert 'capitalize' in names
|
||||
assert 'numerator' in names
|
||||
|
||||
@pytest.mark.skipif(numpydoc_unavailable,
|
||||
reason='numpydoc module is unavailable')
|
||||
def test_numpydoc_returns_set_of_values():
|
||||
s = dedent('''
|
||||
def foobar():
|
||||
"""
|
||||
Returns
|
||||
----------
|
||||
x : {'foo', 'bar', 100500}
|
||||
"""
|
||||
return x
|
||||
|
||||
def bazbiz():
|
||||
z = foobar()
|
||||
z.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' in names
|
||||
assert 'capitalize' in names
|
||||
assert 'numerator' in names
|
||||
|
||||
@pytest.mark.skipif(numpydoc_unavailable,
|
||||
reason='numpydoc module is unavailable')
|
||||
def test_numpydoc_returns_alternative_types():
|
||||
s = dedent('''
|
||||
def foobar():
|
||||
"""
|
||||
Returns
|
||||
----------
|
||||
int or list of str
|
||||
"""
|
||||
return x
|
||||
|
||||
def bazbiz():
|
||||
z = foobar()
|
||||
z.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'isupper' not in names
|
||||
assert 'capitalize' not in names
|
||||
assert 'numerator' in names
|
||||
assert 'append' in names
|
||||
|
||||
def test_numpydoc_returns_list_of():
|
||||
s = dedent('''
|
||||
def foobar():
|
||||
"""
|
||||
Returns
|
||||
----------
|
||||
list of str
|
||||
"""
|
||||
return x
|
||||
|
||||
def bazbiz():
|
||||
z = foobar()
|
||||
z.''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
assert 'append' in names
|
||||
assert 'isupper' not in names
|
||||
assert 'capitalize' not in names
|
||||
|
||||
@pytest.mark.skipif(numpydoc_unavailable,
|
||||
reason='numpydoc module is unavailable')
|
||||
def test_numpydoc_returns_obj():
|
||||
s = dedent('''
|
||||
def foobar(x, y):
|
||||
"""
|
||||
Returns
|
||||
----------
|
||||
int or random.Random
|
||||
"""
|
||||
return x + y
|
||||
|
||||
def bazbiz():
|
||||
z = foobar(x, y)
|
||||
z.''')
|
||||
script = jedi.Script(s)
|
||||
names = [c.name for c in script.completions()]
|
||||
assert 'numerator' in names
|
||||
assert 'seed' in names
|
||||
|
||||
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
||||
reason='numpydoc or numpy module is unavailable')
|
||||
def test_numpy_returns():
|
||||
s = dedent('''
|
||||
import numpy
|
||||
x = numpy.asarray([])
|
||||
x.d''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
print(names)
|
||||
assert 'diagonal' in names
|
||||
|
||||
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
||||
reason='numpydoc or numpy module is unavailable')
|
||||
def test_numpy_comp_returns():
|
||||
s = dedent('''
|
||||
import numpy
|
||||
x = numpy.array([])
|
||||
x.d''')
|
||||
names = [c.name for c in jedi.Script(s).completions()]
|
||||
print(names)
|
||||
assert 'diagonal' in names
|
||||
|
||||
|
||||
Reference in New Issue
Block a user