Debian Codegraph @

test/completion/pep0484_basic.py

raw symbols SCIP
1 """ Pep-0484 type hinting """
2
3
4 class A():
5 pass
6
7
8 def function_parameters(a: A, b, c: str, d: int, e: str, f: str, g: int=4):
9 """
10 :param e: if docstring and annotation agree, only one should be returned
11 :type e: str
12 :param f: if docstring and annotation disagree, both should be returned
13 :type f: int
14 """
15 #? A()
16 a
17 #?
18 b
19 #? str()
20 c
21 #? int()
22 d
23 #? str()
24 e
25 #? str()
26 f
27 # int()
28 g
29
30
31 def return_unspecified():
32 pass
33
34 #?
35 return_unspecified()
36
37
38 def return_none() -> None:
39 """
40 Return type None means the same as no return type as far as jedi
41 is concerned
42 """
43 pass
44
45 #? None
46 return_none()
47
48
49 def return_str() -> str:
50 pass
51
52 #? str()
53 return_str()
54
55
56 def return_custom_class() -> A:
57 pass
58
59 #? A()
60 return_custom_class()
61
62
63 def return_annotation_and_docstring() -> str:
64 """
65 :rtype: int
66 """
67 pass
68
69 #? str()
70 return_annotation_and_docstring()
71
72
73 def return_annotation_and_docstring_different() -> str:
74 """
75 :rtype: str
76 """
77 pass
78
79 #? str()
80 return_annotation_and_docstring_different()
81
82
83 def annotation_forward_reference(b: "B") -> "B":
84 #? B()
85 b
86
87 #? ["test_element"]
88 annotation_forward_reference(1).t
89
90 class B:
91 test_element = 1
92 pass
93
94 #? B()
95 annotation_forward_reference(1)
96
97
98 class SelfReference:
99 test_element = 1
100 def test_method(self, x: "SelfReference") -> "SelfReference":
101 #? SelfReference()
102 x
103 #? ["test_element", "test_method"]
104 self.t
105 #? ["test_element", "test_method"]
106 x.t
107 #? ["test_element", "test_method"]
108 self.test_method(1).t
109
110 #? SelfReference()
111 SelfReference().test_method()
112
113 def function_with_non_pep_0484_annotation(
114 x: "I can put anything here",
115 xx: "",
116 yy: "\r\n\0;+*&^564835(---^&*34",
117 y: 3 + 3,
118 zz: float) -> int("42"):
119 # infers int from function call
120 #? int()
121 x
122 # infers int from function call
123 #? int()
124 xx
125 # infers int from function call
126 #? int()
127 yy
128 # infers str from function call
129 #? str()
130 y
131 #? float()
132 zz
133 #?
134 function_with_non_pep_0484_annotation(1, 2, 3, "force string")
135
136 def function_forward_reference_dynamic(
137 x: return_str_type(),
138 y: "return_str_type()") -> None:
139 #? str()
140 x
141 #? str()
142 y
143
144 def return_str_type():
145 return str
146
147
148 X = str
149 def function_with_assined_class_in_reference(x: X, y: "Y"):
150 #? str()
151 x
152 #? int()
153 y
154 Y = int
155
156 def just_because_we_can(x: "flo" + "at"):
157 #? float()
158 x
159
160
161 def keyword_only(a: str, *, b: str):
162 #? ['startswith']
163 a.startswi
164 #? ['startswith']
165 b.startswi
166
167
168 def argskwargs(*args: int, **kwargs: float):
169 """
170 This might be a bit confusing, but is part of the standard.
171 args is changed to Tuple[int] in this case and kwargs to Dict[str, float],
172 which makes sense if you think about it a bit.
173 """
174 #? tuple()
175 args
176 #? int()
177 args[0]
178 #? str()
179 next(iter(kwargs.keys()))
180 #? float()
181 kwargs['']
182
183 class Test:
184 str: str = 'abc'
185
186 #? ['upper']
187 Test.str.upp
188
189 class NotCalledClass:
190 def __init__(self, x):
191 self.x: int = x
192 self.y: int = ''
193 #? int()
194 self.x
195 #? int()
196 self.y
197 #? int()
198 self.y
199 self.z: int
200 self.z = ''
201 #? str() int()
202 self.z
203 self.w: float
204 #? float()
205 self.w
206
207 def tuple_func() -> tuple[int, str]:
208 return 1, ""
209
210 x = tuple_func()
211 a, b = x
212 #? int()
213 a
214 #? str()
215 b
216 #? int()
217 x[0]
218 #? str()
219 x[1]
220
221 def check_newstyle_unions(u1: int | str, u2: list[int] | list[str]):
222 #? int() str()
223 u1
224 #? list()
225 u2
226 #? int() str()
227 u2[1]
228
229 def use_type_with_annotation() -> type[int]: ...
230
231 #? int
232 use_type_with_annotation()
233
234 def union_with_forward_references(x: int | "str", y: "int" | str, z: "int | str"):
235 #? int() str()
236 x
237 #? int() str()
238 y
239 #? int() str()
240 z