| 1 | import pytest |
| 2 | |
| 3 | _tuple_code = 'from typing import Tuple\ndef f(x: Tuple[int]): ...\nf' |
| 4 | |
| 5 | |
| 6 | @pytest.mark.parametrize( |
| 7 | 'code, expected_params, execute_annotation', [ |
| 8 | ('def f(x: 1, y): ...\nf', [None, None], True), |
| 9 | ('def f(x: 1, y): ...\nf', ['instance int', None], False), |
| 10 | ('def f(x: int): ...\nf', ['instance int'], True), |
| 11 | ('from typing import List\ndef f(x: List[int]): ...\nf', ['instance list'], True), |
| 12 | ('from typing import List\ndef f(x: List[int]): ...\nf', ['class list'], False), |
| 13 | (_tuple_code, ['instance tuple'], True), |
| 14 | (_tuple_code, ['class Tuple'], False), |
| 15 | ('x=str\ndef f(p: x): ...\nx=int\nf', ['instance int'], True), |
| 16 | |
| 17 | ('def f(*args, **kwargs): ...\nf', [None, None], False), |
| 18 | ('def f(*args: int, **kwargs: str): ...\nf', ['class int', 'class str'], False), |
| 19 | ] |
| 20 | ) |
| 21 | def test_param_annotation(Script, code, expected_params, execute_annotation): |
| 22 | func, = Script(code).goto() |
| 23 | sig, = func.get_signatures() |
| 24 | for p, expected in zip(sig.params, expected_params): |
| 25 | annotations = p.infer_annotation(execute_annotation=execute_annotation) |
| 26 | if expected is None: |
| 27 | assert not annotations |
| 28 | else: |
| 29 | annotation, = annotations |
| 30 | assert annotation.description == expected |
| 31 | |
| 32 | |
| 33 | @pytest.mark.parametrize( |
| 34 | 'code, expected_params', [ |
| 35 | ('def f(x=1, y=int, z): pass\nf', ['instance int', 'class int', None]), |
| 36 | ('def f(*args, **kwargs): pass\nf', [None, None]), |
| 37 | ('x=1\ndef f(p=x): pass\nx=""\nf', ['instance int']), |
| 38 | ] |
| 39 | ) |
| 40 | def test_param_default(Script, code, expected_params): |
| 41 | func, = Script(code).goto() |
| 42 | sig, = func.get_signatures() |
| 43 | for p, expected in zip(sig.params, expected_params): |
| 44 | annotations = p.infer_default() |
| 45 | if expected is None: |
| 46 | assert not annotations |
| 47 | else: |
| 48 | annotation, = annotations |
| 49 | assert annotation.description == expected |
| 50 | |
| 51 | |
| 52 | @pytest.mark.parametrize( |
| 53 | 'code, index, param_code, kind', [ |
| 54 | ('def f(x=1): pass\nf', 0, 'x=1', 'POSITIONAL_OR_KEYWORD'), |
| 55 | ('def f(*args:int): pass\nf', 0, '*args: int', 'VAR_POSITIONAL'), |
| 56 | ('def f(**kwargs: List[x]): pass\nf', 0, '**kwargs: List[x]', 'VAR_KEYWORD'), |
| 57 | ('def f(*, x:int=5): pass\nf', 0, 'x: int=5', 'KEYWORD_ONLY'), |
| 58 | ('def f(*args, x): pass\nf', 1, 'x', 'KEYWORD_ONLY'), |
| 59 | ] |
| 60 | ) |
| 61 | def test_param_kind_and_name(code, index, param_code, kind, Script): |
| 62 | func, = Script(code).goto() |
| 63 | sig, = func.get_signatures() |
| 64 | param = sig.params[index] |
| 65 | assert param.to_string() == param_code |
| 66 | assert param.kind.name == kind |
| 67 | |
| 68 | |
| 69 | def test_staticmethod(Script): |
| 70 | s, = Script('staticmethod(').get_signatures() |
| 71 | assert s.to_string() == 'staticmethod(f: Callable[_P, _R_co], /)' |