| 1 |
from textwrap import dedent |
| 2 |
import sys |
| 3 |
import math |
| 4 |
from collections import Counter |
| 5 |
from datetime import datetime |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import jedi |
| 10 |
from jedi.inference import compiled |
| 11 |
from jedi.inference.compiled.access import DirectObjectAccess |
| 12 |
from jedi.inference.gradual.conversion import _stub_to_python_value_set |
| 13 |
from jedi.inference.syntax_tree import _infer_comparison_part |
| 14 |
|
| 15 |
|
| 16 |
def test_simple(inference_state, environment): |
| 17 |
obj = compiled.create_simple_object(inference_state, '_str_') |
| 18 |
upper, = obj.py__getattribute__('upper') |
| 19 |
objs = list(upper.execute_with_values()) |
| 20 |
assert len(objs) == 1 |
| 21 |
assert objs[0].name.string_name == 'str' |
| 22 |
|
| 23 |
|
| 24 |
def test_builtin_loading(inference_state): |
| 25 |
string, = inference_state.builtins_module.py__getattribute__('str') |
| 26 |
from_name, = string.py__getattribute__('__init__') |
| 27 |
assert from_name.tree_node |
| 28 |
assert not from_name.py__doc__() |
| 29 |
|
| 30 |
|
| 31 |
def test_next_docstr(inference_state, environment): |
| 32 |
if environment.version_info[:2] != sys.version_info[:2]: |
| 33 |
pytest.skip() |
| 34 |
|
| 35 |
next_ = compiled.builtin_from_name(inference_state, 'next') |
| 36 |
assert next_.tree_node is not None |
| 37 |
assert next_.py__doc__() == '' |
| 38 |
for non_stub in _stub_to_python_value_set(next_): |
| 39 |
assert non_stub.py__doc__() == next.__doc__ |
| 40 |
|
| 41 |
|
| 42 |
def test_parse_function_doc_illegal_docstr(): |
| 43 |
docstr = """ |
| 44 |
test_func(o |
| 45 |
|
| 46 |
doesn't have a closing bracket. |
| 47 |
""" |
| 48 |
assert ('', '') == compiled.value._parse_function_doc(docstr) |
| 49 |
|
| 50 |
|
| 51 |
def test_doc(inference_state): |
| 52 |
""" |
| 53 |
Even CompiledValue docs always return empty docstrings - not None, that's |
| 54 |
just a Jedi API definition. |
| 55 |
""" |
| 56 |
str_ = compiled.create_simple_object(inference_state, '') |
| 57 |
|
| 58 |
obj, = str_.py__getattribute__('__getnewargs__') |
| 59 |
assert obj.py__doc__() == '' |
| 60 |
|
| 61 |
|
| 62 |
def test_string_literals(Script, environment): |
| 63 |
def typ(string): |
| 64 |
d = Script("a = %s; a" % string).infer()[0] |
| 65 |
return d.name |
| 66 |
|
| 67 |
assert typ('""') == 'str' |
| 68 |
assert typ('r""') == 'str' |
| 69 |
assert typ('br""') == 'bytes' |
| 70 |
assert typ('b""') == 'bytes' |
| 71 |
assert typ('u""') == 'str' |
| 72 |
|
| 73 |
|
| 74 |
def test_method_completion(Script, environment): |
| 75 |
code = dedent(''' |
| 76 |
class Foo: |
| 77 |
def bar(self): |
| 78 |
pass |
| 79 |
|
| 80 |
foo = Foo() |
| 81 |
foo.bar.__func__''') |
| 82 |
assert [c.name for c in Script(code).complete()] == ['__func__'] |
| 83 |
|
| 84 |
|
| 85 |
def test_time_docstring(): |
| 86 |
import time |
| 87 |
comp, = jedi.Script('import time\ntime.sleep').complete() |
| 88 |
assert comp.docstring(raw=True) == time.sleep.__doc__ |
| 89 |
expected = 'sleep(seconds: _SupportsFloatOrIndex, /) -> None\n\n' + time.sleep.__doc__ |
| 90 |
assert comp.docstring() == expected |
| 91 |
|
| 92 |
|
| 93 |
def test_dict_values(Script, environment): |
| 94 |
assert Script('import sys\nsys.modules["alshdb;lasdhf"]').infer() |
| 95 |
|
| 96 |
|
| 97 |
def test_getitem_on_none(Script): |
| 98 |
script = Script('None[1j]') |
| 99 |
assert not script.infer() |
| 100 |
issue, = script._inference_state.analysis |
| 101 |
assert issue.name == 'type-error-not-subscriptable' |
| 102 |
|
| 103 |
|
| 104 |
def _return_int(): |
| 105 |
return 1 |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize( |
| 109 |
'attribute, expected_name, expected_parent', [ |
| 110 |
('x', 'int', 'builtins'), |
| 111 |
('y', 'int', 'builtins'), |
| 112 |
('z', 'bool', 'builtins'), |
| 113 |
('cos', 'cos', 'math'), |
| 114 |
('dec', 'Decimal', 'decimal'), |
| 115 |
('dt', 'datetime', 'datetime'), |
| 116 |
('ret_int', '_return_int', 'test.test_inference.test_compiled'), |
| 117 |
] |
| 118 |
) |
| 119 |
def test_parent_context(same_process_inference_state, attribute, expected_name, expected_parent): |
| 120 |
import decimal |
| 121 |
|
| 122 |
class C: |
| 123 |
x = 1 |
| 124 |
y = int |
| 125 |
z = True |
| 126 |
cos = math.cos |
| 127 |
dec = decimal.Decimal(1) |
| 128 |
dt = datetime(2000, 1, 1) |
| 129 |
ret_int = _return_int |
| 130 |
|
| 131 |
o = compiled.CompiledValue( |
| 132 |
same_process_inference_state, |
| 133 |
DirectObjectAccess(same_process_inference_state, C) |
| 134 |
) |
| 135 |
x, = o.py__getattribute__(attribute) |
| 136 |
assert x.py__name__() == expected_name |
| 137 |
module_name = x.parent_context.py__name__() |
| 138 |
assert module_name == expected_parent |
| 139 |
assert x.parent_context.parent_context is None |
| 140 |
|
| 141 |
|
| 142 |
@pytest.mark.parametrize( |
| 143 |
'obj, expected_names', [ |
| 144 |
('', ['str']), |
| 145 |
(str, ['str']), |
| 146 |
(''.upper, ['str', 'upper']), |
| 147 |
(str.upper, ['str', 'upper']), |
| 148 |
|
| 149 |
(math.cos, ['cos']), |
| 150 |
|
| 151 |
(Counter, ['Counter']), |
| 152 |
(Counter(""), ['Counter']), |
| 153 |
(Counter.most_common, ['Counter', 'most_common']), |
| 154 |
(Counter("").most_common, ['Counter', 'most_common']), |
| 155 |
] |
| 156 |
) |
| 157 |
def test_qualified_names(same_process_inference_state, obj, expected_names): |
| 158 |
o = compiled.CompiledValue( |
| 159 |
same_process_inference_state, |
| 160 |
DirectObjectAccess(same_process_inference_state, obj) |
| 161 |
) |
| 162 |
assert o.get_qualified_names() == tuple(expected_names) |
| 163 |
|
| 164 |
|
| 165 |
def test_operation(Script, inference_state, create_compiled_object): |
| 166 |
b = create_compiled_object(bool) |
| 167 |
false, true = _infer_comparison_part( |
| 168 |
inference_state, b.parent_context, |
| 169 |
left=list(b.execute_with_values())[0], |
| 170 |
operator='is not', |
| 171 |
right=b, |
| 172 |
) |
| 173 |
assert false.py__name__() == 'bool' |
| 174 |
assert true.py__name__() == 'bool' |