| 1 |
from typing import Generic, TypeVar, List |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import jedi |
| 6 |
from jedi.inference.value import ModuleValue |
| 7 |
|
| 8 |
|
| 9 |
def interpreter(code, namespace, *args, **kwargs): |
| 10 |
return jedi.Interpreter(code, [namespace], *args, **kwargs) |
| 11 |
|
| 12 |
|
| 13 |
def test_on_code(): |
| 14 |
from functools import wraps |
| 15 |
i = interpreter("wraps.__code__", {'wraps': wraps}) |
| 16 |
assert i.infer() |
| 17 |
|
| 18 |
|
| 19 |
def test_generics_without_definition() -> None: |
| 20 |
|
| 21 |
T = TypeVar('T') |
| 22 |
|
| 23 |
class Stack(Generic[T]): |
| 24 |
def __init__(self) -> None: |
| 25 |
self.items: List[T] = [] |
| 26 |
|
| 27 |
def push(self, item): |
| 28 |
self.items.append(item) |
| 29 |
|
| 30 |
def pop(self): |
| 31 |
|
| 32 |
return self.items.pop() |
| 33 |
|
| 34 |
class StackWrapper(): |
| 35 |
def __init__(self): |
| 36 |
self.stack = Stack() |
| 37 |
self.stack.push(1) |
| 38 |
|
| 39 |
s = StackWrapper() |
| 40 |
assert not interpreter('s.stack.pop().', locals()).complete() |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.parametrize( |
| 44 |
'code, expected', [ |
| 45 |
('Foo().method()', 'str'), |
| 46 |
('Foo.method()', 'str'), |
| 47 |
('foo.method()', 'str'), |
| 48 |
('Foo().read()', 'str'), |
| 49 |
('Foo.read()', 'str'), |
| 50 |
('foo.read()', 'str'), |
| 51 |
] |
| 52 |
) |
| 53 |
def test_generics_methods(code, expected, class_findable): |
| 54 |
T = TypeVar("T") |
| 55 |
|
| 56 |
class Reader(Generic[T]): |
| 57 |
@classmethod |
| 58 |
def read(cls) -> T: |
| 59 |
return cls() |
| 60 |
|
| 61 |
def method(self) -> T: |
| 62 |
return 1 |
| 63 |
|
| 64 |
class Foo(Reader[str]): |
| 65 |
def transform(self) -> int: |
| 66 |
return 42 |
| 67 |
|
| 68 |
foo = Foo() |
| 69 |
|
| 70 |
defs = jedi.Interpreter(code, [locals()]).infer() |
| 71 |
if class_findable: |
| 72 |
def_, = defs |
| 73 |
assert def_.name == expected |
| 74 |
else: |
| 75 |
assert not defs |
| 76 |
|
| 77 |
|
| 78 |
def test_mixed_module_cache(): |
| 79 |
"""Caused by #1479""" |
| 80 |
interpreter = jedi.Interpreter('jedi', [{'jedi': jedi}]) |
| 81 |
d, = interpreter.infer() |
| 82 |
assert d.name == 'jedi' |
| 83 |
inference_state = interpreter._inference_state |
| 84 |
jedi_module, = inference_state.module_cache.get(('jedi',)) |
| 85 |
assert isinstance(jedi_module, ModuleValue) |
| 86 |
|
| 87 |
|
| 88 |
def test_signature(): |
| 89 |
""" |
| 90 |
For performance reasons we use the signature of the compiled object and not |
| 91 |
the tree object. |
| 92 |
""" |
| 93 |
def some_signature(foo): |
| 94 |
pass |
| 95 |
|
| 96 |
from inspect import Signature, Parameter |
| 97 |
some_signature.__signature__ = Signature([ |
| 98 |
Parameter('bar', kind=Parameter.KEYWORD_ONLY, default=1) |
| 99 |
]) |
| 100 |
|
| 101 |
s, = jedi.Interpreter('some_signature', [locals()]).goto() |
| 102 |
assert s.docstring() == 'some_signature(*, bar=1)' |
| 103 |
|
| 104 |
|
| 105 |
def test_compiled_signature_annotation_string(): |
| 106 |
import typing |
| 107 |
|
| 108 |
def func(x: typing.Type, y: typing.Union[typing.Type, int]): |
| 109 |
pass |
| 110 |
func.__name__ = 'not_func' |
| 111 |
|
| 112 |
s, = jedi.Interpreter('func()', [locals()]).get_signatures(1, 5) |
| 113 |
assert s.params[0].description == 'param x: Type' |
| 114 |
assert s.params[1].description == 'param y: Type | int' |