| 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 |
|
| 16 |
a |
| 17 |
|
| 18 |
b |
| 19 |
|
| 20 |
c |
| 21 |
|
| 22 |
d |
| 23 |
|
| 24 |
e |
| 25 |
|
| 26 |
f |
| 27 |
|
| 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 |
|
| 46 |
return_none() |
| 47 |
|
| 48 |
|
| 49 |
def return_str() -> str: |
| 50 |
pass |
| 51 |
|
| 52 |
|
| 53 |
return_str() |
| 54 |
|
| 55 |
|
| 56 |
def return_custom_class() -> A: |
| 57 |
pass |
| 58 |
|
| 59 |
|
| 60 |
return_custom_class() |
| 61 |
|
| 62 |
|
| 63 |
def return_annotation_and_docstring() -> str: |
| 64 |
""" |
| 65 |
:rtype: int |
| 66 |
""" |
| 67 |
pass |
| 68 |
|
| 69 |
|
| 70 |
return_annotation_and_docstring() |
| 71 |
|
| 72 |
|
| 73 |
def return_annotation_and_docstring_different() -> str: |
| 74 |
""" |
| 75 |
:rtype: str |
| 76 |
""" |
| 77 |
pass |
| 78 |
|
| 79 |
|
| 80 |
return_annotation_and_docstring_different() |
| 81 |
|
| 82 |
|
| 83 |
def annotation_forward_reference(b: "B") -> "B": |
| 84 |
|
| 85 |
b |
| 86 |
|
| 87 |
|
| 88 |
annotation_forward_reference(1).t |
| 89 |
|
| 90 |
class B: |
| 91 |
test_element = 1 |
| 92 |
pass |
| 93 |
|
| 94 |
|
| 95 |
annotation_forward_reference(1) |
| 96 |
|
| 97 |
|
| 98 |
class SelfReference: |
| 99 |
test_element = 1 |
| 100 |
def test_method(self, x: "SelfReference") -> "SelfReference": |
| 101 |
|
| 102 |
x |
| 103 |
|
| 104 |
self.t |
| 105 |
|
| 106 |
x.t |
| 107 |
|
| 108 |
self.test_method(1).t |
| 109 |
|
| 110 |
|
| 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 |
|
| 120 |
|
| 121 |
x |
| 122 |
|
| 123 |
|
| 124 |
xx |
| 125 |
|
| 126 |
|
| 127 |
yy |
| 128 |
|
| 129 |
|
| 130 |
y |
| 131 |
|
| 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 |
|
| 140 |
x |
| 141 |
|
| 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 |
|
| 151 |
x |
| 152 |
|
| 153 |
y |
| 154 |
Y = int |
| 155 |
|
| 156 |
def just_because_we_can(x: "flo" + "at"): |
| 157 |
|
| 158 |
x |
| 159 |
|
| 160 |
|
| 161 |
def keyword_only(a: str, *, b: str): |
| 162 |
|
| 163 |
a.startswi |
| 164 |
|
| 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 |
|
| 175 |
args |
| 176 |
|
| 177 |
args[0] |
| 178 |
|
| 179 |
next(iter(kwargs.keys())) |
| 180 |
|
| 181 |
kwargs[''] |
| 182 |
|
| 183 |
class Test: |
| 184 |
str: str = 'abc' |
| 185 |
|
| 186 |
|
| 187 |
Test.str.upp |
| 188 |
|
| 189 |
class NotCalledClass: |
| 190 |
def __init__(self, x): |
| 191 |
self.x: int = x |
| 192 |
self.y: int = '' |
| 193 |
|
| 194 |
self.x |
| 195 |
|
| 196 |
self.y |
| 197 |
|
| 198 |
self.y |
| 199 |
self.z: int |
| 200 |
self.z = '' |
| 201 |
|
| 202 |
self.z |
| 203 |
self.w: float |
| 204 |
|
| 205 |
self.w |
| 206 |
|
| 207 |
def tuple_func() -> tuple[int, str]: |
| 208 |
return 1, "" |
| 209 |
|
| 210 |
x = tuple_func() |
| 211 |
a, b = x |
| 212 |
|
| 213 |
a |
| 214 |
|
| 215 |
b |
| 216 |
|
| 217 |
x[0] |
| 218 |
|
| 219 |
x[1] |
| 220 |
|
| 221 |
def check_newstyle_unions(u1: int | str, u2: list[int] | list[str]): |
| 222 |
|
| 223 |
u1 |
| 224 |
|
| 225 |
u2 |
| 226 |
|
| 227 |
u2[1] |
| 228 |
|
| 229 |
def use_type_with_annotation() -> type[int]: ... |
| 230 |
|
| 231 |
|
| 232 |
use_type_with_annotation() |
| 233 |
|
| 234 |
def union_with_forward_references(x: int | "str", y: "int" | str, z: "int | str"): |
| 235 |
|
| 236 |
x |
| 237 |
|
| 238 |
y |
| 239 |
|
| 240 |
z |