Coverage for tests/test_functions.py: 98.97%
97 statements
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-12 01:34 +0200
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-12 01:34 +0200
1"""Test functions loading."""
3from __future__ import annotations
5import pytest
7from griffe import ParameterKind, temporary_visited_module
10def test_visit_simple_function() -> None:
11 """Test functions parameters loading."""
12 with temporary_visited_module("def f(foo='<>'): ...") as module:
13 function = module["f"]
14 assert len(function.parameters) == 1
15 param = function.parameters[0]
16 assert param is function.parameters["foo"]
17 assert param.name == "foo"
18 assert param.kind is ParameterKind.positional_or_keyword
19 assert param.default == "'<>'"
22def test_visit_function_positional_only_param() -> None:
23 """Test functions parameters loading."""
24 with temporary_visited_module("def f(posonly, /): ...") as module:
25 function = module["f"]
26 assert len(function.parameters) == 1
27 param = function.parameters[0]
28 assert param is function.parameters["posonly"]
29 assert param.name == "posonly"
30 assert param.kind is ParameterKind.positional_only
31 assert param.default is None
34def test_visit_function_positional_only_param_with_default() -> None:
35 """Test functions parameters loading."""
36 with temporary_visited_module("def f(posonly=0, /): ...") as module:
37 function = module["f"]
38 assert len(function.parameters) == 1
39 param = function.parameters[0]
40 assert param is function.parameters["posonly"]
41 assert param.name == "posonly"
42 assert param.kind is ParameterKind.positional_only
43 assert param.default == "0"
46def test_visit_function_positional_or_keyword_param() -> None:
47 """Test functions parameters loading."""
48 with temporary_visited_module("def f(posonly, /, poskw): ...") as module:
49 function = module["f"]
50 assert len(function.parameters) == 2
51 param = function.parameters[1]
52 assert param is function.parameters["poskw"]
53 assert param.name == "poskw"
54 assert param.kind is ParameterKind.positional_or_keyword
55 assert param.default is None
58def test_visit_function_positional_or_keyword_param_with_default() -> None:
59 """Test functions parameters loading."""
60 with temporary_visited_module("def f(posonly, /, poskw=0): ...") as module:
61 function = module["f"]
62 assert len(function.parameters) == 2
63 param = function.parameters[1]
64 assert param is function.parameters["poskw"]
65 assert param.name == "poskw"
66 assert param.kind is ParameterKind.positional_or_keyword
67 assert param.default == "0"
70def test_visit_function_keyword_only_param() -> None:
71 """Test functions parameters loading."""
72 with temporary_visited_module("def f(*, kwonly): ...") as module:
73 function = module["f"]
74 assert len(function.parameters) == 1
75 param = function.parameters[0]
76 assert param is function.parameters["kwonly"]
77 assert param.name == "kwonly"
78 assert param.kind is ParameterKind.keyword_only
79 assert param.default is None
82def test_visit_function_keyword_only_param_with_default() -> None:
83 """Test functions parameters loading."""
84 with temporary_visited_module("def f(*, kwonly=0): ...") as module:
85 function = module["f"]
86 assert len(function.parameters) == 1
87 param = function.parameters[0]
88 assert param is function.parameters["kwonly"]
89 assert param.name == "kwonly"
90 assert param.kind is ParameterKind.keyword_only
91 assert param.default == "0"
94def test_visit_function_syntax_error() -> None:
95 """Test functions parameters loading."""
96 with pytest.raises(SyntaxError), temporary_visited_module("def f(/, poskw=0): ..."):
97 ...
100def test_visit_function_variadic_params() -> None:
101 """Test functions variadic parameters visit."""
102 with temporary_visited_module("def f(*args: str, kw=1, **kwargs: int): ...") as module:
103 function = module["f"]
104 assert len(function.parameters) == 3
105 param = function.parameters[0]
106 assert param.name == "args"
107 assert param.annotation.name == "str"
108 assert param.annotation.canonical_path == "str"
109 param = function.parameters[1]
110 assert param.annotation is None
111 param = function.parameters[2]
112 assert param.name == "kwargs"
113 assert param.annotation.name == "int"
114 assert param.annotation.canonical_path == "int"
117def test_visit_function_params_annotations() -> None:
118 """Test functions parameters loading."""
119 with temporary_visited_module(
120 """
121 import typing
122 from typing import Any
123 def f_annorations(
124 a: str,
125 b: Any,
126 c: typing.Optional[typing.List[int]],
127 d: float | None):
128 ...
129 """,
130 ) as module:
131 function = module["f_annorations"]
132 assert len(function.parameters) == 4
133 param = function.parameters[0]
134 assert param.annotation.name == "str"
135 assert param.annotation.canonical_path == "str"
136 param = function.parameters[1]
137 assert param.annotation.name == "Any"
138 assert param.annotation.canonical_path == "typing.Any"
139 param = function.parameters[2]
140 assert str(param.annotation) == "typing.Optional[typing.List[int]]"
141 param = function.parameters[3]
142 assert str(param.annotation) == "float | None"