| 1 |
"""Tests the service layer.""" |
| 2 |
|
| 3 |
from textwrap import dedent |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from autoimport.model import common_statements |
| 8 |
from autoimport.services import fix_code |
| 9 |
|
| 10 |
|
| 11 |
def test_fix_code_adds_missing_import() -> None: |
| 12 |
"""Understands that os is a package and add it to the top of the file.""" |
| 13 |
source = "os.getcwd()" |
| 14 |
fixed_source = dedent( |
| 15 |
"""\ |
| 16 |
import os |
| 17 |
|
| 18 |
|
| 19 |
os.getcwd()""" |
| 20 |
) |
| 21 |
|
| 22 |
result = fix_code(source) |
| 23 |
|
| 24 |
assert result == fixed_source |
| 25 |
|
| 26 |
|
| 27 |
def test_fix_doesnt_change_source_if_package_doesnt_exist() -> None: |
| 28 |
"""As foo is not found, nothing is changed.""" |
| 29 |
source = "foo" |
| 30 |
|
| 31 |
result = fix_code(source) |
| 32 |
|
| 33 |
assert result == source |
| 34 |
|
| 35 |
|
| 36 |
def test_fix_imports_packages_below_docstring() -> None: |
| 37 |
"""Imports are located below the module docstrings.""" |
| 38 |
source = dedent( |
| 39 |
'''\ |
| 40 |
"""Module docstring. |
| 41 |
|
| 42 |
""" |
| 43 |
import pytest |
| 44 |
os.getcwd()''' |
| 45 |
) |
| 46 |
fixed_source = dedent( |
| 47 |
'''\ |
| 48 |
"""Module docstring. |
| 49 |
|
| 50 |
""" |
| 51 |
|
| 52 |
import os |
| 53 |
|
| 54 |
|
| 55 |
os.getcwd()''' |
| 56 |
) |
| 57 |
|
| 58 |
result = fix_code(source) |
| 59 |
|
| 60 |
assert result == fixed_source |
| 61 |
|
| 62 |
|
| 63 |
def test_fix_imports_packages_below_single_line_docstring() -> None: |
| 64 |
"""Imports are located below the module docstrings when they only take one line.""" |
| 65 |
source = dedent( |
| 66 |
'''\ |
| 67 |
"""Module docstring.""" |
| 68 |
|
| 69 |
import pytest |
| 70 |
os.getcwd()''' |
| 71 |
) |
| 72 |
fixed_source = dedent( |
| 73 |
'''\ |
| 74 |
"""Module docstring.""" |
| 75 |
|
| 76 |
import os |
| 77 |
|
| 78 |
|
| 79 |
os.getcwd()''' |
| 80 |
) |
| 81 |
|
| 82 |
result = fix_code(source) |
| 83 |
|
| 84 |
assert result == fixed_source |
| 85 |
|
| 86 |
|
| 87 |
def test_fix_imports_type_hints() -> None: |
| 88 |
"""Typing objects are initialized with their required header.""" |
| 89 |
source = dedent( |
| 90 |
"""\ |
| 91 |
def function(dictionary: Dict) -> None: |
| 92 |
pass""" |
| 93 |
) |
| 94 |
fixed_source = dedent( |
| 95 |
"""\ |
| 96 |
from typing import Dict |
| 97 |
|
| 98 |
|
| 99 |
def function(dictionary: Dict) -> None: |
| 100 |
pass""" |
| 101 |
) |
| 102 |
|
| 103 |
result = fix_code(source) |
| 104 |
|
| 105 |
assert result == fixed_source |
| 106 |
|
| 107 |
|
| 108 |
def test_fix_removes_unneeded_imports() -> None: |
| 109 |
"""If there is an import statement of an unused package it should be removed.""" |
| 110 |
source = dedent( |
| 111 |
"""\ |
| 112 |
import requests |
| 113 |
foo = 1""" |
| 114 |
) |
| 115 |
fixed_source = "foo = 1" |
| 116 |
|
| 117 |
result = fix_code(source) |
| 118 |
|
| 119 |
assert result == fixed_source |
| 120 |
|
| 121 |
|
| 122 |
def test_fix_removes_multiple_unneeded_imports() -> None: |
| 123 |
""" |
| 124 |
Given: A source code with multiple unused import statements. |
| 125 |
When: fix_code is run. |
| 126 |
Then: The unused import statements are deleted. |
| 127 |
""" |
| 128 |
source = dedent( |
| 129 |
"""\ |
| 130 |
import requests |
| 131 |
from textwrap import dedent |
| 132 |
|
| 133 |
from yaml import YAMLError |
| 134 |
foo = 1""" |
| 135 |
) |
| 136 |
fixed_source = "foo = 1" |
| 137 |
|
| 138 |
result = fix_code(source) |
| 139 |
|
| 140 |
assert result == fixed_source |
| 141 |
|
| 142 |
|
| 143 |
def test_fix_removes_unneeded_imports_in_from_statements() -> None: |
| 144 |
"""Remove `from package import` statement of an unused packages.""" |
| 145 |
source = dedent( |
| 146 |
"""\ |
| 147 |
from os import path |
| 148 |
foo = 1""" |
| 149 |
) |
| 150 |
fixed_source = "foo = 1" |
| 151 |
|
| 152 |
result = fix_code(source) |
| 153 |
|
| 154 |
assert result == fixed_source |
| 155 |
|
| 156 |
|
| 157 |
def test_fix_removes_unused_imports_in_multiline_from_statements() -> None: |
| 158 |
""" |
| 159 |
Given: A source code with multiline import from statements. |
| 160 |
When: fix_code is run |
| 161 |
Then: Unused import statements are deleted |
| 162 |
""" |
| 163 |
source = dedent( |
| 164 |
"""\ |
| 165 |
from os import ( |
| 166 |
getcwd, |
| 167 |
path, |
| 168 |
) |
| 169 |
|
| 170 |
getcwd()""" |
| 171 |
) |
| 172 |
fixed_source = dedent( |
| 173 |
"""\ |
| 174 |
from os import ( |
| 175 |
getcwd, |
| 176 |
) |
| 177 |
|
| 178 |
|
| 179 |
getcwd()""" |
| 180 |
) |
| 181 |
|
| 182 |
result = fix_code(source) |
| 183 |
|
| 184 |
assert result == fixed_source |
| 185 |
|
| 186 |
|
| 187 |
def test_fix_removes_unneeded_imports_in_beginning_of_from_statements() -> None: |
| 188 |
"""Remove unused `object_name` in `from package import object_name, other_object` |
| 189 |
statements. |
| 190 |
""" |
| 191 |
source = dedent( |
| 192 |
"""\ |
| 193 |
from os import path, getcwd |
| 194 |
|
| 195 |
getcwd()""" |
| 196 |
) |
| 197 |
fixed_source = dedent( |
| 198 |
"""\ |
| 199 |
from os import getcwd |
| 200 |
|
| 201 |
|
| 202 |
getcwd()""" |
| 203 |
) |
| 204 |
|
| 205 |
result = fix_code(source) |
| 206 |
|
| 207 |
assert result == fixed_source |
| 208 |
|
| 209 |
|
| 210 |
def test_fix_removes_unneeded_imports_in_middle_of_from_statements() -> None: |
| 211 |
"""Remove unused `object_name` in |
| 212 |
`from package import other_object, object_name, other_used_object` statements. |
| 213 |
""" |
| 214 |
source = dedent( |
| 215 |
"""\ |
| 216 |
from os import getcwd, path, mkdir |
| 217 |
|
| 218 |
getcwd() |
| 219 |
mkdir()""" |
| 220 |
) |
| 221 |
fixed_source = dedent( |
| 222 |
"""\ |
| 223 |
from os import getcwd, mkdir |
| 224 |
|
| 225 |
|
| 226 |
getcwd() |
| 227 |
mkdir()""" |
| 228 |
) |
| 229 |
|
| 230 |
result = fix_code(source) |
| 231 |
|
| 232 |
assert result == fixed_source |
| 233 |
|
| 234 |
|
| 235 |
def test_fix_removes_unneeded_imports_in_end_of_from_statements() -> None: |
| 236 |
"""Remove unused `object_name` in `from package import other_object, object_name` |
| 237 |
statements. |
| 238 |
""" |
| 239 |
source = dedent( |
| 240 |
"""\ |
| 241 |
from os import getcwd, path |
| 242 |
|
| 243 |
getcwd()""" |
| 244 |
) |
| 245 |
fixed_source = dedent( |
| 246 |
"""\ |
| 247 |
from os import getcwd |
| 248 |
|
| 249 |
|
| 250 |
getcwd()""" |
| 251 |
) |
| 252 |
|
| 253 |
result = fix_code(source) |
| 254 |
|
| 255 |
assert result == fixed_source |
| 256 |
|
| 257 |
|
| 258 |
def test_fix_respects_multiple_from_import_lines() -> None: |
| 259 |
""" |
| 260 |
Given: Multiple from X import Y lines. |
| 261 |
When: Fix code is run |
| 262 |
Then: The import statements aren't broken |
| 263 |
""" |
| 264 |
source = dedent( |
| 265 |
"""\ |
| 266 |
from os import getcwd |
| 267 |
|
| 268 |
from re import match |
| 269 |
|
| 270 |
|
| 271 |
getcwd() |
| 272 |
match(r'a', 'a')""" |
| 273 |
) |
| 274 |
|
| 275 |
result = fix_code(source) |
| 276 |
|
| 277 |
assert result == source |
| 278 |
|
| 279 |
|
| 280 |
def test_fix_respects_multiple_from_import_lines_in_multiple_lines() -> None: |
| 281 |
""" |
| 282 |
Given: Multiple from X import Y lines, some with multiline format. |
| 283 |
When: Fix code is run |
| 284 |
Then: The import statements aren't broken |
| 285 |
""" |
| 286 |
source = dedent( |
| 287 |
"""\ |
| 288 |
from os import ( |
| 289 |
getcwd, |
| 290 |
) |
| 291 |
|
| 292 |
from re import match |
| 293 |
|
| 294 |
|
| 295 |
getcwd() |
| 296 |
match(r'a', 'a')""" |
| 297 |
) |
| 298 |
|
| 299 |
result = fix_code(source) |
| 300 |
|
| 301 |
assert result == source |
| 302 |
|
| 303 |
|
| 304 |
def test_fix_respects_import_lines_in_multiple_line_strings() -> None: |
| 305 |
""" |
| 306 |
Given: Import lines in several multiline strings. |
| 307 |
When: Fix code is run. |
| 308 |
Then: The import statements inside the string are not moved to the top. |
| 309 |
""" |
| 310 |
source = dedent( |
| 311 |
"""\ |
| 312 |
from textwrap import dedent |
| 313 |
|
| 314 |
source = dedent( |
| 315 |
\"\"\"\\ |
| 316 |
from re import match |
| 317 |
|
| 318 |
match(r'a', 'a')\"\"\" |
| 319 |
) |
| 320 |
|
| 321 |
source = dedent( |
| 322 |
\"\"\"\\ |
| 323 |
from os import ( |
| 324 |
getcwd, |
| 325 |
) |
| 326 |
|
| 327 |
getcwd()\"\"\" |
| 328 |
)""" |
| 329 |
) |
| 330 |
fixed_source = dedent( |
| 331 |
"""\ |
| 332 |
from textwrap import dedent |
| 333 |
|
| 334 |
|
| 335 |
source = dedent( |
| 336 |
\"\"\"\\ |
| 337 |
from re import match |
| 338 |
|
| 339 |
match(r'a', 'a')\"\"\" |
| 340 |
) |
| 341 |
|
| 342 |
source = dedent( |
| 343 |
\"\"\"\\ |
| 344 |
from os import ( |
| 345 |
getcwd, |
| 346 |
) |
| 347 |
|
| 348 |
getcwd()\"\"\" |
| 349 |
)""" |
| 350 |
) |
| 351 |
|
| 352 |
result = fix_code(source) |
| 353 |
|
| 354 |
assert result == fixed_source |
| 355 |
|
| 356 |
|
| 357 |
def test_fix_moves_import_statements_to_the_top() -> None: |
| 358 |
"""Move import statements present in the source code to the top of the file""" |
| 359 |
source = dedent( |
| 360 |
"""\ |
| 361 |
a = 3 |
| 362 |
|
| 363 |
import os |
| 364 |
os.getcwd()""" |
| 365 |
) |
| 366 |
fixed_source = dedent( |
| 367 |
"""\ |
| 368 |
import os |
| 369 |
|
| 370 |
|
| 371 |
a = 3 |
| 372 |
|
| 373 |
os.getcwd()""" |
| 374 |
) |
| 375 |
|
| 376 |
result = fix_code(source) |
| 377 |
|
| 378 |
assert result == fixed_source |
| 379 |
|
| 380 |
|
| 381 |
def test_fix_moves_import_statements_in_indented_code_to_the_top() -> None: |
| 382 |
"""Move import statements present indented in the source code |
| 383 |
to the top of the file |
| 384 |
""" |
| 385 |
source = dedent( |
| 386 |
"""\ |
| 387 |
import requests |
| 388 |
|
| 389 |
requests.get('hi') |
| 390 |
|
| 391 |
def test(): |
| 392 |
import os |
| 393 |
os.getcwd()""" |
| 394 |
) |
| 395 |
fixed_source = dedent( |
| 396 |
"""\ |
| 397 |
import requests |
| 398 |
|
| 399 |
import os |
| 400 |
|
| 401 |
|
| 402 |
requests.get('hi') |
| 403 |
|
| 404 |
def test(): |
| 405 |
os.getcwd()""" |
| 406 |
) |
| 407 |
|
| 408 |
result = fix_code(source) |
| 409 |
|
| 410 |
assert result == fixed_source |
| 411 |
|
| 412 |
|
| 413 |
def test_fix_skips_moves_to_the_top_when_disabled_is_true() -> None: |
| 414 |
"""Skip moving import statements when disable_move_to_top config is true.""" |
| 415 |
source = dedent( |
| 416 |
"""\ |
| 417 |
import requests |
| 418 |
|
| 419 |
|
| 420 |
requests.get('hi') |
| 421 |
|
| 422 |
def test(): |
| 423 |
import os |
| 424 |
os.getcwd()""" |
| 425 |
) |
| 426 |
config = {"disable_move_to_top": True} |
| 427 |
|
| 428 |
result = fix_code(source, config) |
| 429 |
|
| 430 |
assert result == source |
| 431 |
|
| 432 |
|
| 433 |
def test_fix_skips_moves_to_the_top_when_disabled_is_false() -> None: |
| 434 |
"""Moving import statements should still occur when disable_move_to_top |
| 435 |
config is false. |
| 436 |
""" |
| 437 |
source = dedent( |
| 438 |
"""\ |
| 439 |
import requests |
| 440 |
|
| 441 |
requests.get('hi') |
| 442 |
|
| 443 |
def test(): |
| 444 |
import os |
| 445 |
os.getcwd()""" |
| 446 |
) |
| 447 |
fixed_source = dedent( |
| 448 |
"""\ |
| 449 |
import requests |
| 450 |
|
| 451 |
import os |
| 452 |
|
| 453 |
|
| 454 |
requests.get('hi') |
| 455 |
|
| 456 |
def test(): |
| 457 |
os.getcwd()""" |
| 458 |
) |
| 459 |
config = {"disable_move_to_top": False} |
| 460 |
|
| 461 |
result = fix_code(source, config) |
| 462 |
|
| 463 |
assert result == fixed_source |
| 464 |
|
| 465 |
|
| 466 |
def test_fix_moves_from_import_statements_to_the_top() -> None: |
| 467 |
"""Move from import statements present in the source code to the top of the file""" |
| 468 |
source = dedent( |
| 469 |
"""\ |
| 470 |
a = 3 |
| 471 |
|
| 472 |
from os import getcwd |
| 473 |
getcwd()""" |
| 474 |
) |
| 475 |
fixed_source = dedent( |
| 476 |
"""\ |
| 477 |
from os import getcwd |
| 478 |
|
| 479 |
|
| 480 |
a = 3 |
| 481 |
|
| 482 |
getcwd()""" |
| 483 |
) |
| 484 |
|
| 485 |
result = fix_code(source) |
| 486 |
|
| 487 |
assert result == fixed_source |
| 488 |
|
| 489 |
|
| 490 |
def test_fix_moves_multiline_import_statements_to_the_top() -> None: |
| 491 |
""" |
| 492 |
Given: Multiple from X import Y lines. |
| 493 |
When: Fix code is run. |
| 494 |
Then: The import statements are moved to the top. |
| 495 |
""" |
| 496 |
source = dedent( |
| 497 |
"""\ |
| 498 |
from os import getcwd |
| 499 |
|
| 500 |
getcwd() |
| 501 |
|
| 502 |
from re import ( |
| 503 |
match, |
| 504 |
) |
| 505 |
match(r'a', 'a')""" |
| 506 |
) |
| 507 |
fixed_source = dedent( |
| 508 |
"""\ |
| 509 |
from os import getcwd |
| 510 |
|
| 511 |
from re import ( |
| 512 |
match, |
| 513 |
) |
| 514 |
|
| 515 |
|
| 516 |
getcwd() |
| 517 |
|
| 518 |
match(r'a', 'a')""" |
| 519 |
) |
| 520 |
|
| 521 |
result = fix_code(source) |
| 522 |
|
| 523 |
assert result == fixed_source |
| 524 |
|
| 525 |
|
| 526 |
def test_fix_doesnt_break_objects_with_import_in_their_names() -> None: |
| 527 |
"""Objects that have the import name in their name should not be changed.""" |
| 528 |
source = dedent( |
| 529 |
"""\ |
| 530 |
def import_code(): |
| 531 |
pass |
| 532 |
|
| 533 |
def code_import(): |
| 534 |
pass |
| 535 |
|
| 536 |
def import(): |
| 537 |
pass |
| 538 |
|
| 539 |
import_string = 'a'""" |
| 540 |
) |
| 541 |
|
| 542 |
result = fix_code(source) |
| 543 |
|
| 544 |
assert result == source |
| 545 |
|
| 546 |
|
| 547 |
def test_fix_doesnt_move_import_statements_with_noqa_to_the_top() -> None: |
| 548 |
"""Ignore lines that have # noqa: autoimport.""" |
| 549 |
source = dedent( |
| 550 |
"""\ |
| 551 |
a = 3 |
| 552 |
|
| 553 |
from os import getcwd # noqa: autoimport |
| 554 |
getcwd()""" |
| 555 |
) |
| 556 |
|
| 557 |
result = fix_code(source) |
| 558 |
|
| 559 |
assert result == source |
| 560 |
|
| 561 |
|
| 562 |
def test_fix_doesnt_fail_on_noqa_lines_on_unused_import() -> None: |
| 563 |
"""Ignore lines that have # noqa: autoimport.""" |
| 564 |
source = dedent( |
| 565 |
"""\ |
| 566 |
from os import getcwd # noqa: autoimport""" |
| 567 |
) |
| 568 |
|
| 569 |
result = fix_code(source) |
| 570 |
|
| 571 |
assert result == source |
| 572 |
|
| 573 |
|
| 574 |
def test_fix_respects_fmt_skip_lines() -> None: |
| 575 |
"""Ignore lines that have # fmt: skip.""" |
| 576 |
source = dedent( |
| 577 |
""" |
| 578 |
def why(): |
| 579 |
import pdb;pdb.set_trace() # fmt: skip |
| 580 |
return 'dunno' |
| 581 |
""" |
| 582 |
).replace("\n", "", 1) |
| 583 |
|
| 584 |
result = fix_code(source) |
| 585 |
|
| 586 |
assert result == source |
| 587 |
|
| 588 |
|
| 589 |
def test_fix_respects_noqa_in_from_import_lines_in_multiple_lines() -> None: |
| 590 |
""" |
| 591 |
Given: Multiple from X import Y lines, some with multiline format with noqa |
| 592 |
statement. |
| 593 |
When: Fix code is run. |
| 594 |
Then: The import statements aren't broken. |
| 595 |
""" |
| 596 |
source = dedent( |
| 597 |
"""\ |
| 598 |
from os import getcwd |
| 599 |
|
| 600 |
|
| 601 |
getcwd() |
| 602 |
|
| 603 |
from re import ( # noqa: autoimport |
| 604 |
match, |
| 605 |
) |
| 606 |
|
| 607 |
match(r'a', 'a')""" |
| 608 |
) |
| 609 |
|
| 610 |
result = fix_code(source) |
| 611 |
|
| 612 |
assert result == source |
| 613 |
|
| 614 |
|
| 615 |
def test_fix_respects_strings_with_import_statements() -> None: |
| 616 |
""" |
| 617 |
Given: Code with a string that has import statements structure. |
| 618 |
When: Fix code is run. |
| 619 |
Then: The string is respected |
| 620 |
""" |
| 621 |
source = dedent( |
| 622 |
"""\ |
| 623 |
import_string = 'import requests' |
| 624 |
from_import_string = "from re import match" |
| 625 |
multiline string = dedent( |
| 626 |
\"\"\"\\ |
| 627 |
import requests |
| 628 |
from re import match |
| 629 |
\"\"\" |
| 630 |
) |
| 631 |
multiline single_quote_string = dedent( |
| 632 |
\'\'\'\\ |
| 633 |
import requests |
| 634 |
from re import match |
| 635 |
\'\'\' |
| 636 |
) |
| 637 |
import os""" |
| 638 |
) |
| 639 |
fixed_source = dedent( |
| 640 |
"""\ |
| 641 |
import os |
| 642 |
|
| 643 |
|
| 644 |
import_string = 'import requests' |
| 645 |
from_import_string = "from re import match" |
| 646 |
multiline string = dedent( |
| 647 |
\"\"\"\\ |
| 648 |
import requests |
| 649 |
from re import match |
| 650 |
\"\"\" |
| 651 |
) |
| 652 |
multiline single_quote_string = dedent( |
| 653 |
\'\'\'\\ |
| 654 |
import requests |
| 655 |
from re import match |
| 656 |
\'\'\' |
| 657 |
)""" |
| 658 |
) |
| 659 |
|
| 660 |
result = fix_code(source) |
| 661 |
|
| 662 |
assert result == fixed_source |
| 663 |
|
| 664 |
|
| 665 |
def test_fix_doesnt_mistake_docstrings_with_multiline_string() -> None: |
| 666 |
""" |
| 667 |
Given: A function with a docstring. |
| 668 |
When: Fix code is run. |
| 669 |
Then: The rest of the file is not mistaken for a long multiline string |
| 670 |
""" |
| 671 |
source = dedent( |
| 672 |
"""\ |
| 673 |
def function_1(): |
| 674 |
\"\"\"Function docstring\"\"\" |
| 675 |
import os |
| 676 |
os.getcwd()""" |
| 677 |
) |
| 678 |
fixed_source = dedent( |
| 679 |
"""\ |
| 680 |
import os |
| 681 |
|
| 682 |
|
| 683 |
def function_1(): |
| 684 |
\"\"\"Function docstring\"\"\" |
| 685 |
os.getcwd()""" |
| 686 |
) |
| 687 |
|
| 688 |
result = fix_code(source) |
| 689 |
|
| 690 |
assert result == fixed_source |
| 691 |
|
| 692 |
|
| 693 |
@pytest.mark.parametrize( |
| 694 |
("import_key", "import_statement"), |
| 695 |
((key, value) for key, value in common_statements.items()), |
| 696 |
ids=list(common_statements.keys()), |
| 697 |
) |
| 698 |
def test_fix_autoimports_common_imports(import_key: str, import_statement: str) -> None: |
| 699 |
""" |
| 700 |
Given: Code with missing import statements that match the common list. |
| 701 |
When: Fix code is run. |
| 702 |
Then: The imports are done |
| 703 |
""" |
| 704 |
source = dedent( |
| 705 |
f"""\ |
| 706 |
import os |
| 707 |
|
| 708 |
os.getcwd |
| 709 |
|
| 710 |
variable = {import_key}""" |
| 711 |
) |
| 712 |
fixed_source = dedent( |
| 713 |
f"""\ |
| 714 |
import os |
| 715 |
|
| 716 |
{import_statement} |
| 717 |
|
| 718 |
|
| 719 |
os.getcwd |
| 720 |
|
| 721 |
variable = {import_key}""" |
| 722 |
) |
| 723 |
|
| 724 |
result = fix_code(source) |
| 725 |
|
| 726 |
assert result == fixed_source |
| 727 |
|
| 728 |
|
| 729 |
def test_fix_autoimports_objects_defined_in_the_root_of_the_package() -> None: |
| 730 |
""" |
| 731 |
Given: |
| 732 |
The fix code is run from a directory that belongs to a python project package. |
| 733 |
And a source code with an object that needs an import statement. |
| 734 |
And that object belongs to the root of the python package. |
| 735 |
When: Fix code is run. |
| 736 |
Then: The import is done |
| 737 |
""" |
| 738 |
source = dedent( |
| 739 |
"""\ |
| 740 |
fix_code()""" |
| 741 |
) |
| 742 |
fixed_source = dedent( |
| 743 |
"""\ |
| 744 |
from autoimport import fix_code |
| 745 |
|
| 746 |
|
| 747 |
fix_code()""" |
| 748 |
) |
| 749 |
|
| 750 |
result = fix_code(source) |
| 751 |
|
| 752 |
assert result == fixed_source |
| 753 |
|
| 754 |
|
| 755 |
def test_fix_autoimports_objects_defined_in___all__special_variable() -> None: |
| 756 |
""" |
| 757 |
Given: Some missing packages in the __all__ variable |
| 758 |
When: Fix code is run. |
| 759 |
Then: The import is done |
| 760 |
""" |
| 761 |
source = dedent( |
| 762 |
"""\ |
| 763 |
__all__ = ['fix_code']""" |
| 764 |
) |
| 765 |
fixed_source = dedent( |
| 766 |
"""\ |
| 767 |
from autoimport import fix_code |
| 768 |
|
| 769 |
|
| 770 |
__all__ = ['fix_code']""" |
| 771 |
) |
| 772 |
|
| 773 |
result = fix_code(source) |
| 774 |
|
| 775 |
assert result == fixed_source |
| 776 |
|
| 777 |
|
| 778 |
@pytest.mark.parametrize( |
| 779 |
"source", |
| 780 |
[ |
| 781 |
dedent( |
| 782 |
"""\ |
| 783 |
import os |
| 784 |
from typing import TYPE_CHECKING |
| 785 |
|
| 786 |
if TYPE_CHECKING: |
| 787 |
from .model import Book |
| 788 |
|
| 789 |
|
| 790 |
os.getcwd() |
| 791 |
|
| 792 |
|
| 793 |
def read_book(book: Book): |
| 794 |
pass""" |
| 795 |
), |
| 796 |
dedent( |
| 797 |
"""\ |
| 798 |
from typing import TYPE_CHECKING |
| 799 |
|
| 800 |
if TYPE_CHECKING: |
| 801 |
foo = "bar" |
| 802 |
""" |
| 803 |
), |
| 804 |
], |
| 805 |
) |
| 806 |
def test_fix_respects_type_checking_import_statements(source: str) -> None: |
| 807 |
""" |
| 808 |
Given: Code with if TYPE_CHECKING imports |
| 809 |
When: Fix code is run. |
| 810 |
Then: The imports are not moved above the if statement. |
| 811 |
|
| 812 |
Related to https://github.com/lyz-code/autoimport/issues/231 |
| 813 |
""" |
| 814 |
result = fix_code(source) |
| 815 |
|
| 816 |
assert result == source |
| 817 |
|
| 818 |
|
| 819 |
def test_fix_respects_multiparagraph_type_checking_import_statements() -> None: |
| 820 |
""" |
| 821 |
Given: Code with two paragraphs of imports inside an if TYPE_CHECKING block |
| 822 |
When: Fix code is run. |
| 823 |
Then: The imports are not moved above the if statement. |
| 824 |
""" |
| 825 |
source = dedent( |
| 826 |
"""\ |
| 827 |
import os |
| 828 |
from typing import TYPE_CHECKING |
| 829 |
|
| 830 |
if TYPE_CHECKING: |
| 831 |
from .model import Book |
| 832 |
|
| 833 |
from other import Other |
| 834 |
|
| 835 |
|
| 836 |
os.getcwd() |
| 837 |
|
| 838 |
|
| 839 |
def read_book(book: Book, other: Other): |
| 840 |
pass""" |
| 841 |
) |
| 842 |
|
| 843 |
result = fix_code(source) |
| 844 |
|
| 845 |
assert result == source |
| 846 |
|
| 847 |
|
| 848 |
def test_fix_creates_the_typing_import() -> None: |
| 849 |
""" |
| 850 |
Given: Code with no TYPE_CHECKING import statement |
| 851 |
When: Fix code is run. |
| 852 |
Then: The import is created. |
| 853 |
|
| 854 |
Related to https://github.com/lyz-code/autoimport/issues/231 |
| 855 |
""" |
| 856 |
source = dedent( |
| 857 |
"""\ |
| 858 |
if TYPE_CHECKING: |
| 859 |
foo = 'bar'""" |
| 860 |
) |
| 861 |
fixed_source = dedent( |
| 862 |
"""\ |
| 863 |
from typing import TYPE_CHECKING |
| 864 |
|
| 865 |
if TYPE_CHECKING: |
| 866 |
foo = 'bar'""" |
| 867 |
) |
| 868 |
|
| 869 |
result = fix_code(source) |
| 870 |
|
| 871 |
assert result == fixed_source |
| 872 |
|
| 873 |
|
| 874 |
def test_fix_respects_try_except_in_import_statements() -> None: |
| 875 |
""" |
| 876 |
Given: Code with try except statements in the imports. |
| 877 |
When: Fix code is run |
| 878 |
Then: The try except statements are respected |
| 879 |
""" |
| 880 |
source = dedent( |
| 881 |
"""\ |
| 882 |
import os |
| 883 |
|
| 884 |
try: |
| 885 |
from typing import TypedDict # noqa |
| 886 |
except ImportError: |
| 887 |
from mypy_extensions import TypedDict # <=3.7 |
| 888 |
|
| 889 |
|
| 890 |
os.getcwd() |
| 891 |
Movie = TypedDict('Movie', {'name': str, 'year': int})""" |
| 892 |
) |
| 893 |
|
| 894 |
result = fix_code(source) |
| 895 |
|
| 896 |
assert result == source |
| 897 |
|
| 898 |
|
| 899 |
def test_fix_respects_leading_comments() -> None: |
| 900 |
""" |
| 901 |
Given: Code with initial comments like shebang and editor configuration. |
| 902 |
When: Fix code is run |
| 903 |
Then: The comment statements are respected |
| 904 |
""" |
| 905 |
source = dedent( |
| 906 |
'''\ |
| 907 |
#!/usr/bin/env python3 |
| 908 |
# -*- coding: latin-1 -*- |
| 909 |
"""docstring""" |
| 910 |
print(os.path.exists("."))''' |
| 911 |
) |
| 912 |
desired_source = dedent( |
| 913 |
'''\ |
| 914 |
#!/usr/bin/env python3 |
| 915 |
# -*- coding: latin-1 -*- |
| 916 |
"""docstring""" |
| 917 |
|
| 918 |
import os |
| 919 |
|
| 920 |
|
| 921 |
print(os.path.exists("."))''' |
| 922 |
) |
| 923 |
|
| 924 |
result = fix_code(source) |
| 925 |
|
| 926 |
assert result == desired_source |
| 927 |
|
| 928 |
|
| 929 |
def test_fix_respects_leading_comments_with_new_lines() -> None: |
| 930 |
""" |
| 931 |
Given: Code with initial comments with new lines and a trailing newline. |
| 932 |
When: Fix code is run. |
| 933 |
Then: The comment statements and trailing newline are respected. |
| 934 |
""" |
| 935 |
source = dedent( |
| 936 |
'''\ |
| 937 |
#!/usr/bin/env python3 |
| 938 |
# -*- coding: latin-1 -*- |
| 939 |
|
| 940 |
# pylint: disable=foobar |
| 941 |
|
| 942 |
""" |
| 943 |
|
| 944 |
This is the docstring. |
| 945 |
|
| 946 |
""" |
| 947 |
|
| 948 |
import sys |
| 949 |
|
| 950 |
print(os.path.exists(sys.argv[1])) |
| 951 |
''' |
| 952 |
) |
| 953 |
desired_source = dedent( |
| 954 |
'''\ |
| 955 |
#!/usr/bin/env python3 |
| 956 |
# -*- coding: latin-1 -*- |
| 957 |
|
| 958 |
# pylint: disable=foobar |
| 959 |
|
| 960 |
""" |
| 961 |
|
| 962 |
This is the docstring. |
| 963 |
|
| 964 |
""" |
| 965 |
|
| 966 |
import sys |
| 967 |
|
| 968 |
import os |
| 969 |
|
| 970 |
|
| 971 |
print(os.path.exists(sys.argv[1])) |
| 972 |
''' |
| 973 |
) |
| 974 |
|
| 975 |
result = fix_code(source) |
| 976 |
|
| 977 |
assert result == desired_source |
| 978 |
|
| 979 |
|
| 980 |
def test_fix_imports_dependency_only_once() -> None: |
| 981 |
""" |
| 982 |
Given: Code with a line that uses a package three times. |
| 983 |
When: Fix code is run. |
| 984 |
Then: The dependency is imported only once |
| 985 |
""" |
| 986 |
source = dedent( |
| 987 |
"""\ |
| 988 |
def f(x): |
| 989 |
return os.getcwd() + os.getcwd() + os.getcwd() |
| 990 |
""" |
| 991 |
) |
| 992 |
desired_source = dedent( |
| 993 |
"""\ |
| 994 |
import os |
| 995 |
|
| 996 |
|
| 997 |
def f(x): |
| 998 |
return os.getcwd() + os.getcwd() + os.getcwd() |
| 999 |
""" |
| 1000 |
) |
| 1001 |
|
| 1002 |
result = fix_code(source) |
| 1003 |
|
| 1004 |
assert result == desired_source |
| 1005 |
|
| 1006 |
|
| 1007 |
def test_fix_doesnt_fail_on_empty_file() -> None: |
| 1008 |
""" |
| 1009 |
Given: An empty file |
| 1010 |
When: Fix code is run. |
| 1011 |
Then: The output doesn't change |
| 1012 |
""" |
| 1013 |
source = "" |
| 1014 |
|
| 1015 |
result = fix_code(source) |
| 1016 |
|
| 1017 |
assert result == source |
| 1018 |
|
| 1019 |
|
| 1020 |
def test_fix_not_remove_unused_imports() -> None: |
| 1021 |
""" |
| 1022 |
Given: Code with imports, few being used, others not being used. |
| 1023 |
When: Fix code is run. |
| 1024 |
Then: Missing imports added, unused imports not removed. |
| 1025 |
""" |
| 1026 |
source = dedent( |
| 1027 |
"""\ |
| 1028 |
import gzip |
| 1029 |
import hashlib |
| 1030 |
|
| 1031 |
csv_writer = csv.DictWriter(filename, fieldnames=["name", "age"]) |
| 1032 |
gzip.open(filename, 'wb') |
| 1033 |
""" |
| 1034 |
) |
| 1035 |
desired_source = dedent( |
| 1036 |
"""\ |
| 1037 |
import gzip |
| 1038 |
import hashlib |
| 1039 |
|
| 1040 |
import csv |
| 1041 |
|
| 1042 |
|
| 1043 |
csv_writer = csv.DictWriter(filename, fieldnames=["name", "age"]) |
| 1044 |
gzip.open(filename, 'wb') |
| 1045 |
""" |
| 1046 |
) |
| 1047 |
|
| 1048 |
result = fix_code(source, keep_unused_imports=True) |
| 1049 |
|
| 1050 |
assert result == desired_source |
| 1051 |
|
| 1052 |
|
| 1053 |
def test_file_that_only_has_unused_imports() -> None: |
| 1054 |
""" |
| 1055 |
Given: A file that only has unused imports. |
| 1056 |
When: Fix code is run. |
| 1057 |
Then: The output should be a single empty line. |
| 1058 |
""" |
| 1059 |
source = dedent( |
| 1060 |
"""\ |
| 1061 |
import os |
| 1062 |
import sys |
| 1063 |
""" |
| 1064 |
) |
| 1065 |
|
| 1066 |
result = fix_code(source) |
| 1067 |
|
| 1068 |
assert result == "\n" |
| 1069 |
|
| 1070 |
|
| 1071 |
def test_file_with_common_statement() -> None: |
| 1072 |
""" |
| 1073 |
Given: Code with a commonly-used object. |
| 1074 |
When: Fix code is run. |
| 1075 |
Then: The appropriate import statement from the common_statements dict is added. |
| 1076 |
""" |
| 1077 |
source = dedent( |
| 1078 |
"""\ |
| 1079 |
BeautifulSoup |
| 1080 |
""" |
| 1081 |
) |
| 1082 |
desired_source = dedent( |
| 1083 |
"""\ |
| 1084 |
from bs4 import BeautifulSoup |
| 1085 |
|
| 1086 |
|
| 1087 |
BeautifulSoup |
| 1088 |
""" |
| 1089 |
) |
| 1090 |
|
| 1091 |
result = fix_code(source) |
| 1092 |
|
| 1093 |
assert result == desired_source |
| 1094 |
|
| 1095 |
|
| 1096 |
def test_file_with_custom_common_statement() -> None: |
| 1097 |
""" |
| 1098 |
Given: Code that uses an undefined object called `FooBar`. |
| 1099 |
When: |
| 1100 |
Fix code is run and a `config` dict is passed specifying `FooBar` as a common |
| 1101 |
statement. |
| 1102 |
Then: |
| 1103 |
The appropriate import statement from the common_statements dict is added. |
| 1104 |
""" |
| 1105 |
source = dedent( |
| 1106 |
"""\ |
| 1107 |
FooBar |
| 1108 |
""" |
| 1109 |
) |
| 1110 |
custom_config = {"common_statements": {"FooBar": "from baz_qux import FooBar"}} |
| 1111 |
desired_source = dedent( |
| 1112 |
"""\ |
| 1113 |
from baz_qux import FooBar |
| 1114 |
|
| 1115 |
|
| 1116 |
FooBar |
| 1117 |
""" |
| 1118 |
) |
| 1119 |
|
| 1120 |
result = fix_code(source, config=custom_config) |
| 1121 |
|
| 1122 |
assert result == desired_source |
| 1123 |
|
| 1124 |
|
| 1125 |
def test_file_with_comment_in_import() -> None: |
| 1126 |
""" |
| 1127 |
Given: Code with a comment on two import statements |
| 1128 |
When: Fix code is run. |
| 1129 |
Then: The unused import line is removed with it's comment |
| 1130 |
""" |
| 1131 |
source = dedent( |
| 1132 |
"""\ |
| 1133 |
import os # comment 1 |
| 1134 |
import sys # comment 2 |
| 1135 |
|
| 1136 |
os.getcwd() |
| 1137 |
""" |
| 1138 |
) |
| 1139 |
desired_source = dedent( |
| 1140 |
"""\ |
| 1141 |
import os # comment 1 |
| 1142 |
|
| 1143 |
|
| 1144 |
os.getcwd() |
| 1145 |
""" |
| 1146 |
) |
| 1147 |
|
| 1148 |
result = fix_code(source) |
| 1149 |
|
| 1150 |
assert result == desired_source |
| 1151 |
|
| 1152 |
|
| 1153 |
def test_file_with_comment_in_from_import() -> None: |
| 1154 |
""" |
| 1155 |
Given: Code with a comment on two import statements |
| 1156 |
When: Fix code is run. |
| 1157 |
Then: The unused import line is removed with it's comment |
| 1158 |
""" |
| 1159 |
source = dedent( |
| 1160 |
"""\ |
| 1161 |
import os # comment 1 |
| 1162 |
from textwrap import dedent # comment 2 |
| 1163 |
|
| 1164 |
os.getcwd() |
| 1165 |
""" |
| 1166 |
) |
| 1167 |
desired_source = dedent( |
| 1168 |
"""\ |
| 1169 |
import os # comment 1 |
| 1170 |
|
| 1171 |
|
| 1172 |
os.getcwd() |
| 1173 |
""" |
| 1174 |
) |
| 1175 |
|
| 1176 |
result = fix_code(source) |
| 1177 |
|
| 1178 |
assert result == desired_source |
| 1179 |
|
| 1180 |
|
| 1181 |
def test_file_with_comment_in_from_import_partial_remove() -> None: |
| 1182 |
""" |
| 1183 |
Given: Code with a comment on an from import statement |
| 1184 |
When: Fix code is run. |
| 1185 |
Then: The unused dependency is removed but the comment is respected |
| 1186 |
""" |
| 1187 |
source = dedent( |
| 1188 |
"""\ |
| 1189 |
from os import getcwd, chmod # noqa: E0611 |
| 1190 |
|
| 1191 |
getcwd() |
| 1192 |
""" |
| 1193 |
) |
| 1194 |
desired_source = dedent( |
| 1195 |
"""\ |
| 1196 |
from os import getcwd # noqa: E0611 |
| 1197 |
|
| 1198 |
|
| 1199 |
getcwd() |
| 1200 |
""" |
| 1201 |
) |
| 1202 |
|
| 1203 |
result = fix_code(source) |
| 1204 |
|
| 1205 |
assert result == desired_source |
| 1206 |
|
| 1207 |
|
| 1208 |
def test_file_with_comment_in_from_import_that_will_dissapear() -> None: |
| 1209 |
""" |
| 1210 |
Given: Code with a comment on an from import statement that is to be deleted |
| 1211 |
When: Fix code is run. |
| 1212 |
Then: Everything is deleted |
| 1213 |
""" |
| 1214 |
source = dedent( |
| 1215 |
"""\ |
| 1216 |
from os import getcwd, chmod # noqa: E0611 |
| 1217 |
|
| 1218 |
a = 1 |
| 1219 |
""" |
| 1220 |
) |
| 1221 |
desired_source = dedent( |
| 1222 |
"""\ |
| 1223 |
a = 1 |
| 1224 |
""" |
| 1225 |
) |
| 1226 |
|
| 1227 |
result = fix_code(source) |
| 1228 |
|
| 1229 |
assert result == desired_source |
| 1230 |
|
| 1231 |
|
| 1232 |
def test_file_with_import_as() -> None: |
| 1233 |
""" |
| 1234 |
Given: Code with an from x import y as z import statement |
| 1235 |
When: Fix code is run. |
| 1236 |
Then: The unused import line is removed |
| 1237 |
""" |
| 1238 |
source = dedent( |
| 1239 |
"""\ |
| 1240 |
from subprocess import run as run |
| 1241 |
""" |
| 1242 |
) |
| 1243 |
|
| 1244 |
result = fix_code(source) |
| 1245 |
|
| 1246 |
assert result == "\n" |
| 1247 |
|
| 1248 |
|
| 1249 |
def test_file_with_non_used_multiline_import() -> None: |
| 1250 |
""" |
| 1251 |
Given: Code with a multiline from import where no one is used. |
| 1252 |
When: Fix code is run. |
| 1253 |
Then: The unused import line is removed |
| 1254 |
""" |
| 1255 |
source = dedent( |
| 1256 |
"""\ |
| 1257 |
from foo import ( |
| 1258 |
bar, |
| 1259 |
baz, |
| 1260 |
) |
| 1261 |
""" |
| 1262 |
) |
| 1263 |
|
| 1264 |
result = fix_code(source) |
| 1265 |
|
| 1266 |
assert result == "\n" |
| 1267 |
|
| 1268 |
|
| 1269 |
def test_file_with_import_and_seperator() -> None: |
| 1270 |
"""Ensure import lines with seperators are fixed correctly.""" |
| 1271 |
source = dedent( |
| 1272 |
""" |
| 1273 |
a = 1 |
| 1274 |
import pdb;pdb.set_trace() |
| 1275 |
b = 2 |
| 1276 |
""" |
| 1277 |
) |
| 1278 |
expected = dedent( |
| 1279 |
""" |
| 1280 |
import pdb |
| 1281 |
|
| 1282 |
|
| 1283 |
a = 1 |
| 1284 |
pdb.set_trace() |
| 1285 |
b = 2 |
| 1286 |
""" |
| 1287 |
).replace("\n", "", 1) |
| 1288 |
|
| 1289 |
result = fix_code(source) |
| 1290 |
|
| 1291 |
assert result == expected |
| 1292 |
|
| 1293 |
|
| 1294 |
def test_file_with_import_and_seperator_indentation() -> None: |
| 1295 |
"""Ensure import lines with seperators are fixed correctly when indented.""" |
| 1296 |
source = dedent( |
| 1297 |
""" |
| 1298 |
Class Person: |
| 1299 |
import pdb; pdb.set_trace() |
| 1300 |
def say_hi(self): |
| 1301 |
print('hi') |
| 1302 |
""" |
| 1303 |
) |
| 1304 |
expected = dedent( |
| 1305 |
""" |
| 1306 |
import pdb |
| 1307 |
|
| 1308 |
|
| 1309 |
Class Person: |
| 1310 |
pdb.set_trace() |
| 1311 |
def say_hi(self): |
| 1312 |
print('hi') |
| 1313 |
""" |
| 1314 |
).replace("\n", "", 1) |
| 1315 |
|
| 1316 |
result = fix_code(source) |
| 1317 |
|
| 1318 |
assert result == expected |
| 1319 |
|
| 1320 |
|
| 1321 |
def test_import_module_with_dot() -> None: |
| 1322 |
""" |
| 1323 |
Given: An import file with an import with a dot |
| 1324 |
When: running autoimport on the file |
| 1325 |
Then: ValueError exception is not raised |
| 1326 |
|
| 1327 |
Tests https://github.com/lyz-code/autoimport/issues/225 |
| 1328 |
""" |
| 1329 |
source = dedent( |
| 1330 |
""" |
| 1331 |
import my_module.m |
| 1332 |
""" |
| 1333 |
) |
| 1334 |
|
| 1335 |
result = fix_code(source) |
| 1336 |
|
| 1337 |
assert result == "\n" |
| 1338 |
|
| 1339 |
|
| 1340 |
def test_respect_new_lines_between_imports_and_code() -> None: |
| 1341 |
r""" |
| 1342 |
Given: A file with two \n between imports and the code |
| 1343 |
When: running autoimport on the file |
| 1344 |
Then: the file is untouched |
| 1345 |
|
| 1346 |
For more info check https://github.com/lyz-code/autoimport/issues/219 |
| 1347 |
""" |
| 1348 |
source = dedent( |
| 1349 |
"""\ |
| 1350 |
import random |
| 1351 |
|
| 1352 |
|
| 1353 |
def foo(): |
| 1354 |
print(random.random()) |
| 1355 |
""" |
| 1356 |
) |
| 1357 |
|
| 1358 |
result = fix_code(source) |
| 1359 |
|
| 1360 |
assert result == source |