| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
def decorator(func): |
| 6 |
def wrapper(*args): |
| 7 |
return func(1, *args) |
| 8 |
return wrapper |
| 9 |
|
| 10 |
@decorator |
| 11 |
def decorated(a,b): |
| 12 |
return a,b |
| 13 |
|
| 14 |
exe = decorated(set, '') |
| 15 |
|
| 16 |
|
| 17 |
exe[1] |
| 18 |
|
| 19 |
|
| 20 |
exe[0] |
| 21 |
|
| 22 |
|
| 23 |
def dec(func): |
| 24 |
def wrapper(*args, **kwargs): |
| 25 |
return func(*args, **kwargs) |
| 26 |
return wrapper |
| 27 |
|
| 28 |
@dec |
| 29 |
def fu(a, b, c, *args, **kwargs): |
| 30 |
return a, b, c, args, kwargs |
| 31 |
|
| 32 |
exe = fu(list, c=set, b=3, d='') |
| 33 |
|
| 34 |
|
| 35 |
exe[0] |
| 36 |
|
| 37 |
exe[1] |
| 38 |
|
| 39 |
exe[2] |
| 40 |
|
| 41 |
exe[3][0]. |
| 42 |
|
| 43 |
exe[4]['d'] |
| 44 |
|
| 45 |
|
| 46 |
exe = fu(list, set, 3, '', d='') |
| 47 |
|
| 48 |
|
| 49 |
exe[3][0] |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
def dec2(func2): |
| 55 |
def wrapper2(first_arg, *args2, **kwargs2): |
| 56 |
return func2(first_arg, *args2, **kwargs2) |
| 57 |
return wrapper2 |
| 58 |
|
| 59 |
@dec2 |
| 60 |
@dec |
| 61 |
def fu2(a, b, c, *args, **kwargs): |
| 62 |
return a, b, c, args, kwargs |
| 63 |
|
| 64 |
exe = fu2(list, c=set, b=3, d='str') |
| 65 |
|
| 66 |
|
| 67 |
exe[0] |
| 68 |
|
| 69 |
exe[1] |
| 70 |
|
| 71 |
exe[2] |
| 72 |
|
| 73 |
exe[3][0]. |
| 74 |
|
| 75 |
exe[4]['d'] |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
def same_func(func): |
| 82 |
return func |
| 83 |
|
| 84 |
class Decorator(object): |
| 85 |
def __init__(self, func): |
| 86 |
self.func = func |
| 87 |
|
| 88 |
def __call__(self, *args, **kwargs): |
| 89 |
return self.func(1, *args, **kwargs) |
| 90 |
|
| 91 |
@Decorator |
| 92 |
def nothing(a,b,c): |
| 93 |
return a,b,c |
| 94 |
|
| 95 |
|
| 96 |
nothing("")[0] |
| 97 |
|
| 98 |
nothing("")[1] |
| 99 |
|
| 100 |
|
| 101 |
@same_func |
| 102 |
@Decorator |
| 103 |
def nothing(a,b,c): |
| 104 |
return a,b,c |
| 105 |
|
| 106 |
|
| 107 |
nothing("")[0] |
| 108 |
|
| 109 |
class MethodDecoratorAsClass(): |
| 110 |
class_var = 3 |
| 111 |
@Decorator |
| 112 |
def func_without_self(arg, arg2): |
| 113 |
return arg, arg2 |
| 114 |
|
| 115 |
@Decorator |
| 116 |
def func_with_self(self, arg): |
| 117 |
return self.class_var |
| 118 |
|
| 119 |
|
| 120 |
MethodDecoratorAsClass().func_without_self('')[0] |
| 121 |
|
| 122 |
MethodDecoratorAsClass().func_without_self('')[1] |
| 123 |
|
| 124 |
MethodDecoratorAsClass().func_with_self(1) |
| 125 |
|
| 126 |
|
| 127 |
class SelfVars(): |
| 128 |
"""Init decorator problem as an instance, #247""" |
| 129 |
@Decorator |
| 130 |
def __init__(self): |
| 131 |
""" |
| 132 |
__init__ decorators should be ignored when looking up variables in the |
| 133 |
class. |
| 134 |
""" |
| 135 |
self.c = list |
| 136 |
|
| 137 |
@Decorator |
| 138 |
def shouldnt_expose_var(not_self): |
| 139 |
""" |
| 140 |
Even though in real Python this shouldn't expose the variable, in this |
| 141 |
case Jedi exposes the variable, because these kind of decorators are |
| 142 |
normally descriptors, which SHOULD be exposed (at least 90%). |
| 143 |
""" |
| 144 |
not_self.b = 1.0 |
| 145 |
|
| 146 |
def other_method(self): |
| 147 |
|
| 148 |
self.b |
| 149 |
|
| 150 |
self.c |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
@not_found_decorator |
| 156 |
def just_a_func(): |
| 157 |
return 1 |
| 158 |
|
| 159 |
|
| 160 |
just_a_func() |
| 161 |
|
| 162 |
|
| 163 |
just_a_func.__closure__ |
| 164 |
|
| 165 |
|
| 166 |
class JustAClass: |
| 167 |
@not_found_decorator2 |
| 168 |
def a(self): |
| 169 |
return 1 |
| 170 |
|
| 171 |
|
| 172 |
JustAClass().a.__call__ |
| 173 |
|
| 174 |
JustAClass().a() |
| 175 |
|
| 176 |
JustAClass.a.__call__ |
| 177 |
|
| 178 |
JustAClass.a() |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
class DecoratorWithoutCall(): |
| 185 |
def __init__(self, func): |
| 186 |
self.func = func |
| 187 |
|
| 188 |
@DecoratorWithoutCall |
| 189 |
def f(): |
| 190 |
return 1 |
| 191 |
|
| 192 |
|
| 193 |
@DecoratorWithoutCall(None) |
| 194 |
def g(): |
| 195 |
return 1 |
| 196 |
|
| 197 |
|
| 198 |
f() |
| 199 |
|
| 200 |
g() |
| 201 |
|
| 202 |
|
| 203 |
class X(): |
| 204 |
@str |
| 205 |
def x(self): |
| 206 |
pass |
| 207 |
|
| 208 |
def y(self): |
| 209 |
|
| 210 |
self.x |
| 211 |
|
| 212 |
self.x() |
| 213 |
|
| 214 |
|
| 215 |
def decorator_var_args(function, *args): |
| 216 |
return function(*args) |
| 217 |
|
| 218 |
@decorator_var_args |
| 219 |
def function_var_args(param): |
| 220 |
return param |
| 221 |
|
| 222 |
|
| 223 |
function_var_args(1) |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
def dec(f): |
| 230 |
def wrapper(s): |
| 231 |
return f(s) |
| 232 |
return wrapper |
| 233 |
|
| 234 |
class MethodDecorators(): |
| 235 |
_class_var = 1 |
| 236 |
def __init__(self): |
| 237 |
self._method_var = '' |
| 238 |
|
| 239 |
@dec |
| 240 |
def constant(self): |
| 241 |
return 1.0 |
| 242 |
|
| 243 |
@dec |
| 244 |
def class_var(self): |
| 245 |
return self._class_var |
| 246 |
|
| 247 |
@dec |
| 248 |
def method_var(self): |
| 249 |
return self._method_var |
| 250 |
|
| 251 |
|
| 252 |
MethodDecorators().constant() |
| 253 |
|
| 254 |
MethodDecorators().class_var() |
| 255 |
|
| 256 |
MethodDecorators().method_var() |
| 257 |
|
| 258 |
|
| 259 |
class Base(): |
| 260 |
@not_existing |
| 261 |
def __init__(self): |
| 262 |
pass |
| 263 |
@not_existing |
| 264 |
def b(self): |
| 265 |
return '' |
| 266 |
@dec |
| 267 |
def c(self): |
| 268 |
return 1 |
| 269 |
|
| 270 |
class MethodDecoratorDoesntExist(Base): |
| 271 |
"""#272 github: combination of method decorators and super()""" |
| 272 |
def a(self): |
| 273 |
|
| 274 |
super().__init__() |
| 275 |
|
| 276 |
super().b() |
| 277 |
|
| 278 |
super().c() |
| 279 |
|
| 280 |
self.d() |
| 281 |
|
| 282 |
@doesnt_exist |
| 283 |
def d(self): |
| 284 |
return 1.0 |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
def memoize(function): |
| 290 |
def wrapper(*args): |
| 291 |
if random.choice([0, 1]): |
| 292 |
pass |
| 293 |
else: |
| 294 |
rv = function(*args) |
| 295 |
return rv |
| 296 |
return wrapper |
| 297 |
|
| 298 |
@memoize |
| 299 |
def follow_statement(stmt): |
| 300 |
return stmt |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
follow_statement(1) |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
@should_ignore |
| 312 |
class A(): |
| 313 |
x = 3 |
| 314 |
def ret(self): |
| 315 |
return 1 |
| 316 |
|
| 317 |
|
| 318 |
A().ret() |
| 319 |
|
| 320 |
A().x |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
import abc |
| 328 |
|
| 329 |
@abc |
| 330 |
|
| 331 |
|
| 332 |
@abc.abstractmethod |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
x = 1 |
| 338 |
|
| 339 |
|
| 340 |
@x.foo() |
| 341 |
def f(): pass |
| 342 |
|
| 343 |
|
| 344 |
@x.foo() |
| 345 |
def f(): pass |