|
3 | 3 | from collections import namedtuple |
4 | 4 |
|
5 | 5 | import os |
6 | | -import sys |
7 | 6 | import shlex |
8 | 7 | import shutil |
9 | 8 | import pytest |
@@ -502,6 +501,189 @@ def foo(): |
502 | 501 | in err) |
503 | 502 |
|
504 | 503 |
|
| 504 | +def test_overload_function(env): |
| 505 | + """Functions decorated with @overload trigger D418 error.""" |
| 506 | + with env.open('example.py', 'wt') as example: |
| 507 | + example.write(textwrap.dedent('''\ |
| 508 | + from typing import overload |
| 509 | +
|
| 510 | +
|
| 511 | + @overload |
| 512 | + def overloaded_func(a: int) -> str: |
| 513 | + ... |
| 514 | +
|
| 515 | +
|
| 516 | + @overload |
| 517 | + def overloaded_func(a: str) -> str: |
| 518 | + """Foo bar documentation.""" |
| 519 | + ... |
| 520 | +
|
| 521 | +
|
| 522 | + def overloaded_func(a): |
| 523 | + """Foo bar documentation.""" |
| 524 | + return str(a) |
| 525 | +
|
| 526 | + ''')) |
| 527 | + env.write_config(ignore="D100") |
| 528 | + out, err, code = env.invoke() |
| 529 | + assert code == 1 |
| 530 | + assert 'D418' in out |
| 531 | + assert 'D103' not in out |
| 532 | + |
| 533 | + |
| 534 | +def test_overload_method(env): |
| 535 | + """Methods decorated with @overload trigger D418 error.""" |
| 536 | + with env.open('example.py', 'wt') as example: |
| 537 | + example.write(textwrap.dedent('''\ |
| 538 | + from typing import overload |
| 539 | +
|
| 540 | + class ClassWithMethods: |
| 541 | + @overload |
| 542 | + def overloaded_method(a: int) -> str: |
| 543 | + ... |
| 544 | +
|
| 545 | +
|
| 546 | + @overload |
| 547 | + def overloaded_method(a: str) -> str: |
| 548 | + """Foo bar documentation.""" |
| 549 | + ... |
| 550 | +
|
| 551 | +
|
| 552 | + def overloaded_method(a): |
| 553 | + """Foo bar documentation.""" |
| 554 | + return str(a) |
| 555 | +
|
| 556 | + ''')) |
| 557 | + env.write_config(ignore="D100") |
| 558 | + out, err, code = env.invoke() |
| 559 | + assert code == 1 |
| 560 | + assert 'D418' in out |
| 561 | + assert 'D102' not in out |
| 562 | + assert 'D103' not in out |
| 563 | + |
| 564 | + |
| 565 | +def test_overload_method_valid(env): |
| 566 | + """Valid case for overload decorated Methods. |
| 567 | +
|
| 568 | + This shouldn't throw any errors. |
| 569 | + """ |
| 570 | + with env.open('example.py', 'wt') as example: |
| 571 | + example.write(textwrap.dedent('''\ |
| 572 | + from typing import overload |
| 573 | +
|
| 574 | + class ClassWithMethods: |
| 575 | + """Valid docstring in public Class.""" |
| 576 | +
|
| 577 | + @overload |
| 578 | + def overloaded_method(a: int) -> str: |
| 579 | + ... |
| 580 | +
|
| 581 | +
|
| 582 | + @overload |
| 583 | + def overloaded_method(a: str) -> str: |
| 584 | + ... |
| 585 | +
|
| 586 | +
|
| 587 | + def overloaded_method(a): |
| 588 | + """Foo bar documentation.""" |
| 589 | + return str(a) |
| 590 | +
|
| 591 | + ''')) |
| 592 | + env.write_config(ignore="D100, D203") |
| 593 | + out, err, code = env.invoke() |
| 594 | + assert code == 0 |
| 595 | + |
| 596 | + |
| 597 | +def test_overload_function_valid(env): |
| 598 | + """Valid case for overload decorated functions. |
| 599 | +
|
| 600 | + This shouldn't throw any errors. |
| 601 | + """ |
| 602 | + with env.open('example.py', 'wt') as example: |
| 603 | + example.write(textwrap.dedent('''\ |
| 604 | + from typing import overload |
| 605 | +
|
| 606 | +
|
| 607 | + @overload |
| 608 | + def overloaded_func(a: int) -> str: |
| 609 | + ... |
| 610 | +
|
| 611 | +
|
| 612 | + @overload |
| 613 | + def overloaded_func(a: str) -> str: |
| 614 | + ... |
| 615 | +
|
| 616 | +
|
| 617 | + def overloaded_func(a): |
| 618 | + """Foo bar documentation.""" |
| 619 | + return str(a) |
| 620 | +
|
| 621 | + ''')) |
| 622 | + env.write_config(ignore="D100") |
| 623 | + out, err, code = env.invoke() |
| 624 | + assert code == 0 |
| 625 | + |
| 626 | + |
| 627 | +def test_overload_nested_function(env): |
| 628 | + """Nested functions decorated with @overload trigger D418 error.""" |
| 629 | + with env.open('example.py', 'wt') as example: |
| 630 | + example.write(textwrap.dedent('''\ |
| 631 | + from typing import overload |
| 632 | +
|
| 633 | + def function_with_nesting(): |
| 634 | + """Valid docstring in public function.""" |
| 635 | + @overload |
| 636 | + def overloaded_func(a: int) -> str: |
| 637 | + ... |
| 638 | +
|
| 639 | +
|
| 640 | + @overload |
| 641 | + def overloaded_func(a: str) -> str: |
| 642 | + """Foo bar documentation.""" |
| 643 | + ... |
| 644 | +
|
| 645 | +
|
| 646 | + def overloaded_func(a): |
| 647 | + """Foo bar documentation.""" |
| 648 | + return str(a) |
| 649 | + ''')) |
| 650 | + env.write_config(ignore="D100") |
| 651 | + out, err, code = env.invoke() |
| 652 | + assert code == 1 |
| 653 | + assert 'D418' in out |
| 654 | + assert 'D103' not in out |
| 655 | + |
| 656 | + |
| 657 | +def test_overload_nested_function_valid(env): |
| 658 | + """Valid case for overload decorated nested functions. |
| 659 | +
|
| 660 | + This shouldn't throw any errors. |
| 661 | + """ |
| 662 | + with env.open('example.py', 'wt') as example: |
| 663 | + example.write(textwrap.dedent('''\ |
| 664 | + from typing import overload |
| 665 | +
|
| 666 | + def function_with_nesting(): |
| 667 | + """Adding a docstring to a function.""" |
| 668 | + @overload |
| 669 | + def overloaded_func(a: int) -> str: |
| 670 | + ... |
| 671 | +
|
| 672 | +
|
| 673 | + @overload |
| 674 | + def overloaded_func(a: str) -> str: |
| 675 | + ... |
| 676 | +
|
| 677 | +
|
| 678 | + def overloaded_func(a): |
| 679 | + """Foo bar documentation.""" |
| 680 | + return str(a) |
| 681 | + ''')) |
| 682 | + env.write_config(ignore="D100") |
| 683 | + out, err, code = env.invoke() |
| 684 | + assert code == 0 |
| 685 | + |
| 686 | + |
505 | 687 | def test_conflicting_select_ignore_config(env): |
506 | 688 | """Test that select and ignore are mutually exclusive.""" |
507 | 689 | env.write_config(select="D100", ignore="D101") |
|
0 commit comments