|
203 | 203 | "@patch_to(_T5, cls_method=True)\n", |
204 | 204 | "def func(cls, x): return cls.attr + x # you can access class attributes in the normal way\n", |
205 | 205 | "\n", |
206 | | - "t = _T5()\n", |
207 | | - "test_eq(t.func(4), 7)" |
| 206 | + "test_eq(_T5.func(4), 7)" |
208 | 207 | ] |
209 | 208 | }, |
210 | 209 | { |
|
259 | 258 | "outputs": [], |
260 | 259 | "source": [ |
261 | 260 | "#export\n", |
262 | | - "def patch(f):\n", |
| 261 | + "def patch(f=None, *, as_prop=False, cls_method=False):\n", |
263 | 262 | " \"Decorator: add `f` to the first parameter's class (based on f's type annotations)\"\n", |
| 263 | + " if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method)\n", |
264 | 264 | " cls = next(iter(f.__annotations__.values()))\n", |
265 | | - " return patch_to(cls)(f)" |
| 265 | + " return patch_to(cls, as_prop=as_prop, cls_method=cls_method)(f)" |
266 | 266 | ] |
267 | 267 | }, |
268 | 268 | { |
|
315 | 315 | "test_eq(t.func2.__qualname__, '_T9.func2')" |
316 | 316 | ] |
317 | 317 | }, |
| 318 | + { |
| 319 | + "cell_type": "markdown", |
| 320 | + "metadata": {}, |
| 321 | + "source": [ |
| 322 | + "Just like `patch_to` decorator you can use `as_propas_prop` and `cls_method` parameters with `patch` decorator:" |
| 323 | + ] |
| 324 | + }, |
| 325 | + { |
| 326 | + "cell_type": "code", |
| 327 | + "execution_count": null, |
| 328 | + "metadata": {}, |
| 329 | + "outputs": [], |
| 330 | + "source": [ |
| 331 | + "@patch(as_prop=True)\n", |
| 332 | + "def add_ten(self:_T5): return self + 10\n", |
| 333 | + "\n", |
| 334 | + "t = _T5(4)\n", |
| 335 | + "test_eq(t.add_ten, 14)" |
| 336 | + ] |
| 337 | + }, |
| 338 | + { |
| 339 | + "cell_type": "code", |
| 340 | + "execution_count": null, |
| 341 | + "metadata": {}, |
| 342 | + "outputs": [], |
| 343 | + "source": [ |
| 344 | + "class _T5(int): attr = 3 # attr is a class attribute we will access in a later method\n", |
| 345 | + " \n", |
| 346 | + "@patch(cls_method=True)\n", |
| 347 | + "def func(cls:_T5, x): return cls.attr + x # you can access class attributes in the normal way\n", |
| 348 | + "\n", |
| 349 | + "test_eq(_T5.func(4), 7)" |
| 350 | + ] |
| 351 | + }, |
318 | 352 | { |
319 | 353 | "cell_type": "code", |
320 | 354 | "execution_count": null, |
|
0 commit comments