|
24 | 24 | import java.util.HashSet;
|
25 | 25 | import java.util.List;
|
26 | 26 | import java.util.Map;
|
| 27 | +import java.util.Optional; |
27 | 28 | import java.util.Set;
|
28 | 29 | import org.assertj.core.api.Assertions;
|
29 | 30 | import org.junit.jupiter.api.Disabled;
|
|
39 | 40 | import org.sonar.plugins.python.api.tree.ImportFrom;
|
40 | 41 | import org.sonar.plugins.python.api.tree.ImportName;
|
41 | 42 | import org.sonar.plugins.python.api.tree.Name;
|
| 43 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
42 | 44 | import org.sonar.plugins.python.api.tree.Statement;
|
43 | 45 | import org.sonar.plugins.python.api.tree.StatementList;
|
44 | 46 | import org.sonar.plugins.python.api.tree.Tree;
|
@@ -626,6 +628,207 @@ def foo():
|
626 | 628 | """).typeV2().unwrappedType()).isEqualTo(INT_TYPE);
|
627 | 629 | }
|
628 | 630 |
|
| 631 | + @Test |
| 632 | + void flow_insensitive_when_try_except() { |
| 633 | + FileInput fileInput = inferTypes(""" |
| 634 | + try: |
| 635 | + if p: |
| 636 | + x = 42 |
| 637 | + type(x) |
| 638 | + else: |
| 639 | + x = "foo" |
| 640 | + type(x) |
| 641 | + except: |
| 642 | + type(x) |
| 643 | + """); |
| 644 | + |
| 645 | + List<CallExpression> calls = PythonTestUtils.getAllDescendant(fileInput, tree -> tree.is(Tree.Kind.CALL_EXPR)); |
| 646 | + RegularArgument firstX = (RegularArgument) calls.get(0).arguments().get(0); |
| 647 | + RegularArgument secondX = (RegularArgument) calls.get(1).arguments().get(0); |
| 648 | + RegularArgument thirdX = (RegularArgument) calls.get(2).arguments().get(0); |
| 649 | + assertThat(((UnionType) firstX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 650 | + assertThat(((UnionType) secondX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 651 | + assertThat(((UnionType) thirdX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 652 | + } |
| 653 | + |
| 654 | + @Test |
| 655 | + void nested_try_except() { |
| 656 | + FileInput fileInput = inferTypes(""" |
| 657 | + def f(p): |
| 658 | + try: |
| 659 | + if p: |
| 660 | + x = 42 |
| 661 | + type(x) |
| 662 | + else: |
| 663 | + x = "foo" |
| 664 | + type(x) |
| 665 | + except: |
| 666 | + type(x) |
| 667 | + def g(p): |
| 668 | + if p: |
| 669 | + y = 42 |
| 670 | + type(y) |
| 671 | + else: |
| 672 | + y = "hello" |
| 673 | + type(y) |
| 674 | + type(y) |
| 675 | + if cond: |
| 676 | + z = 42 |
| 677 | + type(z) |
| 678 | + else: |
| 679 | + z = "hello" |
| 680 | + type(z) |
| 681 | + type(z) |
| 682 | + """); |
| 683 | + List<CallExpression> calls = PythonTestUtils.getAllDescendant(fileInput, tree -> tree.is(Tree.Kind.CALL_EXPR)); |
| 684 | + RegularArgument firstX = (RegularArgument) calls.get(0).arguments().get(0); |
| 685 | + RegularArgument secondX = (RegularArgument) calls.get(1).arguments().get(0); |
| 686 | + RegularArgument thirdX = (RegularArgument) calls.get(2).arguments().get(0); |
| 687 | + assertThat(((UnionType) firstX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 688 | + assertThat(((UnionType) secondX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 689 | + assertThat(((UnionType) thirdX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 690 | + |
| 691 | + RegularArgument firstY = (RegularArgument) calls.get(3).arguments().get(0); |
| 692 | + RegularArgument secondY = (RegularArgument) calls.get(4).arguments().get(0); |
| 693 | + RegularArgument thirdY = (RegularArgument) calls.get(5).arguments().get(0); |
| 694 | + assertThat(firstY.expression().typeV2().unwrappedType()).isEqualTo(INT_TYPE); |
| 695 | + assertThat(secondY.expression().typeV2().unwrappedType()).isEqualTo(STR_TYPE); |
| 696 | + assertThat(((UnionType) thirdY.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 697 | + |
| 698 | + RegularArgument firstZ = (RegularArgument) calls.get(6).arguments().get(0); |
| 699 | + RegularArgument secondZ = (RegularArgument) calls.get(7).arguments().get(0); |
| 700 | + RegularArgument thirdZ = (RegularArgument) calls.get(8).arguments().get(0); |
| 701 | + assertThat(firstZ.expression().typeV2().unwrappedType()).isEqualTo(INT_TYPE); |
| 702 | + assertThat(secondZ.expression().typeV2().unwrappedType()).isEqualTo(STR_TYPE); |
| 703 | + assertThat(((UnionType) thirdZ.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 704 | + } |
| 705 | + |
| 706 | + @Test |
| 707 | + void nested_try_except_2() { |
| 708 | + FileInput fileInput = inferTypes(""" |
| 709 | + try: |
| 710 | + if p: |
| 711 | + x = 42 |
| 712 | + type(x) |
| 713 | + else: |
| 714 | + x = "foo" |
| 715 | + type(x) |
| 716 | + except: |
| 717 | + type(x) |
| 718 | + def g(p): |
| 719 | + if p: |
| 720 | + y = 42 |
| 721 | + type(y) |
| 722 | + else: |
| 723 | + y = "hello" |
| 724 | + type(y) |
| 725 | + type(y) |
| 726 | + if cond: |
| 727 | + z = 42 |
| 728 | + type(z) |
| 729 | + else: |
| 730 | + z = "hello" |
| 731 | + type(z) |
| 732 | + type(z) |
| 733 | + """); |
| 734 | + List<CallExpression> calls = PythonTestUtils.getAllDescendant(fileInput, tree -> tree.is(Tree.Kind.CALL_EXPR)); |
| 735 | + RegularArgument firstX = (RegularArgument) calls.get(0).arguments().get(0); |
| 736 | + RegularArgument secondX = (RegularArgument) calls.get(1).arguments().get(0); |
| 737 | + RegularArgument thirdX = (RegularArgument) calls.get(2).arguments().get(0); |
| 738 | + assertThat(((UnionType) firstX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 739 | + assertThat(((UnionType) secondX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 740 | + assertThat(((UnionType) thirdX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 741 | + |
| 742 | + RegularArgument firstY = (RegularArgument) calls.get(3).arguments().get(0); |
| 743 | + RegularArgument secondY = (RegularArgument) calls.get(4).arguments().get(0); |
| 744 | + RegularArgument thirdY = (RegularArgument) calls.get(5).arguments().get(0); |
| 745 | + assertThat(firstY.expression().typeV2().unwrappedType()).isEqualTo(INT_TYPE); |
| 746 | + assertThat(secondY.expression().typeV2().unwrappedType()).isEqualTo(STR_TYPE); |
| 747 | + assertThat(((UnionType) thirdY.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 748 | + |
| 749 | + RegularArgument firstZ = (RegularArgument) calls.get(6).arguments().get(0); |
| 750 | + RegularArgument secondZ = (RegularArgument) calls.get(7).arguments().get(0); |
| 751 | + RegularArgument thirdZ = (RegularArgument) calls.get(8).arguments().get(0); |
| 752 | + assertThat(((UnionType) firstZ.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 753 | + assertThat(((UnionType) secondZ.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 754 | + assertThat(((UnionType) thirdZ.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 755 | + } |
| 756 | + |
| 757 | + @Test |
| 758 | + void try_except_with_dependents() { |
| 759 | + FileInput fileInput = inferTypes(""" |
| 760 | + try: |
| 761 | + x = 42 |
| 762 | + y = x |
| 763 | + z = y |
| 764 | + type(x) |
| 765 | + type(y) |
| 766 | + type(z) |
| 767 | + except: |
| 768 | + x = "hello" |
| 769 | + y = x |
| 770 | + z = y |
| 771 | + type(x) |
| 772 | + type(y) |
| 773 | + type(z) |
| 774 | + type(x) |
| 775 | + type(y) |
| 776 | + type(z) |
| 777 | + """); |
| 778 | + |
| 779 | + List<CallExpression> calls = PythonTestUtils.getAllDescendant(fileInput, tree -> tree.is(Tree.Kind.CALL_EXPR)); |
| 780 | + RegularArgument firstX = (RegularArgument) calls.get(0).arguments().get(0); |
| 781 | + RegularArgument firstY = (RegularArgument) calls.get(1).arguments().get(0); |
| 782 | + RegularArgument firstZ = (RegularArgument) calls.get(2).arguments().get(0); |
| 783 | + assertThat(((UnionType) firstX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 784 | + assertThat(((UnionType) firstY.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 785 | + assertThat(((UnionType) firstZ.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 786 | + |
| 787 | + RegularArgument secondX = (RegularArgument) calls.get(3).arguments().get(0); |
| 788 | + RegularArgument secondY = (RegularArgument) calls.get(4).arguments().get(0); |
| 789 | + RegularArgument secondZ = (RegularArgument) calls.get(5).arguments().get(0); |
| 790 | + assertThat(((UnionType) secondX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 791 | + assertThat(((UnionType) secondY.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 792 | + assertThat(((UnionType) secondZ.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 793 | + |
| 794 | + RegularArgument thirdX = (RegularArgument) calls.get(6).arguments().get(0); |
| 795 | + RegularArgument thirdY = (RegularArgument) calls.get(7).arguments().get(0); |
| 796 | + RegularArgument thirdZ = (RegularArgument) calls.get(8).arguments().get(0); |
| 797 | + assertThat(((UnionType) thirdX.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 798 | + assertThat(((UnionType) thirdY.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 799 | + assertThat(((UnionType) thirdZ.expression().typeV2()).candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 800 | + } |
| 801 | + |
| 802 | + @Test |
| 803 | + void try_except_list_attributes() { |
| 804 | + FileInput fileInput = inferTypes(""" |
| 805 | + try: |
| 806 | + my_list = [1, 2, 3] |
| 807 | + type(my_list) |
| 808 | + except: |
| 809 | + my_list = ["a", "b", "c"] |
| 810 | + type(my_list) |
| 811 | + type(my_list) |
| 812 | + """); |
| 813 | + |
| 814 | + List<CallExpression> calls = PythonTestUtils.getAllDescendant(fileInput, tree -> tree.is(Tree.Kind.CALL_EXPR)); |
| 815 | + RegularArgument list1 = (RegularArgument) calls.get(0).arguments().get(0); |
| 816 | + RegularArgument list2 = (RegularArgument) calls.get(1).arguments().get(0); |
| 817 | + RegularArgument list3 = (RegularArgument) calls.get(2).arguments().get(0); |
| 818 | + |
| 819 | + UnionType listType = (UnionType) list1.expression().typeV2(); |
| 820 | + assertThat(listType.candidates()).extracting(PythonType::unwrappedType).containsExactlyInAnyOrder(LIST_TYPE, LIST_TYPE); |
| 821 | + assertThat(listType.candidates()) |
| 822 | + .map(ObjectType.class::cast) |
| 823 | + .flatExtracting(ObjectType::attributes) |
| 824 | + .extracting(PythonType::unwrappedType) |
| 825 | + .containsExactlyInAnyOrder(INT_TYPE, STR_TYPE); |
| 826 | + |
| 827 | + assertThat(list2.expression().typeV2()).isEqualTo(listType); |
| 828 | + assertThat(list3.expression().typeV2()).isEqualTo(listType); |
| 829 | + |
| 830 | + } |
| 831 | + |
629 | 832 | private static FileInput inferTypes(String lines) {
|
630 | 833 | return inferTypes(lines, new HashMap<>());
|
631 | 834 | }
|
|
0 commit comments