| 1 |
#!/usr/bin/env python |
| 2 |
from __future__ import annotations |
| 3 |
import ast |
| 4 |
import os |
| 5 |
import platform |
| 6 |
import pytest |
| 7 |
import random |
| 8 |
import shutil |
| 9 |
import sys |
| 10 |
import tempfile |
| 11 |
import textwrap |
| 12 |
import time |
| 13 |
import unittest |
| 14 |
from types import ModuleType |
| 15 |
|
| 16 |
from IPython.extensions.autoreload import AutoreloadMagics |
| 17 |
|
| 18 |
from IPython.extensions.deduperreload.deduperreload import compare_ast, DeduperReloader |
| 19 |
|
| 20 |
if platform.python_implementation() != "CPython": |
| 21 |
pytest.skip( |
| 22 |
"We do not support non-CPython versions.", |
| 23 |
allow_module_level=True, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
class DeduperTestReloader(DeduperReloader): |
| 28 |
def __init__(self, *args, **kwargs) -> None: |
| 29 |
super().__init__(*args, **kwargs) |
| 30 |
self.exceptions_raised: list[Exception] = [] |
| 31 |
|
| 32 |
def _patch_namespace( |
| 33 |
self, module: ModuleType | type, prefixes: list[str] | None = None |
| 34 |
) -> bool: |
| 35 |
try: |
| 36 |
assert super()._patch_namespace(module, prefixes) |
| 37 |
return True |
| 38 |
except Exception as e: |
| 39 |
self.exceptions_raised.append(e) |
| 40 |
return False |
| 41 |
|
| 42 |
|
| 43 |
def is_nonempty_file(fname: str) -> bool: |
| 44 |
return len(fname) > 0 |
| 45 |
|
| 46 |
|
| 47 |
def squish_text(text: str) -> str: |
| 48 |
""" |
| 49 |
Turns text like this: |
| 50 |
|
| 51 |
''' def foo(): |
| 52 |
return "bar" |
| 53 |
def baz(): |
| 54 |
return "bat" |
| 55 |
def bam(): |
| 56 |
return "bat" |
| 57 |
''' |
| 58 |
|
| 59 |
into this: |
| 60 |
|
| 61 |
'''def foo(): |
| 62 |
return "bar" |
| 63 |
def baz(): |
| 64 |
return "bat" |
| 65 |
def bam(): |
| 66 |
return "bat" |
| 67 |
''' |
| 68 |
|
| 69 |
The former is common when we are trying to use string templates |
| 70 |
whose parameters are multiline and unaware of the existing indentation. |
| 71 |
|
| 72 |
:param text: a string with messed up indentation |
| 73 |
:return: `text` but with indentation fixed |
| 74 |
""" |
| 75 |
prev_indentation = 0 |
| 76 |
transformed_text_lines = [] |
| 77 |
for line in text.strip("\n").splitlines(): |
| 78 |
line_without_indentation = line.lstrip() |
| 79 |
indentation = len(line) - len(line_without_indentation) |
| 80 |
if indentation == 0: |
| 81 |
indentation = prev_indentation |
| 82 |
else: |
| 83 |
prev_indentation = indentation |
| 84 |
transformed_text_lines.append( |
| 85 |
textwrap.indent(line_without_indentation, " " * indentation) |
| 86 |
) |
| 87 |
return textwrap.dedent("\n".join(transformed_text_lines)) |
| 88 |
|
| 89 |
|
| 90 |
class AutoreloadDetectionSuite(unittest.TestCase): |
| 91 |
""" |
| 92 |
Unit tests for autoreload testing logic |
| 93 |
""" |
| 94 |
|
| 95 |
def test_compare_ast(self): |
| 96 |
code1 = squish_text( |
| 97 |
""" |
| 98 |
def factorial(n): |
| 99 |
def fn(sdfsdf): |
| 100 |
print(sdfsdf) |
| 101 |
if n < 0: |
| 102 |
return "Factorial is not defined for negative numbers." |
| 103 |
elif n == 0 or n == 1: |
| 104 |
return 1 |
| 105 |
else: |
| 106 |
result = 1 |
| 107 |
for i in range(2, n + 1): |
| 108 |
result *= i |
| 109 |
return result |
| 110 |
y = 12 |
| 111 |
print(y) |
| 112 |
print(1) |
| 113 |
x = 1212121 |
| 114 |
""" |
| 115 |
) |
| 116 |
code2 = squish_text( |
| 117 |
""" |
| 118 |
def factorial(n): |
| 119 |
def fn(sdfsdf): |
| 120 |
print(sdfsdf+"!!!") |
| 121 |
if n < 0: |
| 122 |
return "Factorial is not defined for negative numbers." |
| 123 |
elif n == 0 or n == 1: |
| 124 |
return 1 |
| 125 |
else: |
| 126 |
result = 1 |
| 127 |
for i in range(2, n + 1): |
| 128 |
result *= i |
| 129 |
return result |
| 130 |
y = 12 |
| 131 |
print(y) |
| 132 |
print(1) |
| 133 |
x = 1212121 |
| 134 |
""" |
| 135 |
) |
| 136 |
ast_1 = ast.parse(code1) |
| 137 |
ast_2 = ast.parse(code2) |
| 138 |
|
| 139 |
assert not compare_ast(ast_1, ast_2) |
| 140 |
|
| 141 |
def test_compare_ast2(self): |
| 142 |
code1 = squish_text( |
| 143 |
""" |
| 144 |
def factorial(n): |
| 145 |
def fn(sdfsdf): |
| 146 |
print(sdfsdf) |
| 147 |
if n < 0: |
| 148 |
return "Factorial is not defined for negative numbers." |
| 149 |
elif n == 0 or n == 1: |
| 150 |
return 1 |
| 151 |
else: |
| 152 |
result = 1 |
| 153 |
for i in range(2, n + 1): |
| 154 |
result *= i |
| 155 |
return result |
| 156 |
y = 12 |
| 157 |
print(y) |
| 158 |
print(1) |
| 159 |
x = 1212121 |
| 160 |
""" |
| 161 |
) |
| 162 |
code2 = squish_text( |
| 163 |
""" |
| 164 |
def factorial(n): |
| 165 |
def fn(sdfsdf): |
| 166 |
print(sdfsdf) |
| 167 |
if n < 0: |
| 168 |
return "Factorial is not defined for negative numbers." |
| 169 |
elif n == 0 or n == 1: |
| 170 |
return 1 |
| 171 |
else: |
| 172 |
result = 1 |
| 173 |
for i in range(2, n + 1): |
| 174 |
result *= i |
| 175 |
return result |
| 176 |
y = 12 |
| 177 |
print(y) |
| 178 |
print(1) |
| 179 |
x = 1212121 |
| 180 |
""" |
| 181 |
) |
| 182 |
ast_1 = ast.parse(code1) |
| 183 |
ast_2 = ast.parse(code2) |
| 184 |
|
| 185 |
assert compare_ast(ast_1, ast_2) |
| 186 |
|
| 187 |
def test_autoreload_no_changes(self): |
| 188 |
code1 = squish_text( |
| 189 |
""" |
| 190 |
def factorial(n): |
| 191 |
print(n) |
| 192 |
print(1) |
| 193 |
x = 1212121 |
| 194 |
""" |
| 195 |
) |
| 196 |
code2 = squish_text( |
| 197 |
""" |
| 198 |
def factorial(n): |
| 199 |
print(n) |
| 200 |
print(1) |
| 201 |
x = 1212121 |
| 202 |
""" |
| 203 |
) |
| 204 |
deduperreloader = DeduperTestReloader() |
| 205 |
ast_1 = ast.parse(code1) |
| 206 |
ast_2 = ast.parse(code2) |
| 207 |
|
| 208 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 209 |
assert deduperreloader._to_autoreload.defs_to_reload == [] |
| 210 |
|
| 211 |
def test_autoreload_static_assign_change_outside_function(self): |
| 212 |
code1 = squish_text( |
| 213 |
""" |
| 214 |
def factorial(n): |
| 215 |
print(n) |
| 216 |
print(1) |
| 217 |
""" |
| 218 |
) |
| 219 |
code2 = squish_text( |
| 220 |
""" |
| 221 |
def factorial(n): |
| 222 |
print(n) |
| 223 |
print(1) |
| 224 |
x = 1212121 |
| 225 |
""" |
| 226 |
) |
| 227 |
deduperreloader = DeduperTestReloader() |
| 228 |
ast_1 = ast.parse(code1) |
| 229 |
ast_2 = ast.parse(code2) |
| 230 |
|
| 231 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 232 |
|
| 233 |
def test_autoreload_changes_inside_function(self): |
| 234 |
code1 = squish_text( |
| 235 |
""" |
| 236 |
def factorial(n): |
| 237 |
print(n) |
| 238 |
print(1) |
| 239 |
""" |
| 240 |
) |
| 241 |
code2 = squish_text( |
| 242 |
""" |
| 243 |
def factorial(n): |
| 244 |
print(n + "edit!") |
| 245 |
print(1) |
| 246 |
""" |
| 247 |
) |
| 248 |
deduperreloader = DeduperTestReloader() |
| 249 |
ast_1 = ast.parse(code1) |
| 250 |
ast_2 = ast.parse(code2) |
| 251 |
|
| 252 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 253 |
assert len(deduperreloader._to_autoreload.defs_to_reload) == 1 |
| 254 |
|
| 255 |
def test_autoreload_changes_inside_and_outside_function(self): |
| 256 |
code1 = squish_text( |
| 257 |
""" |
| 258 |
def factorial(n): |
| 259 |
print(n) |
| 260 |
print(1) |
| 261 |
""" |
| 262 |
) |
| 263 |
code2 = squish_text( |
| 264 |
""" |
| 265 |
def factorial(n): |
| 266 |
print(n + "edit!") |
| 267 |
print(2) |
| 268 |
""" |
| 269 |
) |
| 270 |
deduperreloader = DeduperTestReloader() |
| 271 |
ast_1 = ast.parse(code1) |
| 272 |
ast_2 = ast.parse(code2) |
| 273 |
|
| 274 |
assert not deduperreloader.detect_autoreload(ast_1, ast_2) |
| 275 |
|
| 276 |
def test_autoreload_changes_inner_function(self): |
| 277 |
code1 = squish_text( |
| 278 |
""" |
| 279 |
def factorial(n): |
| 280 |
def foo(): |
| 281 |
x = 3 |
| 282 |
return x |
| 283 |
return foo |
| 284 |
""" |
| 285 |
) |
| 286 |
code2 = squish_text( |
| 287 |
""" |
| 288 |
def factorial(n): |
| 289 |
def foo(): |
| 290 |
x = 4 |
| 291 |
return x |
| 292 |
return foo |
| 293 |
""" |
| 294 |
) |
| 295 |
deduperreloader = DeduperTestReloader() |
| 296 |
ast_1 = ast.parse(code1) |
| 297 |
ast_2 = ast.parse(code2) |
| 298 |
|
| 299 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 300 |
assert len(deduperreloader._to_autoreload.defs_to_reload) == 1 |
| 301 |
|
| 302 |
def test_autoreload_changes_multiple_function(self): |
| 303 |
code1 = squish_text( |
| 304 |
""" |
| 305 |
def factorial(n): |
| 306 |
def foo(): |
| 307 |
x = 3 |
| 308 |
return x |
| 309 |
return foo |
| 310 |
def bar(): |
| 311 |
return 1 |
| 312 |
""" |
| 313 |
) |
| 314 |
code2 = squish_text( |
| 315 |
""" |
| 316 |
def factorial(n): |
| 317 |
def foo(): |
| 318 |
x = 4 |
| 319 |
return x |
| 320 |
return foo |
| 321 |
def bar(): |
| 322 |
return 2 |
| 323 |
""" |
| 324 |
) |
| 325 |
deduperreloader = DeduperTestReloader() |
| 326 |
ast_1 = ast.parse(code1) |
| 327 |
ast_2 = ast.parse(code2) |
| 328 |
|
| 329 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 330 |
assert len(deduperreloader._to_autoreload.defs_to_reload) == 2 |
| 331 |
|
| 332 |
def test_autoreload_change_one_function_of_multiple(self): |
| 333 |
code1 = squish_text( |
| 334 |
""" |
| 335 |
def factorial(n): |
| 336 |
def foo(): |
| 337 |
x = 3 |
| 338 |
return x |
| 339 |
return foo |
| 340 |
def bar(): |
| 341 |
return 1 |
| 342 |
""" |
| 343 |
) |
| 344 |
code2 = squish_text( |
| 345 |
""" |
| 346 |
def factorial(n): |
| 347 |
def foo(): |
| 348 |
x = 4 |
| 349 |
return x |
| 350 |
return foo |
| 351 |
def bar(): |
| 352 |
return 1 |
| 353 |
""" |
| 354 |
) |
| 355 |
deduperreloader = DeduperTestReloader() |
| 356 |
ast_1 = ast.parse(code1) |
| 357 |
ast_2 = ast.parse(code2) |
| 358 |
|
| 359 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 360 |
assert len(deduperreloader._to_autoreload.defs_to_reload) == 1 |
| 361 |
|
| 362 |
def test_autoreload_handling_moves(self): |
| 363 |
code1 = squish_text( |
| 364 |
""" |
| 365 |
def factorial(n): |
| 366 |
return 1 |
| 367 |
x = 1 |
| 368 |
""" |
| 369 |
) |
| 370 |
code2 = squish_text( |
| 371 |
""" |
| 372 |
x = 1 |
| 373 |
def factorial(n): |
| 374 |
return 1 |
| 375 |
""" |
| 376 |
) |
| 377 |
deduperreloader = DeduperTestReloader() |
| 378 |
ast_1 = ast.parse(code1) |
| 379 |
ast_2 = ast.parse(code2) |
| 380 |
|
| 381 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 382 |
assert len(deduperreloader._to_autoreload.defs_to_reload) == 0 |
| 383 |
|
| 384 |
def test_autoreload_handling_function_moves_success(self): |
| 385 |
code1 = squish_text( |
| 386 |
""" |
| 387 |
def factorial(n): |
| 388 |
return 1 |
| 389 |
x = 1 |
| 390 |
def foo(): |
| 391 |
return 23 |
| 392 |
x = 2 |
| 393 |
""" |
| 394 |
) |
| 395 |
code2 = squish_text( |
| 396 |
""" |
| 397 |
x = 1 |
| 398 |
x = 2 |
| 399 |
def foo(): |
| 400 |
return 23 |
| 401 |
def factorial(n): |
| 402 |
return 1 |
| 403 |
""" |
| 404 |
) |
| 405 |
deduperreloader = DeduperTestReloader() |
| 406 |
ast_1 = ast.parse(code1) |
| 407 |
ast_2 = ast.parse(code2) |
| 408 |
|
| 409 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 410 |
assert len(deduperreloader._to_autoreload.defs_to_reload) == 0 |
| 411 |
|
| 412 |
def test_autoreload_handling_function_moves_only(self): |
| 413 |
code1 = squish_text( |
| 414 |
""" |
| 415 |
def factorial(n): |
| 416 |
return 1 |
| 417 |
x = 1 |
| 418 |
x = 2 |
| 419 |
""" |
| 420 |
) |
| 421 |
code2 = squish_text( |
| 422 |
""" |
| 423 |
x = 1 |
| 424 |
x = 2 |
| 425 |
def factorial(n): |
| 426 |
return 1 |
| 427 |
""" |
| 428 |
) |
| 429 |
deduperreloader = DeduperTestReloader() |
| 430 |
ast_1 = ast.parse(code1) |
| 431 |
ast_2 = ast.parse(code2) |
| 432 |
|
| 433 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 434 |
|
| 435 |
def test_autoreload_handles_new_imports(self): |
| 436 |
code1 = squish_text( |
| 437 |
""" |
| 438 |
def factorial(n): |
| 439 |
return 1 |
| 440 |
x = 1 |
| 441 |
""" |
| 442 |
) |
| 443 |
code2 = squish_text( |
| 444 |
""" |
| 445 |
import ast |
| 446 |
def factorial(n): |
| 447 |
return 1 |
| 448 |
x = 1 |
| 449 |
""" |
| 450 |
) |
| 451 |
deduperreloader = DeduperTestReloader() |
| 452 |
ast_1 = ast.parse(code1) |
| 453 |
ast_2 = ast.parse(code2) |
| 454 |
|
| 455 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 456 |
|
| 457 |
def test_autoreload_async_function(self): |
| 458 |
code1 = squish_text( |
| 459 |
""" |
| 460 |
async def sleep(): |
| 461 |
print(f'Time: {time.time() - start:.2f}') |
| 462 |
await asyncio.sleep(1) |
| 463 |
""" |
| 464 |
) |
| 465 |
code2 = squish_text( |
| 466 |
""" |
| 467 |
async def sleep(): |
| 468 |
print(f'Time: {time.time() - start:.2f}') |
| 469 |
await asyncio.sleep(10) |
| 470 |
""" |
| 471 |
) |
| 472 |
deduperreloader = DeduperTestReloader() |
| 473 |
ast_1 = ast.parse(code1) |
| 474 |
ast_2 = ast.parse(code2) |
| 475 |
|
| 476 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 477 |
assert len(deduperreloader._to_autoreload.defs_to_reload) == 1 |
| 478 |
|
| 479 |
def test_autoreload_add_function(self): |
| 480 |
code1 = squish_text( |
| 481 |
""" |
| 482 |
async def sleep(): |
| 483 |
print(f'Time: {time.time() - start:.2f}') |
| 484 |
await asyncio.sleep(1) |
| 485 |
""" |
| 486 |
) |
| 487 |
code2 = squish_text( |
| 488 |
""" |
| 489 |
async def sleep(): |
| 490 |
print(f'Time: {time.time() - start:.2f}') |
| 491 |
await asyncio.sleep(1) |
| 492 |
def add(x,y): |
| 493 |
pass |
| 494 |
""" |
| 495 |
) |
| 496 |
deduperreloader = DeduperTestReloader() |
| 497 |
ast_1 = ast.parse(code1) |
| 498 |
ast_2 = ast.parse(code2) |
| 499 |
|
| 500 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 501 |
assert list(d[0][0] for d in deduperreloader._to_autoreload.defs_to_reload) == [ |
| 502 |
"add" |
| 503 |
] |
| 504 |
|
| 505 |
def test_autoreload_add_function_ellipsis(self): |
| 506 |
code1 = squish_text( |
| 507 |
""" |
| 508 |
async def sleep(): |
| 509 |
print(f'Time: {time.time() - start:.2f}') |
| 510 |
await asyncio.sleep(1) |
| 511 |
""" |
| 512 |
) |
| 513 |
code2 = squish_text( |
| 514 |
""" |
| 515 |
async def sleep(): |
| 516 |
print(f'Time: {time.time() - start:.2f}') |
| 517 |
await asyncio.sleep(1) |
| 518 |
def add(x,y): |
| 519 |
... |
| 520 |
""" |
| 521 |
) |
| 522 |
deduperreloader = DeduperTestReloader() |
| 523 |
ast_1 = ast.parse(code1) |
| 524 |
ast_2 = ast.parse(code2) |
| 525 |
|
| 526 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 527 |
assert list(d[0][0] for d in deduperreloader._to_autoreload.defs_to_reload) == [ |
| 528 |
"add" |
| 529 |
] |
| 530 |
|
| 531 |
|
| 532 |
class AutoreloadPatchingSuite(unittest.TestCase): |
| 533 |
""" |
| 534 |
Unit tests for autoreload patching logic |
| 535 |
""" |
| 536 |
|
| 537 |
def setUp(self) -> None: |
| 538 |
self.deduperreloader = DeduperTestReloader() |
| 539 |
|
| 540 |
def test_patching(self): |
| 541 |
code1 = squish_text( |
| 542 |
""" |
| 543 |
def foo(): |
| 544 |
return 1 |
| 545 |
""" |
| 546 |
) |
| 547 |
code2 = squish_text( |
| 548 |
""" |
| 549 |
def foo(): |
| 550 |
return 2 |
| 551 |
""" |
| 552 |
) |
| 553 |
self.deduperreloader._to_autoreload.defs_to_reload = [ |
| 554 |
(("foo",), ast.parse(code2)) |
| 555 |
] |
| 556 |
mod = ModuleType("mod") |
| 557 |
exec(code1, mod.__dict__) |
| 558 |
self.deduperreloader._patch_namespace(mod) |
| 559 |
assert mod.foo() == 2 |
| 560 |
|
| 561 |
def test_patching_parameters(self): |
| 562 |
code1 = squish_text( |
| 563 |
""" |
| 564 |
def foo(n,s): |
| 565 |
return n+s |
| 566 |
""" |
| 567 |
) |
| 568 |
code2 = squish_text( |
| 569 |
""" |
| 570 |
def foo(n): |
| 571 |
return n |
| 572 |
""" |
| 573 |
) |
| 574 |
self.deduperreloader._to_autoreload.defs_to_reload = [ |
| 575 |
(("foo",), ast.parse(code2)) |
| 576 |
] |
| 577 |
mod = ModuleType("mod") |
| 578 |
exec(code1, mod.__dict__) |
| 579 |
self.deduperreloader._patch_namespace(mod) |
| 580 |
assert mod.foo(2) == 2 |
| 581 |
|
| 582 |
def test_add_function(self): |
| 583 |
code1 = squish_text( |
| 584 |
""" |
| 585 |
def foo2(n): |
| 586 |
return n |
| 587 |
""" |
| 588 |
) |
| 589 |
code2 = squish_text( |
| 590 |
""" |
| 591 |
def foo(n): |
| 592 |
return 55 |
| 593 |
""" |
| 594 |
) |
| 595 |
self.deduperreloader._to_autoreload.defs_to_reload = [ |
| 596 |
(("foo",), ast.parse(code2)) |
| 597 |
] |
| 598 |
mod = ModuleType("mod") |
| 599 |
exec(code1, mod.__dict__) |
| 600 |
self.deduperreloader._patch_namespace(mod) |
| 601 |
assert mod.foo(2) == 55 |
| 602 |
assert mod.foo2(2) == 2 |
| 603 |
|
| 604 |
def test_two_operations(self): |
| 605 |
code1 = squish_text( |
| 606 |
""" |
| 607 |
def foo(n): |
| 608 |
return 1 |
| 609 |
""" |
| 610 |
) |
| 611 |
code2 = squish_text( |
| 612 |
""" |
| 613 |
def foo(n): |
| 614 |
return 55 |
| 615 |
""" |
| 616 |
) |
| 617 |
code3 = squish_text( |
| 618 |
""" |
| 619 |
def goo(): |
| 620 |
return -1 |
| 621 |
def foo(n): |
| 622 |
x = 2 |
| 623 |
return x+n |
| 624 |
def bar(): |
| 625 |
return 200 |
| 626 |
""" |
| 627 |
) |
| 628 |
self.deduperreloader._to_autoreload.defs_to_reload = [ |
| 629 |
(("foo",), ast.parse(code2)) |
| 630 |
] |
| 631 |
mod = ModuleType("mod") |
| 632 |
exec(code1, mod.__dict__) |
| 633 |
assert mod.foo(2) == 1 |
| 634 |
self.deduperreloader._patch_namespace(mod) |
| 635 |
assert mod.foo(2) == 55 |
| 636 |
self.deduperreloader._to_autoreload.defs_to_reload = [ |
| 637 |
(("foo",), ast.parse(code3)) |
| 638 |
] |
| 639 |
self.deduperreloader._patch_namespace(mod) |
| 640 |
assert mod.foo(2) == 4 |
| 641 |
|
| 642 |
def test_using_outside_param(self): |
| 643 |
code1 = squish_text( |
| 644 |
""" |
| 645 |
x=1 |
| 646 |
def foo(n): |
| 647 |
return 1 |
| 648 |
""" |
| 649 |
) |
| 650 |
code2 = squish_text( |
| 651 |
""" |
| 652 |
x=1 |
| 653 |
def foo(n): |
| 654 |
return 55+x |
| 655 |
""" |
| 656 |
) |
| 657 |
self.deduperreloader._to_autoreload.defs_to_reload = [ |
| 658 |
(("foo",), ast.parse(code2)) |
| 659 |
] |
| 660 |
mod = ModuleType("mod") |
| 661 |
exec(code1, mod.__dict__) |
| 662 |
assert mod.foo(2) == 1 |
| 663 |
self.deduperreloader._patch_namespace(mod) |
| 664 |
assert mod.foo(2) == 56 |
| 665 |
|
| 666 |
def test_importing_func(self): |
| 667 |
code1 = squish_text( |
| 668 |
""" |
| 669 |
from os import environ |
| 670 |
def foo(n): |
| 671 |
pass |
| 672 |
""" |
| 673 |
) |
| 674 |
code2 = squish_text( |
| 675 |
""" |
| 676 |
from os import environ |
| 677 |
def foo(): |
| 678 |
environ._data |
| 679 |
return 1 |
| 680 |
""" |
| 681 |
) |
| 682 |
self.deduperreloader._to_autoreload.defs_to_reload = [ |
| 683 |
(("foo",), ast.parse(code2)) |
| 684 |
] |
| 685 |
mod = ModuleType("mod") |
| 686 |
exec(code1, mod.__dict__) |
| 687 |
self.deduperreloader._patch_namespace(mod) |
| 688 |
assert mod.foo() == 1 |
| 689 |
|
| 690 |
|
| 691 |
class FakeShell: |
| 692 |
def __init__(self): |
| 693 |
self.ns = {} |
| 694 |
self.user_ns = self.ns |
| 695 |
self.user_ns_hidden = {} |
| 696 |
self.auto_magics = AutoreloadMagics(shell=self) |
| 697 |
|
| 698 |
@staticmethod |
| 699 |
def pre_run_cell(obj): |
| 700 |
try_with_arg = False |
| 701 |
try: |
| 702 |
obj.pre_run_cell() |
| 703 |
except TypeError: |
| 704 |
try_with_arg = True |
| 705 |
if try_with_arg: |
| 706 |
obj.pre_run_cell(None) |
| 707 |
|
| 708 |
@staticmethod |
| 709 |
def post_run_cell(obj): |
| 710 |
try_with_arg = False |
| 711 |
try: |
| 712 |
obj.post_run_cell() |
| 713 |
except TypeError: |
| 714 |
try_with_arg = True |
| 715 |
if try_with_arg: |
| 716 |
obj.post_run_cell(None) |
| 717 |
|
| 718 |
def run_code(self, code): |
| 719 |
self.pre_run_cell(self.auto_magics) |
| 720 |
exec(code, self.user_ns) |
| 721 |
self.auto_magics.post_execute_hook() |
| 722 |
|
| 723 |
def push(self, items): |
| 724 |
self.ns.update(items) |
| 725 |
|
| 726 |
def magic_autoreload(self, parameter): |
| 727 |
self.auto_magics.autoreload(parameter) |
| 728 |
|
| 729 |
|
| 730 |
class ShellFixture(unittest.TestCase): |
| 731 |
"""Fixture for creating test module files""" |
| 732 |
|
| 733 |
test_dir = None |
| 734 |
old_sys_path = None |
| 735 |
filename_chars = "abcdefghijklmopqrstuvwxyz0123456789" |
| 736 |
|
| 737 |
def setUp(self): |
| 738 |
self.created_temp_modules = set() |
| 739 |
self.test_dir = tempfile.mkdtemp() |
| 740 |
self.old_sys_path = list(sys.path) |
| 741 |
sys.path.insert(0, self.test_dir) |
| 742 |
self.shell = FakeShell() |
| 743 |
|
| 744 |
def tearDown(self): |
| 745 |
for mod_name in self.created_temp_modules: |
| 746 |
sys.modules.pop(mod_name, None) |
| 747 |
shutil.rmtree(self.test_dir) |
| 748 |
sys.path = self.old_sys_path |
| 749 |
|
| 750 |
self.test_dir = None |
| 751 |
self.old_sys_path = None |
| 752 |
self.shell = None |
| 753 |
|
| 754 |
def get_module(self): |
| 755 |
module_name = "tmpmod_" + "".join(random.sample(self.filename_chars, 20)) |
| 756 |
if module_name in sys.modules: |
| 757 |
del sys.modules[module_name] |
| 758 |
file_name = os.path.join(self.test_dir, module_name + ".py") |
| 759 |
return module_name, file_name |
| 760 |
|
| 761 |
def write_file(self, filename, content): |
| 762 |
""" |
| 763 |
Write a file, and force a timestamp difference of at least one second |
| 764 |
|
| 765 |
Notes |
| 766 |
----- |
| 767 |
Python's .pyc files record the timestamp of their compilation |
| 768 |
with a time resolution of one second. |
| 769 |
|
| 770 |
Therefore, we need to force a timestamp difference between .py |
| 771 |
and .pyc, without having the .py file be timestamped in the |
| 772 |
future, and without changing the timestamp of the .pyc file |
| 773 |
(because that is stored in the file). The only reliable way |
| 774 |
to achieve this seems to be to sleep. |
| 775 |
|
| 776 |
Doesn't seem necessary on Darwin so we make this the exception. |
| 777 |
""" |
| 778 |
if platform.system().lower() != "darwin": |
| 779 |
time.sleep(1.05) |
| 780 |
with open(filename, "w", encoding="utf-8") as f: |
| 781 |
f.write(squish_text(content)) |
| 782 |
|
| 783 |
def new_module(self, code): |
| 784 |
mod_name, mod_fn = self.get_module() |
| 785 |
with open(mod_fn, "w", encoding="utf-8") as f: |
| 786 |
f.write(squish_text(code)) |
| 787 |
self.created_temp_modules.add(mod_name) |
| 788 |
return mod_name, mod_fn |
| 789 |
|
| 790 |
|
| 791 |
class AutoreloadHookSuite(ShellFixture): |
| 792 |
def test_deduperreloader_basic(self): |
| 793 |
self.shell.magic_autoreload("2") |
| 794 |
mod_name, mod_fn = self.new_module( |
| 795 |
""" |
| 796 |
x = 9 |
| 797 |
def foo(y): |
| 798 |
return y + 3 |
| 799 |
""" |
| 800 |
) |
| 801 |
self.shell.run_code("import %s" % mod_name) |
| 802 |
self.shell.run_code("pass") |
| 803 |
self.write_file( |
| 804 |
mod_fn, |
| 805 |
""" |
| 806 |
x = 9 |
| 807 |
def foo(y): |
| 808 |
return y + 5 |
| 809 |
""", |
| 810 |
) |
| 811 |
self.shell.run_code("pass") |
| 812 |
self.assertIn(mod_name, self.shell.user_ns) |
| 813 |
mod = sys.modules[mod_name] |
| 814 |
assert mod.foo(0) == 5 |
| 815 |
|
| 816 |
def test_deduperreloader_basic2(self): |
| 817 |
self.shell.magic_autoreload("2") |
| 818 |
mod_name, mod_fn = self.new_module( |
| 819 |
""" |
| 820 |
x = 9 |
| 821 |
def foo(y): |
| 822 |
return y + 3 |
| 823 |
""" |
| 824 |
) |
| 825 |
self.shell.run_code("import %s" % mod_name) |
| 826 |
self.shell.run_code("pass") |
| 827 |
self.write_file( |
| 828 |
mod_fn, |
| 829 |
""" |
| 830 |
x = 9 |
| 831 |
def foo(y): |
| 832 |
return y + 5 |
| 833 |
""", |
| 834 |
) |
| 835 |
self.shell.run_code("pass") |
| 836 |
self.assertIn(mod_name, self.shell.user_ns) |
| 837 |
mod = sys.modules[mod_name] |
| 838 |
assert mod.foo(0) == 5 |
| 839 |
|
| 840 |
def test_deduperreloader_basic3(self): |
| 841 |
mod_name, mod_fn = self.new_module( |
| 842 |
""" |
| 843 |
x = 9 |
| 844 |
def foo(y): |
| 845 |
return y + 3 |
| 846 |
""" |
| 847 |
) |
| 848 |
self.shell.run_code("import %s" % mod_name) |
| 849 |
self.shell.magic_autoreload("2") |
| 850 |
self.shell.run_code("pass") |
| 851 |
self.write_file( |
| 852 |
mod_fn, |
| 853 |
""" |
| 854 |
x = 9 |
| 855 |
def foo(y): |
| 856 |
return y + 5 |
| 857 |
""", |
| 858 |
) |
| 859 |
self.shell.run_code("pass") |
| 860 |
self.assertIn(mod_name, self.shell.user_ns) |
| 861 |
mod = sys.modules[mod_name] |
| 862 |
assert mod.foo(0) == 5 |
| 863 |
|
| 864 |
def test_deduperreloader_basic4(self): |
| 865 |
mod_name, mod_fn = self.new_module( |
| 866 |
""" |
| 867 |
x = 9 |
| 868 |
def foo(y): |
| 869 |
return y + 3 |
| 870 |
""" |
| 871 |
) |
| 872 |
self.shell.run_code("import %s" % mod_name) |
| 873 |
self.shell.magic_autoreload("2") |
| 874 |
self.shell.run_code("pass") |
| 875 |
self.write_file( |
| 876 |
mod_fn, |
| 877 |
""" |
| 878 |
x = 9 |
| 879 |
def foo(y): |
| 880 |
return y + 5 |
| 881 |
""", |
| 882 |
) |
| 883 |
self.shell.run_code("pass") |
| 884 |
self.assertIn(mod_name, self.shell.user_ns) |
| 885 |
mod = sys.modules[mod_name] |
| 886 |
assert mod.foo(0) == 5 |
| 887 |
|
| 888 |
def test_deduperreloader_basic5(self): |
| 889 |
mod_name, mod_fn = self.new_module( |
| 890 |
""" |
| 891 |
x = 9 |
| 892 |
def foo(y): |
| 893 |
return y + 3 |
| 894 |
""" |
| 895 |
) |
| 896 |
self.shell.run_code("import %s" % mod_name) |
| 897 |
self.shell.run_code("pass") |
| 898 |
self.shell.magic_autoreload("2") |
| 899 |
self.shell.run_code("pass") |
| 900 |
self.write_file( |
| 901 |
mod_fn, |
| 902 |
""" |
| 903 |
x = 9 |
| 904 |
def foo(y): |
| 905 |
return y + 5 |
| 906 |
""", |
| 907 |
) |
| 908 |
self.shell.run_code("pass") |
| 909 |
self.assertIn(mod_name, self.shell.user_ns) |
| 910 |
mod = sys.modules[mod_name] |
| 911 |
assert mod.foo(0) == 5 |
| 912 |
|
| 913 |
def test_deduperreloader_basic6(self): |
| 914 |
mod_name, mod_fn = self.new_module( |
| 915 |
""" |
| 916 |
x = 9 |
| 917 |
def foo(y): |
| 918 |
return y + 3 |
| 919 |
""" |
| 920 |
) |
| 921 |
self.shell.run_code("import %s" % mod_name) |
| 922 |
self.shell.run_code("pass") |
| 923 |
self.shell.magic_autoreload("2") |
| 924 |
self.shell.run_code("pass") |
| 925 |
self.write_file( |
| 926 |
mod_fn, |
| 927 |
""" |
| 928 |
x = 9 |
| 929 |
def foo(y): |
| 930 |
return y + 5 |
| 931 |
""", |
| 932 |
) |
| 933 |
self.shell.run_code("pass") |
| 934 |
self.assertIn(mod_name, self.shell.user_ns) |
| 935 |
mod = sys.modules[mod_name] |
| 936 |
assert mod.foo(0) == 5 |
| 937 |
|
| 938 |
def test_super(self): |
| 939 |
self.shell.magic_autoreload("2") |
| 940 |
mod_name, mod_fn = self.new_module( |
| 941 |
""" |
| 942 |
class Foo: |
| 943 |
def foo(self): |
| 944 |
return 1 |
| 945 |
class Bar(Foo): |
| 946 |
def bar(self): |
| 947 |
return super().foo() + 1 |
| 948 |
""" |
| 949 |
) |
| 950 |
self.shell.run_code(f"from {mod_name} import Bar; bar = Bar()") |
| 951 |
self.shell.run_code("pass") |
| 952 |
self.write_file( |
| 953 |
mod_fn, |
| 954 |
""" |
| 955 |
class Foo: |
| 956 |
def foo(self): |
| 957 |
return 1 |
| 958 |
class Bar(Foo): |
| 959 |
def bar(self): |
| 960 |
return super().foo() + 2 |
| 961 |
""", |
| 962 |
) |
| 963 |
self.shell.run_code("result = bar.bar()") |
| 964 |
# NOTE : only works on CPython, requires read-only patching. |
| 965 |
if platform.python_implementation() == "CPython": |
| 966 |
assert self.shell.user_ns["result"] == 3 |
| 967 |
|
| 968 |
def test_deduperreloader_need_to_default_back(self): |
| 969 |
self.shell.magic_autoreload("2") |
| 970 |
mod_name, mod_fn = self.new_module( |
| 971 |
""" |
| 972 |
x = 9 |
| 973 |
def foo(y): |
| 974 |
return y + 3 |
| 975 |
""" |
| 976 |
) |
| 977 |
self.shell.run_code("import %s" % mod_name) |
| 978 |
self.shell.run_code("pass") |
| 979 |
self.write_file( |
| 980 |
mod_fn, |
| 981 |
""" |
| 982 |
x = 200 |
| 983 |
def foo(y): |
| 984 |
return y + 5 |
| 985 |
""", |
| 986 |
) |
| 987 |
self.shell.run_code("pass") |
| 988 |
self.assertIn(mod_name, self.shell.user_ns) |
| 989 |
mod = sys.modules[mod_name] |
| 990 |
assert mod.foo(0) == 5 |
| 991 |
|
| 992 |
def test_deduperreloader_failure(self): |
| 993 |
self.shell.magic_autoreload("2") |
| 994 |
mod_name, mod_fn = self.new_module( |
| 995 |
""" |
| 996 |
x = 9 |
| 997 |
def foo(y): |
| 998 |
return y + 3 |
| 999 |
""" |
| 1000 |
) |
| 1001 |
self.shell.run_code("import %s" % mod_name) |
| 1002 |
self.shell.run_code("pass") |
| 1003 |
self.write_file( |
| 1004 |
mod_fn, |
| 1005 |
""" |
| 1006 |
broken broken broken |
| 1007 |
""", |
| 1008 |
) |
| 1009 |
self.shell.run_code("pass") |
| 1010 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1011 |
mod = sys.modules[mod_name] |
| 1012 |
assert mod.foo(0) == 3 |
| 1013 |
|
| 1014 |
def test_deduperreloader_imported_mod(self): |
| 1015 |
self.shell.magic_autoreload("2") |
| 1016 |
mod_name, mod_fn = self.new_module( |
| 1017 |
""" |
| 1018 |
from os import environ |
| 1019 |
def foo(n): |
| 1020 |
pass |
| 1021 |
""" |
| 1022 |
) |
| 1023 |
self.shell.run_code("import %s" % mod_name) |
| 1024 |
self.shell.run_code("pass") |
| 1025 |
self.write_file( |
| 1026 |
mod_fn, |
| 1027 |
""" |
| 1028 |
from os import environ |
| 1029 |
def foo(): |
| 1030 |
environ._data |
| 1031 |
return 1 |
| 1032 |
""", |
| 1033 |
) |
| 1034 |
self.shell.run_code("pass") |
| 1035 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1036 |
mod = sys.modules[mod_name] |
| 1037 |
assert mod.foo() == 1 |
| 1038 |
|
| 1039 |
|
| 1040 |
class AutoreloadClassMethodsDetectionSuite(unittest.TestCase): |
| 1041 |
""" |
| 1042 |
Unit tests for autoreload testing logic |
| 1043 |
""" |
| 1044 |
|
| 1045 |
def test_autoreload_method(self): |
| 1046 |
code1 = squish_text( |
| 1047 |
""" |
| 1048 |
class C(n): |
| 1049 |
def foo(): |
| 1050 |
pass |
| 1051 |
""" |
| 1052 |
) |
| 1053 |
code2 = squish_text( |
| 1054 |
""" |
| 1055 |
class C(n): |
| 1056 |
def foo(): |
| 1057 |
x = 1 |
| 1058 |
return x |
| 1059 |
""" |
| 1060 |
) |
| 1061 |
deduperreloader = DeduperTestReloader() |
| 1062 |
ast_1 = ast.parse(code1) |
| 1063 |
ast_2 = ast.parse(code2) |
| 1064 |
|
| 1065 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1066 |
assert deduperreloader._to_autoreload.defs_to_reload == [] |
| 1067 |
assert "C" in deduperreloader._to_autoreload.children |
| 1068 |
assert ["foo"] == list( |
| 1069 |
d[0][0] for d in deduperreloader._to_autoreload.children["C"].defs_to_reload |
| 1070 |
) |
| 1071 |
assert deduperreloader._to_autoreload.children["C"].children == {} |
| 1072 |
|
| 1073 |
def test_autoreload_no_changes(self): |
| 1074 |
code1 = squish_text( |
| 1075 |
""" |
| 1076 |
class D(n): |
| 1077 |
def foo(): |
| 1078 |
pass |
| 1079 |
def foo(): |
| 1080 |
pass |
| 1081 |
""" |
| 1082 |
) |
| 1083 |
code2 = squish_text( |
| 1084 |
""" |
| 1085 |
class D(n): |
| 1086 |
def foo(): |
| 1087 |
pass |
| 1088 |
def foo(): |
| 1089 |
pass |
| 1090 |
""" |
| 1091 |
) |
| 1092 |
deduperreloader = DeduperTestReloader() |
| 1093 |
ast_1 = ast.parse(code1) |
| 1094 |
ast_2 = ast.parse(code2) |
| 1095 |
|
| 1096 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1097 |
assert deduperreloader._to_autoreload.defs_to_reload == [] |
| 1098 |
assert deduperreloader._to_autoreload.children == {} |
| 1099 |
|
| 1100 |
def test_autoreload_add_function(self): |
| 1101 |
code1 = squish_text( |
| 1102 |
""" |
| 1103 |
class C: |
| 1104 |
def foo(): |
| 1105 |
pass |
| 1106 |
def foo(): |
| 1107 |
pass |
| 1108 |
""" |
| 1109 |
) |
| 1110 |
code2 = squish_text( |
| 1111 |
""" |
| 1112 |
class C: |
| 1113 |
def foo(): |
| 1114 |
x = 1 |
| 1115 |
return x |
| 1116 |
def bar(): |
| 1117 |
return -1 |
| 1118 |
def foo(): |
| 1119 |
pass |
| 1120 |
""" |
| 1121 |
) |
| 1122 |
deduperreloader = DeduperTestReloader() |
| 1123 |
ast_1 = ast.parse(code1) |
| 1124 |
ast_2 = ast.parse(code2) |
| 1125 |
|
| 1126 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1127 |
assert deduperreloader._to_autoreload.defs_to_reload == [] |
| 1128 |
assert "C" in deduperreloader._to_autoreload.children |
| 1129 |
assert ["foo", "bar"] == list( |
| 1130 |
d[0][0] for d in deduperreloader._to_autoreload.children["C"].defs_to_reload |
| 1131 |
) |
| 1132 |
assert len(deduperreloader._to_autoreload.children["C"].children) == 0 |
| 1133 |
|
| 1134 |
def test_autoreload_remove_method(self): |
| 1135 |
code1 = squish_text( |
| 1136 |
""" |
| 1137 |
class C: |
| 1138 |
def foo(): |
| 1139 |
pass |
| 1140 |
def foo(): |
| 1141 |
pass |
| 1142 |
""" |
| 1143 |
) |
| 1144 |
code2 = squish_text( |
| 1145 |
""" |
| 1146 |
class C: |
| 1147 |
pass |
| 1148 |
def foo(): |
| 1149 |
pass |
| 1150 |
""" |
| 1151 |
) |
| 1152 |
deduperreloader = DeduperTestReloader() |
| 1153 |
ast_1 = ast.parse(code1) |
| 1154 |
ast_2 = ast.parse(code2) |
| 1155 |
|
| 1156 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1157 |
|
| 1158 |
def test_autoreload_remove_class(self): |
| 1159 |
code1 = squish_text( |
| 1160 |
""" |
| 1161 |
class C: |
| 1162 |
def foo(): |
| 1163 |
pass |
| 1164 |
def foo(): |
| 1165 |
pass |
| 1166 |
""" |
| 1167 |
) |
| 1168 |
code2 = squish_text( |
| 1169 |
""" |
| 1170 |
def foo(): |
| 1171 |
pass |
| 1172 |
""" |
| 1173 |
) |
| 1174 |
deduperreloader = DeduperTestReloader() |
| 1175 |
ast_1 = ast.parse(code1) |
| 1176 |
ast_2 = ast.parse(code2) |
| 1177 |
|
| 1178 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1179 |
|
| 1180 |
def test_autoreload_add_class_in_class(self): |
| 1181 |
code1 = squish_text( |
| 1182 |
""" |
| 1183 |
class C: |
| 1184 |
pass |
| 1185 |
def foo(): |
| 1186 |
pass |
| 1187 |
""" |
| 1188 |
) |
| 1189 |
code2 = squish_text( |
| 1190 |
""" |
| 1191 |
class C: |
| 1192 |
class D: |
| 1193 |
pass |
| 1194 |
def foo(): |
| 1195 |
pass |
| 1196 |
""" |
| 1197 |
) |
| 1198 |
deduperreloader = DeduperTestReloader() |
| 1199 |
ast_1 = ast.parse(code1) |
| 1200 |
ast_2 = ast.parse(code2) |
| 1201 |
|
| 1202 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1203 |
|
| 1204 |
def test_autoreload_add_method_in_class_in_class(self): |
| 1205 |
code1 = squish_text( |
| 1206 |
""" |
| 1207 |
class C: |
| 1208 |
class D: |
| 1209 |
x = 1 |
| 1210 |
def foo(): |
| 1211 |
pass |
| 1212 |
""" |
| 1213 |
) |
| 1214 |
code2 = squish_text( |
| 1215 |
""" |
| 1216 |
class C: |
| 1217 |
class D: |
| 1218 |
x = 1 |
| 1219 |
def foo(): |
| 1220 |
return 1 |
| 1221 |
def foo(): |
| 1222 |
pass |
| 1223 |
""" |
| 1224 |
) |
| 1225 |
deduperreloader = DeduperTestReloader() |
| 1226 |
ast_1 = ast.parse(code1) |
| 1227 |
ast_2 = ast.parse(code2) |
| 1228 |
|
| 1229 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1230 |
assert deduperreloader._to_autoreload.defs_to_reload == [] |
| 1231 |
assert list(deduperreloader._to_autoreload.children.keys()) == ["C"] |
| 1232 |
assert deduperreloader._to_autoreload.children["C"].defs_to_reload == [] |
| 1233 |
assert list(deduperreloader._to_autoreload.children["C"].children.keys()) == [ |
| 1234 |
"D" |
| 1235 |
] |
| 1236 |
assert list( |
| 1237 |
d[0][0] |
| 1238 |
for d in deduperreloader._to_autoreload.children["C"] |
| 1239 |
.children["D"] |
| 1240 |
.defs_to_reload |
| 1241 |
) == ["foo"] |
| 1242 |
assert deduperreloader._to_autoreload.children["C"].children["D"].children == {} |
| 1243 |
|
| 1244 |
def test_autoreload_add_var_and_method_in_class_in_class(self): |
| 1245 |
code1 = squish_text( |
| 1246 |
""" |
| 1247 |
class C: |
| 1248 |
class D: |
| 1249 |
pass |
| 1250 |
def foo(): |
| 1251 |
pass |
| 1252 |
""" |
| 1253 |
) |
| 1254 |
code2 = squish_text( |
| 1255 |
""" |
| 1256 |
class C: |
| 1257 |
class D: |
| 1258 |
x = 1 |
| 1259 |
def foo(): |
| 1260 |
return 1 |
| 1261 |
def foo(): |
| 1262 |
pass |
| 1263 |
""" |
| 1264 |
) |
| 1265 |
deduperreloader = DeduperTestReloader() |
| 1266 |
ast_1 = ast.parse(code1) |
| 1267 |
ast_2 = ast.parse(code2) |
| 1268 |
|
| 1269 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1270 |
|
| 1271 |
def test_autoreload_add_method_in_class_in_class_pass(self): |
| 1272 |
code1 = squish_text( |
| 1273 |
""" |
| 1274 |
class C: |
| 1275 |
class D: |
| 1276 |
pass |
| 1277 |
def foo(): |
| 1278 |
pass |
| 1279 |
""" |
| 1280 |
) |
| 1281 |
code2 = squish_text( |
| 1282 |
""" |
| 1283 |
class C: |
| 1284 |
class D: |
| 1285 |
def foo(): |
| 1286 |
return 1 |
| 1287 |
def foo(): |
| 1288 |
pass |
| 1289 |
""" |
| 1290 |
) |
| 1291 |
deduperreloader = DeduperTestReloader() |
| 1292 |
ast_1 = ast.parse(code1) |
| 1293 |
ast_2 = ast.parse(code2) |
| 1294 |
|
| 1295 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1296 |
assert deduperreloader._to_autoreload.defs_to_reload == [] |
| 1297 |
assert list(deduperreloader._to_autoreload.children.keys()) == ["C"] |
| 1298 |
assert deduperreloader._to_autoreload.children["C"].defs_to_reload == [] |
| 1299 |
assert list(deduperreloader._to_autoreload.children["C"].children.keys()) == [ |
| 1300 |
"D" |
| 1301 |
] |
| 1302 |
assert list( |
| 1303 |
d[0][0] |
| 1304 |
for d in deduperreloader._to_autoreload.children["C"] |
| 1305 |
.children["D"] |
| 1306 |
.defs_to_reload |
| 1307 |
) == ["foo"] |
| 1308 |
assert deduperreloader._to_autoreload.children["C"].children["D"].children == {} |
| 1309 |
|
| 1310 |
def test_autoreload_add_method_in_class_in_class_more(self): |
| 1311 |
code1 = squish_text( |
| 1312 |
""" |
| 1313 |
class C: |
| 1314 |
class D: |
| 1315 |
pass |
| 1316 |
def foo(): |
| 1317 |
pass |
| 1318 |
""" |
| 1319 |
) |
| 1320 |
code2 = squish_text( |
| 1321 |
""" |
| 1322 |
class C: |
| 1323 |
def bar(): |
| 1324 |
return -1 |
| 1325 |
class D: |
| 1326 |
def foo(): |
| 1327 |
return 1 |
| 1328 |
def foo(): |
| 1329 |
pass |
| 1330 |
""" |
| 1331 |
) |
| 1332 |
deduperreloader = DeduperTestReloader() |
| 1333 |
ast_1 = ast.parse(code1) |
| 1334 |
ast_2 = ast.parse(code2) |
| 1335 |
|
| 1336 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1337 |
assert deduperreloader._to_autoreload.defs_to_reload == [] |
| 1338 |
assert list(deduperreloader._to_autoreload.children.keys()) == ["C"] |
| 1339 |
assert list( |
| 1340 |
d[0][0] for d in deduperreloader._to_autoreload.children["C"].defs_to_reload |
| 1341 |
) == ["bar"] |
| 1342 |
assert list(deduperreloader._to_autoreload.children["C"].children.keys()) == [ |
| 1343 |
"D" |
| 1344 |
] |
| 1345 |
assert list( |
| 1346 |
d[0][0] |
| 1347 |
for d in deduperreloader._to_autoreload.children["C"] |
| 1348 |
.children["D"] |
| 1349 |
.defs_to_reload |
| 1350 |
) == ["foo"] |
| 1351 |
assert deduperreloader._to_autoreload.children["C"].children["D"].children == {} |
| 1352 |
|
| 1353 |
def test_autoreload_add_method_in_class_in_class_with_members(self): |
| 1354 |
code1 = squish_text( |
| 1355 |
""" |
| 1356 |
class C: |
| 1357 |
class D: |
| 1358 |
pass |
| 1359 |
def foo(): |
| 1360 |
pass |
| 1361 |
""" |
| 1362 |
) |
| 1363 |
code2 = squish_text( |
| 1364 |
""" |
| 1365 |
class C: |
| 1366 |
def bar(): |
| 1367 |
return -1 |
| 1368 |
class D: |
| 1369 |
s = 2 |
| 1370 |
def foo(): |
| 1371 |
return 1 |
| 1372 |
def foo(): |
| 1373 |
pass |
| 1374 |
""" |
| 1375 |
) |
| 1376 |
deduperreloader = DeduperTestReloader() |
| 1377 |
ast_1 = ast.parse(code1) |
| 1378 |
ast_2 = ast.parse(code2) |
| 1379 |
|
| 1380 |
assert deduperreloader.detect_autoreload(ast_1, ast_2) |
| 1381 |
|
| 1382 |
|
| 1383 |
class AutoreloadReliabilitySuite(ShellFixture): |
| 1384 |
def test_autoreload_class_basic(self): |
| 1385 |
self.shell.magic_autoreload("2") |
| 1386 |
mod_name, mod_fn = self.new_module( |
| 1387 |
""" |
| 1388 |
x = 9 |
| 1389 |
class C: |
| 1390 |
def foo(): |
| 1391 |
return 1 |
| 1392 |
""" |
| 1393 |
) |
| 1394 |
self.shell.run_code("import %s" % mod_name) |
| 1395 |
self.shell.run_code("pass") |
| 1396 |
self.write_file( |
| 1397 |
mod_fn, |
| 1398 |
""" |
| 1399 |
x = 9 |
| 1400 |
class C: |
| 1401 |
def foo(): |
| 1402 |
return 1 |
| 1403 |
""", |
| 1404 |
) |
| 1405 |
self.shell.run_code("pass") |
| 1406 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1407 |
mod = sys.modules[mod_name] |
| 1408 |
assert mod.C.foo() == 1 |
| 1409 |
|
| 1410 |
def test_remove_overridden_method(self): |
| 1411 |
self.shell.magic_autoreload("2") |
| 1412 |
mod_name, mod_fn = self.new_module( |
| 1413 |
""" |
| 1414 |
class A: |
| 1415 |
def foo(self): |
| 1416 |
return 1 |
| 1417 |
class B(A): |
| 1418 |
def foo(self): |
| 1419 |
return 42 |
| 1420 |
""" |
| 1421 |
) |
| 1422 |
self.shell.run_code(f"from {mod_name} import B; b = B()") |
| 1423 |
self.shell.run_code("assert b.foo() == 42") |
| 1424 |
self.write_file( |
| 1425 |
mod_fn, |
| 1426 |
""" |
| 1427 |
class A: |
| 1428 |
def foo(self): |
| 1429 |
return 1 |
| 1430 |
class B(A): |
| 1431 |
pass |
| 1432 |
""", |
| 1433 |
) |
| 1434 |
self.shell.run_code("assert b.foo() == 1") |
| 1435 |
|
| 1436 |
def test_autoreload_class_use_outside_func(self): |
| 1437 |
self.shell.magic_autoreload("2") |
| 1438 |
mod_name, mod_fn = self.new_module( |
| 1439 |
""" |
| 1440 |
x = 9 |
| 1441 |
class C: |
| 1442 |
def foo(): |
| 1443 |
return 1 |
| 1444 |
""" |
| 1445 |
) |
| 1446 |
self.shell.run_code("import %s" % mod_name) |
| 1447 |
self.shell.run_code("pass") |
| 1448 |
self.write_file( |
| 1449 |
mod_fn, |
| 1450 |
""" |
| 1451 |
x = 9 |
| 1452 |
class C: |
| 1453 |
def foo(): |
| 1454 |
return 1+x |
| 1455 |
""", |
| 1456 |
) |
| 1457 |
self.shell.run_code("pass") |
| 1458 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1459 |
mod = sys.modules[mod_name] |
| 1460 |
assert mod.C.foo() == 10 |
| 1461 |
|
| 1462 |
def test_autoreload_class_use_class_member(self): |
| 1463 |
self.shell.magic_autoreload("2") |
| 1464 |
mod_name, mod_fn = self.new_module( |
| 1465 |
""" |
| 1466 |
x = 9 |
| 1467 |
class C: |
| 1468 |
def foo(): |
| 1469 |
return 1 |
| 1470 |
""" |
| 1471 |
) |
| 1472 |
self.shell.run_code("import %s" % mod_name) |
| 1473 |
self.shell.run_code("pass") |
| 1474 |
self.write_file( |
| 1475 |
mod_fn, |
| 1476 |
""" |
| 1477 |
class C: |
| 1478 |
x = 9 |
| 1479 |
def foo(): |
| 1480 |
return 1+C.x |
| 1481 |
""", |
| 1482 |
) |
| 1483 |
self.shell.run_code("pass") |
| 1484 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1485 |
mod = sys.modules[mod_name] |
| 1486 |
assert mod.C.foo() == 10 |
| 1487 |
|
| 1488 |
def test_autoreload_class_pass(self): |
| 1489 |
self.shell.magic_autoreload("2") |
| 1490 |
mod_name, mod_fn = self.new_module( |
| 1491 |
""" |
| 1492 |
x = 9 |
| 1493 |
class C: |
| 1494 |
pass |
| 1495 |
""" |
| 1496 |
) |
| 1497 |
self.shell.run_code("import %s" % mod_name) |
| 1498 |
self.shell.run_code("pass") |
| 1499 |
self.write_file( |
| 1500 |
mod_fn, |
| 1501 |
""" |
| 1502 |
class C: |
| 1503 |
x = 9 |
| 1504 |
def foo(): |
| 1505 |
return 1+C.x |
| 1506 |
""", |
| 1507 |
) |
| 1508 |
self.shell.run_code("pass") |
| 1509 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1510 |
mod = sys.modules[mod_name] |
| 1511 |
assert mod.C.foo() == 10 |
| 1512 |
|
| 1513 |
def test_autoreload_class_ellipsis(self): |
| 1514 |
self.shell.magic_autoreload("2") |
| 1515 |
mod_name, mod_fn = self.new_module( |
| 1516 |
""" |
| 1517 |
x = 9 |
| 1518 |
class C: |
| 1519 |
... |
| 1520 |
""" |
| 1521 |
) |
| 1522 |
self.shell.run_code("import %s" % mod_name) |
| 1523 |
self.shell.run_code("pass") |
| 1524 |
self.write_file( |
| 1525 |
mod_fn, |
| 1526 |
""" |
| 1527 |
class C: |
| 1528 |
x = 9 |
| 1529 |
def foo(): |
| 1530 |
return 1+C.x |
| 1531 |
""", |
| 1532 |
) |
| 1533 |
self.shell.run_code("pass") |
| 1534 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1535 |
mod = sys.modules[mod_name] |
| 1536 |
assert mod.C.foo() == 10 |
| 1537 |
|
| 1538 |
def test_autoreload_class_default_autoreload(self): |
| 1539 |
self.shell.magic_autoreload("2") |
| 1540 |
mod_name, mod_fn = self.new_module( |
| 1541 |
""" |
| 1542 |
class C: |
| 1543 |
x = 9 |
| 1544 |
def foo(): |
| 1545 |
return 1+C.x |
| 1546 |
""" |
| 1547 |
) |
| 1548 |
self.shell.run_code("import %s" % mod_name) |
| 1549 |
self.shell.run_code("pass") |
| 1550 |
self.write_file( |
| 1551 |
mod_fn, |
| 1552 |
""" |
| 1553 |
class C: |
| 1554 |
x = 20 |
| 1555 |
def foo(): |
| 1556 |
return 1+C.x |
| 1557 |
""", |
| 1558 |
) |
| 1559 |
self.shell.run_code("pass") |
| 1560 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1561 |
mod = sys.modules[mod_name] |
| 1562 |
assert mod.C.foo() == 21 |
| 1563 |
|
| 1564 |
def test_autoreload_class_nested(self): |
| 1565 |
self.shell.magic_autoreload("2") |
| 1566 |
mod_name, mod_fn = self.new_module( |
| 1567 |
""" |
| 1568 |
class C: |
| 1569 |
x = 9 |
| 1570 |
class D: |
| 1571 |
def foo(): |
| 1572 |
pass |
| 1573 |
""" |
| 1574 |
) |
| 1575 |
self.shell.run_code("import %s" % mod_name) |
| 1576 |
self.shell.run_code("pass") |
| 1577 |
self.write_file( |
| 1578 |
mod_fn, |
| 1579 |
""" |
| 1580 |
class C: |
| 1581 |
x = 9 |
| 1582 |
class D: |
| 1583 |
def foo(): |
| 1584 |
return 10 |
| 1585 |
""", |
| 1586 |
) |
| 1587 |
self.shell.run_code("pass") |
| 1588 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1589 |
mod = sys.modules[mod_name] |
| 1590 |
assert mod.C.D.foo() == 10 |
| 1591 |
|
| 1592 |
def test_autoreload_class_nested2(self): |
| 1593 |
self.shell.magic_autoreload("2") |
| 1594 |
mod_name, mod_fn = self.new_module( |
| 1595 |
""" |
| 1596 |
class C: |
| 1597 |
x = 9 |
| 1598 |
def c(): |
| 1599 |
return 1 |
| 1600 |
class D: |
| 1601 |
def foo(): |
| 1602 |
pass |
| 1603 |
""" |
| 1604 |
) |
| 1605 |
self.shell.run_code("import %s" % mod_name) |
| 1606 |
self.shell.run_code("pass") |
| 1607 |
self.write_file( |
| 1608 |
mod_fn, |
| 1609 |
""" |
| 1610 |
class C: |
| 1611 |
x = 9 |
| 1612 |
def c(): |
| 1613 |
return 1 |
| 1614 |
class D: |
| 1615 |
def foo(): |
| 1616 |
return 10 |
| 1617 |
""", |
| 1618 |
) |
| 1619 |
self.shell.run_code("pass") |
| 1620 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1621 |
mod = sys.modules[mod_name] |
| 1622 |
assert mod.C.D.foo() == 10 |
| 1623 |
assert mod.C.c() == 1 |
| 1624 |
|
| 1625 |
def test_autoreload_class_nested3(self): |
| 1626 |
self.shell.magic_autoreload("2") |
| 1627 |
mod_name, mod_fn = self.new_module( |
| 1628 |
""" |
| 1629 |
class C: |
| 1630 |
x = 9 |
| 1631 |
def c(): |
| 1632 |
return 1 |
| 1633 |
class D: |
| 1634 |
def foo(): |
| 1635 |
pass |
| 1636 |
""" |
| 1637 |
) |
| 1638 |
self.shell.run_code("import %s" % mod_name) |
| 1639 |
self.shell.run_code("pass") |
| 1640 |
self.write_file( |
| 1641 |
mod_fn, |
| 1642 |
""" |
| 1643 |
class C: |
| 1644 |
x = 9 |
| 1645 |
def c(): |
| 1646 |
return 13 |
| 1647 |
class D: |
| 1648 |
def foo(): |
| 1649 |
return 10 |
| 1650 |
""", |
| 1651 |
) |
| 1652 |
self.shell.run_code("pass") |
| 1653 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1654 |
mod = sys.modules[mod_name] |
| 1655 |
assert mod.C.D.foo() == 10 |
| 1656 |
assert mod.C.c() == 13 |
| 1657 |
|
| 1658 |
def test_autoreload_new_class_added(self): |
| 1659 |
self.shell.magic_autoreload("2") |
| 1660 |
mod_name, mod_fn = self.new_module( |
| 1661 |
""" |
| 1662 |
class C: |
| 1663 |
x = 9 |
| 1664 |
def c(): |
| 1665 |
return 1 |
| 1666 |
""" |
| 1667 |
) |
| 1668 |
self.shell.run_code("import %s" % mod_name) |
| 1669 |
self.shell.run_code("pass") |
| 1670 |
self.write_file( |
| 1671 |
mod_fn, |
| 1672 |
""" |
| 1673 |
class C: |
| 1674 |
x = 9 |
| 1675 |
def c(): |
| 1676 |
return 13 |
| 1677 |
class D: |
| 1678 |
def c(): |
| 1679 |
return 1 |
| 1680 |
""", |
| 1681 |
) |
| 1682 |
self.shell.run_code("pass") |
| 1683 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1684 |
mod = sys.modules[mod_name] |
| 1685 |
assert mod.C.c() == 13 |
| 1686 |
assert mod.D.c() == 1 |
| 1687 |
|
| 1688 |
def test_autoreload_class_nested_default(self): |
| 1689 |
self.shell.magic_autoreload("2") |
| 1690 |
mod_name, mod_fn = self.new_module( |
| 1691 |
""" |
| 1692 |
class C: |
| 1693 |
pass |
| 1694 |
""" |
| 1695 |
) |
| 1696 |
self.shell.run_code("import %s" % mod_name) |
| 1697 |
self.shell.run_code("pass") |
| 1698 |
self.write_file( |
| 1699 |
mod_fn, |
| 1700 |
""" |
| 1701 |
class C: |
| 1702 |
x = 9 |
| 1703 |
def c(): |
| 1704 |
return 13 |
| 1705 |
class D: |
| 1706 |
def foo(): |
| 1707 |
return 10 |
| 1708 |
""", |
| 1709 |
) |
| 1710 |
self.shell.run_code("pass") |
| 1711 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1712 |
mod = sys.modules[mod_name] |
| 1713 |
assert mod.C.D.foo() == 10 |
| 1714 |
assert mod.C.c() == 13 |
| 1715 |
|
| 1716 |
def test_autoreload_class_nested_using_members(self): |
| 1717 |
self.shell.magic_autoreload("2") |
| 1718 |
mod_name, mod_fn = self.new_module( |
| 1719 |
""" |
| 1720 |
class C: |
| 1721 |
x = 9 |
| 1722 |
def c(): |
| 1723 |
return 13 |
| 1724 |
class D: |
| 1725 |
def foo(): |
| 1726 |
return 10 |
| 1727 |
""" |
| 1728 |
) |
| 1729 |
self.shell.run_code("import %s" % mod_name) |
| 1730 |
self.shell.run_code("pass") |
| 1731 |
self.write_file( |
| 1732 |
mod_fn, |
| 1733 |
""" |
| 1734 |
class C: |
| 1735 |
x = 9 |
| 1736 |
def c(): |
| 1737 |
return 13 |
| 1738 |
class D: |
| 1739 |
def foo(): |
| 1740 |
return 10 + C.x |
| 1741 |
""", |
| 1742 |
) |
| 1743 |
self.shell.run_code("pass") |
| 1744 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1745 |
mod = sys.modules[mod_name] |
| 1746 |
assert mod.C.D.foo() == 19 |
| 1747 |
assert mod.C.c() == 13 |
| 1748 |
|
| 1749 |
def test_autoreload_class_nested_using_members_ellipsis(self): |
| 1750 |
self.shell.magic_autoreload("2") |
| 1751 |
mod_name, mod_fn = self.new_module( |
| 1752 |
""" |
| 1753 |
class C: |
| 1754 |
x = 9 |
| 1755 |
def c(): |
| 1756 |
return 13 |
| 1757 |
class D: |
| 1758 |
def foo(): |
| 1759 |
... |
| 1760 |
""" |
| 1761 |
) |
| 1762 |
self.shell.run_code("import %s" % mod_name) |
| 1763 |
self.shell.run_code("pass") |
| 1764 |
self.write_file( |
| 1765 |
mod_fn, |
| 1766 |
""" |
| 1767 |
class C: |
| 1768 |
x = 9 |
| 1769 |
def c(): |
| 1770 |
return 13 |
| 1771 |
class D: |
| 1772 |
def foo(): |
| 1773 |
return 10 + C.x |
| 1774 |
""", |
| 1775 |
) |
| 1776 |
self.shell.run_code("pass") |
| 1777 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1778 |
mod = sys.modules[mod_name] |
| 1779 |
assert mod.C.D.foo() == 19 |
| 1780 |
assert mod.C.c() == 13 |
| 1781 |
|
| 1782 |
def test_method_decorators_no_changes(self): |
| 1783 |
self.shell.magic_autoreload("2") |
| 1784 |
mod_name, mod_file = self.new_module( |
| 1785 |
""" |
| 1786 |
class Foo: |
| 1787 |
@classmethod |
| 1788 |
def bar(cls): |
| 1789 |
return 0 |
| 1790 |
|
| 1791 |
@classmethod |
| 1792 |
def foo(cls): |
| 1793 |
return 42 + cls.bar() |
| 1794 |
|
| 1795 |
foo = Foo.foo |
| 1796 |
""" |
| 1797 |
) |
| 1798 |
self.shell.run_code("import %s" % mod_name) |
| 1799 |
self.shell.run_code("pass") |
| 1800 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1801 |
mod = sys.modules[mod_name] |
| 1802 |
assert mod.foo() == 42 |
| 1803 |
|
| 1804 |
self.shell.run_code(f"assert {mod_name}.foo() == 42") |
| 1805 |
self.write_file( |
| 1806 |
mod_file, |
| 1807 |
""" |
| 1808 |
class Foo: |
| 1809 |
@classmethod |
| 1810 |
def bar(cls): |
| 1811 |
return 0 |
| 1812 |
|
| 1813 |
@classmethod |
| 1814 |
def foo(cls): |
| 1815 |
return 42 + cls.bar() |
| 1816 |
|
| 1817 |
foo = Foo.foo |
| 1818 |
""", |
| 1819 |
) |
| 1820 |
self.shell.run_code(f"assert {mod_name}.foo() == 42") |
| 1821 |
|
| 1822 |
def test_method_decorators_no_changes1(self): |
| 1823 |
self.shell.magic_autoreload("2") |
| 1824 |
mod_name, mod_file = self.new_module( |
| 1825 |
""" |
| 1826 |
class Foo: |
| 1827 |
@classmethod |
| 1828 |
def bar(cls): |
| 1829 |
return 0 |
| 1830 |
|
| 1831 |
@classmethod |
| 1832 |
def foo(cls): |
| 1833 |
return 42 + cls.bar() |
| 1834 |
|
| 1835 |
foo = Foo.foo |
| 1836 |
""" |
| 1837 |
) |
| 1838 |
|
| 1839 |
self.shell.run_code(f"from {mod_name} import foo") |
| 1840 |
self.shell.run_code("assert foo() == 42") |
| 1841 |
self.write_file( |
| 1842 |
mod_file, |
| 1843 |
""" |
| 1844 |
class Foo: |
| 1845 |
@classmethod |
| 1846 |
def bar(cls): |
| 1847 |
return 0 |
| 1848 |
|
| 1849 |
@classmethod |
| 1850 |
def foo(cls): |
| 1851 |
return 42 + cls.bar() |
| 1852 |
|
| 1853 |
foo = Foo.foo |
| 1854 |
""", |
| 1855 |
) |
| 1856 |
self.shell.run_code("assert foo() == 42") |
| 1857 |
|
| 1858 |
def test_method_classmethod_one_change(self): |
| 1859 |
self.shell.magic_autoreload("2") |
| 1860 |
mod_name, mod_file = self.new_module( |
| 1861 |
""" |
| 1862 |
class Foo: |
| 1863 |
@classmethod |
| 1864 |
def bar(cls): |
| 1865 |
return 0 |
| 1866 |
|
| 1867 |
@classmethod |
| 1868 |
def func(cls): |
| 1869 |
return 42 + cls.bar() |
| 1870 |
|
| 1871 |
func = Foo.func |
| 1872 |
""" |
| 1873 |
) |
| 1874 |
self.shell.run_code("import %s" % mod_name) |
| 1875 |
self.shell.run_code(f"assert {mod_name}.func() == 42") |
| 1876 |
self.write_file( |
| 1877 |
mod_file, |
| 1878 |
""" |
| 1879 |
class Foo: |
| 1880 |
@classmethod |
| 1881 |
def bar(cls): |
| 1882 |
return 1 |
| 1883 |
|
| 1884 |
@classmethod |
| 1885 |
def func(cls): |
| 1886 |
return 42 + cls.bar() |
| 1887 |
|
| 1888 |
func = Foo.func |
| 1889 |
""", |
| 1890 |
) |
| 1891 |
mod = sys.modules[mod_name] |
| 1892 |
self.shell.run_code(f"assert {mod_name}.func() == 43") |
| 1893 |
|
| 1894 |
def test_method_staticmethod_one_change(self): |
| 1895 |
self.shell.magic_autoreload("2") |
| 1896 |
mod_name, mod_file = self.new_module( |
| 1897 |
""" |
| 1898 |
class Foo: |
| 1899 |
@staticmethod |
| 1900 |
def bar(): |
| 1901 |
return 0 |
| 1902 |
|
| 1903 |
@staticmethod |
| 1904 |
def func(): |
| 1905 |
return 42 + Foo.bar() |
| 1906 |
|
| 1907 |
func = Foo.func |
| 1908 |
""" |
| 1909 |
) |
| 1910 |
self.shell.run_code(f"from {mod_name} import func") |
| 1911 |
self.shell.run_code("assert func() == 42") |
| 1912 |
self.write_file( |
| 1913 |
mod_file, |
| 1914 |
""" |
| 1915 |
class Foo: |
| 1916 |
@staticmethod |
| 1917 |
def bar(): |
| 1918 |
return 1 |
| 1919 |
|
| 1920 |
@staticmethod |
| 1921 |
def func(): |
| 1922 |
return 42 + Foo.bar() |
| 1923 |
|
| 1924 |
func = Foo.func |
| 1925 |
""", |
| 1926 |
) |
| 1927 |
self.shell.run_code("assert func() == 43") |
| 1928 |
|
| 1929 |
def test_autoreload_class_default_args(self): |
| 1930 |
self.shell.magic_autoreload("2") |
| 1931 |
mod_name, mod_fn = self.new_module( |
| 1932 |
""" |
| 1933 |
x = 42 |
| 1934 |
class Foo: |
| 1935 |
def foo(self, y): return y |
| 1936 |
""" |
| 1937 |
) |
| 1938 |
self.shell.run_code("import %s" % mod_name) |
| 1939 |
self.shell.run_code("pass") |
| 1940 |
self.write_file( |
| 1941 |
mod_fn, |
| 1942 |
""" |
| 1943 |
x = 42 |
| 1944 |
class Foo: |
| 1945 |
def foo(self, y=x): return y |
| 1946 |
""", |
| 1947 |
) |
| 1948 |
self.shell.run_code("pass") |
| 1949 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1950 |
mod = sys.modules[mod_name] |
| 1951 |
obj = mod.Foo() |
| 1952 |
assert obj.foo(2) == 2 |
| 1953 |
assert obj.foo() == 42 |
| 1954 |
|
| 1955 |
def test_autoreload_class_change_default_args(self): |
| 1956 |
self.shell.magic_autoreload("2") |
| 1957 |
mod_name, mod_fn = self.new_module( |
| 1958 |
""" |
| 1959 |
x = 42 |
| 1960 |
class Foo: |
| 1961 |
def foo(y): return y |
| 1962 |
""" |
| 1963 |
) |
| 1964 |
self.shell.run_code("import %s" % mod_name) |
| 1965 |
self.shell.run_code("pass") |
| 1966 |
self.write_file( |
| 1967 |
mod_fn, |
| 1968 |
""" |
| 1969 |
x = 44 |
| 1970 |
class Foo: |
| 1971 |
def foo(y=x): return y |
| 1972 |
""", |
| 1973 |
) |
| 1974 |
self.shell.run_code("pass") |
| 1975 |
self.assertIn(mod_name, self.shell.user_ns) |
| 1976 |
mod = sys.modules[mod_name] |
| 1977 |
assert mod.Foo.foo() == 44 |
| 1978 |
|
| 1979 |
def test_autoreload_class_new_class(self): |
| 1980 |
self.shell.magic_autoreload("2") |
| 1981 |
mod_name, mod_fn = self.new_module( |
| 1982 |
""" |
| 1983 |
x = 42 |
| 1984 |
class Foo: |
| 1985 |
def foo(y=x): return y |
| 1986 |
""" |
| 1987 |
) |
| 1988 |
self.shell.run_code("import %s" % mod_name) |
| 1989 |
self.shell.run_code("pass") |
| 1990 |
prev_foo = self.shell.user_ns[mod_name].Foo.foo |
| 1991 |
self.write_file( |
| 1992 |
mod_fn, |
| 1993 |
""" |
| 1994 |
x = 42 |
| 1995 |
class Foo: |
| 1996 |
def foo(y=x): return y |
| 1997 |
class C: |
| 1998 |
def foo(): return 200 |
| 1999 |
""", |
| 2000 |
) |
| 2001 |
self.shell.run_code("pass") |
| 2002 |
self.assertIn(mod_name, self.shell.user_ns) |
| 2003 |
self.assertIs(prev_foo, self.shell.user_ns[mod_name].Foo.foo) |
| 2004 |
mod = sys.modules[mod_name] |
| 2005 |
assert mod.Foo.foo() == 42 |
| 2006 |
assert mod.C.foo() == 200 |
| 2007 |
|
| 2008 |
def test_autoreload_overloaded_vars(self): |
| 2009 |
self.shell.magic_autoreload("2") |
| 2010 |
mod_name, mod_fn = self.new_module( |
| 2011 |
""" |
| 2012 |
x = 42 |
| 2013 |
class Foo: |
| 2014 |
pass |
| 2015 |
""" |
| 2016 |
) |
| 2017 |
self.shell.run_code("import %s" % mod_name) |
| 2018 |
self.shell.run_code("pass") |
| 2019 |
mod = sys.modules[mod_name] |
| 2020 |
self.write_file( |
| 2021 |
mod_fn, |
| 2022 |
""" |
| 2023 |
x = 44 |
| 2024 |
class Foo: |
| 2025 |
def foo(): |
| 2026 |
x = 2 |
| 2027 |
return x |
| 2028 |
""", |
| 2029 |
) |
| 2030 |
self.shell.run_code("pass") |
| 2031 |
self.assertIn(mod_name, self.shell.user_ns) |
| 2032 |
assert mod.Foo.foo() == 2 |
| 2033 |
|
| 2034 |
def test_autoreload_overloaded_vars2(self): |
| 2035 |
self.shell.magic_autoreload("2") |
| 2036 |
mod_name, mod_fn = self.new_module( |
| 2037 |
""" |
| 2038 |
x = 42 |
| 2039 |
def foo(): |
| 2040 |
return x |
| 2041 |
""" |
| 2042 |
) |
| 2043 |
self.shell.run_code("import %s" % mod_name) |
| 2044 |
self.shell.run_code("pass") |
| 2045 |
mod = sys.modules[mod_name] |
| 2046 |
self.write_file( |
| 2047 |
mod_fn, |
| 2048 |
""" |
| 2049 |
x = 44 |
| 2050 |
def foo(): |
| 2051 |
x = 2 |
| 2052 |
return x |
| 2053 |
""", |
| 2054 |
) |
| 2055 |
self.shell.run_code("pass") |
| 2056 |
self.assertIn(mod_name, self.shell.user_ns) |
| 2057 |
assert mod.foo() == 2 |
| 2058 |
|
| 2059 |
|
| 2060 |
class DecoratorPatchingSuite(ShellFixture): |
| 2061 |
""" |
| 2062 |
Unit tests for autoreload patching logic |
| 2063 |
""" |
| 2064 |
|
| 2065 |
def test_modify_property(self): |
| 2066 |
self.shell.magic_autoreload("2") |
| 2067 |
mod_name, mod_file = self.new_module( |
| 2068 |
""" |
| 2069 |
class Foo: |
| 2070 |
@property |
| 2071 |
def foo(self): |
| 2072 |
return 42 |
| 2073 |
""" |
| 2074 |
) |
| 2075 |
self.shell.run_code(f"from {mod_name} import Foo") |
| 2076 |
self.shell.run_code("foo = Foo()") |
| 2077 |
self.shell.run_code("assert foo.foo == 42") |
| 2078 |
self.write_file( |
| 2079 |
mod_file, |
| 2080 |
""" |
| 2081 |
class Foo: |
| 2082 |
@property |
| 2083 |
def foo(self): |
| 2084 |
return 43 |
| 2085 |
""", |
| 2086 |
) |
| 2087 |
self.shell.run_code("pass") |
| 2088 |
self.shell.run_code("assert foo.foo == 43") |
| 2089 |
|
| 2090 |
def test_method_decorator(self): |
| 2091 |
self.shell.magic_autoreload("2") |
| 2092 |
mod_name, mod_file = self.new_module( |
| 2093 |
""" |
| 2094 |
def incremented(f): |
| 2095 |
return lambda *args: f(*args) + 1 |
| 2096 |
|
| 2097 |
class Foo: |
| 2098 |
@classmethod |
| 2099 |
@incremented |
| 2100 |
def foo(cls): |
| 2101 |
return 42 |
| 2102 |
|
| 2103 |
foo = Foo.foo |
| 2104 |
""" |
| 2105 |
) |
| 2106 |
self.shell.run_code(f"from {mod_name} import foo") |
| 2107 |
self.shell.run_code("assert foo() == 43") |
| 2108 |
self.write_file( |
| 2109 |
mod_file, |
| 2110 |
""" |
| 2111 |
def incremented(f): |
| 2112 |
return lambda *args: f(*args) + 1 |
| 2113 |
|
| 2114 |
class Foo: |
| 2115 |
@classmethod |
| 2116 |
def foo(cls): |
| 2117 |
return 42 |
| 2118 |
|
| 2119 |
foo = Foo.foo |
| 2120 |
""", |
| 2121 |
) |
| 2122 |
self.shell.run_code("assert foo() == 42") |
| 2123 |
|
| 2124 |
def test_method_modified_decorator(self): |
| 2125 |
self.shell.magic_autoreload("2") |
| 2126 |
mod_name, mod_file = self.new_module( |
| 2127 |
""" |
| 2128 |
def incremented(f): |
| 2129 |
return lambda *args: f(*args) + 1 |
| 2130 |
|
| 2131 |
class Foo: |
| 2132 |
@classmethod |
| 2133 |
@incremented |
| 2134 |
def foo(cls): |
| 2135 |
return 42 |
| 2136 |
|
| 2137 |
foo = Foo.foo |
| 2138 |
""" |
| 2139 |
) |
| 2140 |
self.shell.run_code(f"from {mod_name} import foo") |
| 2141 |
self.shell.run_code("assert foo() == 43") |
| 2142 |
self.write_file( |
| 2143 |
mod_file, |
| 2144 |
""" |
| 2145 |
def incremented(f): |
| 2146 |
return lambda *args: f(*args) + 0 |
| 2147 |
|
| 2148 |
class Foo: |
| 2149 |
@classmethod |
| 2150 |
@incremented |
| 2151 |
def foo(cls): |
| 2152 |
return 42 |
| 2153 |
|
| 2154 |
foo = Foo.foo |
| 2155 |
""", |
| 2156 |
) |
| 2157 |
self.shell.run_code("assert foo() == 42") |
| 2158 |
|
| 2159 |
def test_function_decorators(self): |
| 2160 |
self.shell.magic_autoreload("2") |
| 2161 |
mod_name, mod_file = self.new_module( |
| 2162 |
""" |
| 2163 |
def incremented(f): |
| 2164 |
return lambda *args: f(*args) + 1 |
| 2165 |
|
| 2166 |
@incremented |
| 2167 |
def foo(): |
| 2168 |
return 42 |
| 2169 |
""" |
| 2170 |
) |
| 2171 |
self.shell.run_code("import %s" % mod_name) |
| 2172 |
self.shell.run_code("pass") |
| 2173 |
mod = sys.modules[mod_name] |
| 2174 |
assert mod.foo() == 43 |
| 2175 |
self.write_file( |
| 2176 |
mod_file, |
| 2177 |
""" |
| 2178 |
def incremented(f): |
| 2179 |
return lambda *args: f(*args) + 1 |
| 2180 |
|
| 2181 |
def foo(): |
| 2182 |
return 42 |
| 2183 |
""", |
| 2184 |
) |
| 2185 |
self.shell.run_code("pass") |
| 2186 |
assert mod.foo() == 42 |
| 2187 |
self.write_file( |
| 2188 |
mod_file, |
| 2189 |
""" |
| 2190 |
def incremented(v): |
| 2191 |
def deco(f): |
| 2192 |
return lambda *args: f(*args) + v |
| 2193 |
return deco |
| 2194 |
|
| 2195 |
@incremented(2) |
| 2196 |
def foo(): |
| 2197 |
return 43 |
| 2198 |
""", |
| 2199 |
) |
| 2200 |
self.shell.run_code("pass") |
| 2201 |
assert mod.foo() == 45 |
| 2202 |
|
| 2203 |
def test_method_decorators_again(self): |
| 2204 |
self.shell.magic_autoreload("2") |
| 2205 |
mod_name, mod_file = self.new_module( |
| 2206 |
""" |
| 2207 |
class Foo: |
| 2208 |
@classmethod |
| 2209 |
def bar(cls): |
| 2210 |
return 0 |
| 2211 |
|
| 2212 |
@classmethod |
| 2213 |
def foo(cls): |
| 2214 |
return 42 + cls.bar() |
| 2215 |
|
| 2216 |
foo = Foo.foo |
| 2217 |
""" |
| 2218 |
) |
| 2219 |
self.shell.run_code("import %s" % mod_name) |
| 2220 |
self.shell.run_code("pass") |
| 2221 |
mod = sys.modules[mod_name] |
| 2222 |
assert mod.foo() == 42 |
| 2223 |
self.write_file( |
| 2224 |
mod_file, |
| 2225 |
""" |
| 2226 |
class Foo: |
| 2227 |
@classmethod |
| 2228 |
def bar(cls): |
| 2229 |
return 1 |
| 2230 |
|
| 2231 |
@classmethod |
| 2232 |
def foo(cls): |
| 2233 |
return 42 + cls.bar() |
| 2234 |
|
| 2235 |
foo = Foo.foo |
| 2236 |
""", |
| 2237 |
) |
| 2238 |
self.shell.run_code("pass") |
| 2239 |
assert mod.Foo.foo() == 43 |
| 2240 |
assert mod.foo() == 43 |
| 2241 |
|
| 2242 |
# This test verifies that the correct globals are used when patching a function |
| 2243 |
# decorated by a function from another module. For this example, we should always |
| 2244 |
# use <mod_name>.__dict__ as the global environment when patching foo, which comes |
| 2245 |
# from <mod_name>, and never <other_mod_name>.__dict__, which is what we would get |
| 2246 |
# if we use foo.__globals__ after it has been decorated. |
| 2247 |
def test_function_decorator_from_other_module(self): |
| 2248 |
self.shell.magic_autoreload("2") |
| 2249 |
other_mod_name, _ = self.new_module( |
| 2250 |
""" |
| 2251 |
import functools |
| 2252 |
|
| 2253 |
def incremented(f): |
| 2254 |
@functools.wraps(f) |
| 2255 |
def wrapper(*args, **kwargs): |
| 2256 |
return f(*args, **kwargs) + 1 |
| 2257 |
return wrapper |
| 2258 |
""" |
| 2259 |
) |
| 2260 |
mod_name, mod_file = self.new_module( |
| 2261 |
f""" |
| 2262 |
from {other_mod_name} import incremented as deco |
| 2263 |
@deco |
| 2264 |
def foo(): |
| 2265 |
return 42 |
| 2266 |
""" |
| 2267 |
) |
| 2268 |
self.shell.run_code(f"from {mod_name} import foo") |
| 2269 |
self.shell.run_code("assert foo() == 43") |
| 2270 |
self.write_file( |
| 2271 |
mod_file, |
| 2272 |
f""" |
| 2273 |
from {other_mod_name} import incremented as deco |
| 2274 |
@deco |
| 2275 |
def foo(): |
| 2276 |
return 43 |
| 2277 |
""", |
| 2278 |
) |
| 2279 |
self.shell.run_code("assert foo() == 44") |
| 2280 |
|
| 2281 |
def test_decorators_with_freevars(self): |
| 2282 |
self.shell.magic_autoreload("2") |
| 2283 |
mod_name, mod_file = self.new_module( |
| 2284 |
""" |
| 2285 |
def decorate(func): |
| 2286 |
free_var_x = "x" |
| 2287 |
free_var_y = "y" |
| 2288 |
def wrapper(*args, **kwargs): |
| 2289 |
return func(*args, **kwargs), free_var_x, free_var_y |
| 2290 |
return wrapper |
| 2291 |
@decorate |
| 2292 |
def f(): |
| 2293 |
return "v1" |
| 2294 |
""" |
| 2295 |
) |
| 2296 |
self.shell.run_code(f"from {mod_name} import f") |
| 2297 |
mod = sys.modules[mod_name] |
| 2298 |
assert mod.f() == ("v1", "x", "y") |
| 2299 |
self.write_file( |
| 2300 |
mod_file, |
| 2301 |
""" |
| 2302 |
def decorate(func): |
| 2303 |
free_var_ax = "ax" |
| 2304 |
free_var_by = "by" |
| 2305 |
def wrapper(*args, **kwargs): |
| 2306 |
return func(*args, **kwargs), free_var_ax, free_var_by |
| 2307 |
return wrapper |
| 2308 |
@decorate |
| 2309 |
def f(): |
| 2310 |
return "v2" |
| 2311 |
""", |
| 2312 |
) |
| 2313 |
self.shell.run_code("pass") |
| 2314 |
val = mod.f() |
| 2315 |
assert val == ("v2", "ax", "by"), val |
| 2316 |
|
| 2317 |
|
| 2318 |
class TestAutoreloadEnum(ShellFixture): |
| 2319 |
def test_reload_enums(self): |
| 2320 |
self.shell.magic_autoreload("2") |
| 2321 |
mod_name, mod_fn = self.new_module( |
| 2322 |
""" |
| 2323 |
from enum import Enum |
| 2324 |
class MyEnum(Enum): |
| 2325 |
A = 'A' |
| 2326 |
B = 'B' |
| 2327 |
""", |
| 2328 |
) |
| 2329 |
self.shell.run_code("import %s" % mod_name) |
| 2330 |
self.shell.run_code("pass") |
| 2331 |
mod = sys.modules[mod_name] |
| 2332 |
self.write_file( |
| 2333 |
mod_fn, |
| 2334 |
""" |
| 2335 |
from enum import Enum |
| 2336 |
class MyEnum(Enum): |
| 2337 |
A = 'A' |
| 2338 |
B = 'B' |
| 2339 |
C = 'C' |
| 2340 |
""", |
| 2341 |
) |
| 2342 |
self.shell.run_code("pass") |
| 2343 |
assert mod.MyEnum.C.value == "C" |
| 2344 |
|
| 2345 |
|
| 2346 |
if __name__ == "__main__": |
| 2347 |
unittest.main() |