|
11 | 11 | import unittest |
12 | 12 | from collections.abc import Iterator |
13 | 13 | from typing import Any, Callable |
| 14 | +import unittest.mock |
14 | 15 |
|
15 | 16 | import mypy.stubtest |
16 | 17 | from mypy import build, nodes |
@@ -1666,104 +1667,108 @@ def test_not_subclassable(self) -> Iterator[Case]: |
1666 | 1667 |
|
1667 | 1668 | @collect_cases |
1668 | 1669 | def test_disjoint_base(self) -> Iterator[Case]: |
1669 | | - yield Case( |
1670 | | - stub=""" |
1671 | | - class A: pass |
1672 | | - """, |
1673 | | - runtime=""" |
1674 | | - class A: pass |
1675 | | - """, |
1676 | | - error=None, |
1677 | | - ) |
1678 | | - yield Case( |
1679 | | - stub=""" |
1680 | | - from typing_extensions import disjoint_base |
| 1670 | + # TODO: Remove this patch once PEP 800 is accepted |
| 1671 | + with unittest.mock.patch.object( |
| 1672 | + sys, "argv", new=sys.argv + ["--check-disjoint-bases"] |
| 1673 | + ): |
| 1674 | + yield Case( |
| 1675 | + stub=""" |
| 1676 | + class A: pass |
| 1677 | + """, |
| 1678 | + runtime=""" |
| 1679 | + class A: pass |
| 1680 | + """, |
| 1681 | + error=None, |
| 1682 | + ) |
| 1683 | + yield Case( |
| 1684 | + stub=""" |
| 1685 | + from typing_extensions import disjoint_base |
1681 | 1686 |
|
1682 | | - @disjoint_base |
1683 | | - class B: pass |
1684 | | - """, |
1685 | | - runtime=""" |
1686 | | - class B: pass |
1687 | | - """, |
1688 | | - error="test_module.B", |
1689 | | - ) |
1690 | | - yield Case( |
1691 | | - stub=""" |
1692 | | - from typing_extensions import Self |
| 1687 | + @disjoint_base |
| 1688 | + class B: pass |
| 1689 | + """, |
| 1690 | + runtime=""" |
| 1691 | + class B: pass |
| 1692 | + """, |
| 1693 | + error="test_module.B", |
| 1694 | + ) |
| 1695 | + yield Case( |
| 1696 | + stub=""" |
| 1697 | + from typing_extensions import Self |
1693 | 1698 |
|
1694 | | - class mytakewhile: |
1695 | | - def __new__(cls, predicate: object, iterable: object, /) -> Self: ... |
1696 | | - def __iter__(self) -> Self: ... |
1697 | | - def __next__(self) -> object: ... |
1698 | | - """, |
1699 | | - runtime=""" |
1700 | | - from itertools import takewhile as mytakewhile |
1701 | | - """, |
1702 | | - # Should have @disjoint_base |
1703 | | - error="test_module.mytakewhile", |
1704 | | - ) |
1705 | | - yield Case( |
1706 | | - stub=""" |
1707 | | - from typing_extensions import disjoint_base, Self |
| 1699 | + class mytakewhile: |
| 1700 | + def __new__(cls, predicate: object, iterable: object, /) -> Self: ... |
| 1701 | + def __iter__(self) -> Self: ... |
| 1702 | + def __next__(self) -> object: ... |
| 1703 | + """, |
| 1704 | + runtime=""" |
| 1705 | + from itertools import takewhile as mytakewhile |
| 1706 | + """, |
| 1707 | + # Should have @disjoint_base |
| 1708 | + error="test_module.mytakewhile", |
| 1709 | + ) |
| 1710 | + yield Case( |
| 1711 | + stub=""" |
| 1712 | + from typing_extensions import disjoint_base, Self |
1708 | 1713 |
|
1709 | | - @disjoint_base |
1710 | | - class mycorrecttakewhile: |
1711 | | - def __new__(cls, predicate: object, iterable: object, /) -> Self: ... |
1712 | | - def __iter__(self) -> Self: ... |
1713 | | - def __next__(self) -> object: ... |
1714 | | - """, |
1715 | | - runtime=""" |
1716 | | - from itertools import takewhile as mycorrecttakewhile |
1717 | | - """, |
1718 | | - error=None, |
1719 | | - ) |
1720 | | - yield Case( |
1721 | | - runtime=""" |
1722 | | - class IsDisjointBaseBecauseItHasSlots: |
1723 | | - __slots__ = ("a",) |
1724 | | - a: int |
1725 | | - """, |
1726 | | - stub=""" |
1727 | | - from typing_extensions import disjoint_base |
| 1714 | + @disjoint_base |
| 1715 | + class mycorrecttakewhile: |
| 1716 | + def __new__(cls, predicate: object, iterable: object, /) -> Self: ... |
| 1717 | + def __iter__(self) -> Self: ... |
| 1718 | + def __next__(self) -> object: ... |
| 1719 | + """, |
| 1720 | + runtime=""" |
| 1721 | + from itertools import takewhile as mycorrecttakewhile |
| 1722 | + """, |
| 1723 | + error=None, |
| 1724 | + ) |
| 1725 | + yield Case( |
| 1726 | + runtime=""" |
| 1727 | + class IsDisjointBaseBecauseItHasSlots: |
| 1728 | + __slots__ = ("a",) |
| 1729 | + a: int |
| 1730 | + """, |
| 1731 | + stub=""" |
| 1732 | + from typing_extensions import disjoint_base |
1728 | 1733 |
|
1729 | | - @disjoint_base |
1730 | | - class IsDisjointBaseBecauseItHasSlots: |
1731 | | - a: int |
1732 | | - """, |
1733 | | - error="test_module.IsDisjointBaseBecauseItHasSlots", |
1734 | | - ) |
1735 | | - yield Case( |
1736 | | - runtime=""" |
1737 | | - class IsFinalSoDisjointBaseIsRedundant: ... |
1738 | | - """, |
1739 | | - stub=""" |
1740 | | - from typing_extensions import disjoint_base, final |
| 1734 | + @disjoint_base |
| 1735 | + class IsDisjointBaseBecauseItHasSlots: |
| 1736 | + a: int |
| 1737 | + """, |
| 1738 | + error="test_module.IsDisjointBaseBecauseItHasSlots", |
| 1739 | + ) |
| 1740 | + yield Case( |
| 1741 | + runtime=""" |
| 1742 | + class IsFinalSoDisjointBaseIsRedundant: ... |
| 1743 | + """, |
| 1744 | + stub=""" |
| 1745 | + from typing_extensions import disjoint_base, final |
1741 | 1746 |
|
1742 | | - @final |
1743 | | - @disjoint_base |
1744 | | - class IsFinalSoDisjointBaseIsRedundant: ... |
1745 | | - """, |
1746 | | - error="test_module.IsFinalSoDisjointBaseIsRedundant", |
1747 | | - ) |
1748 | | - yield Case( |
1749 | | - runtime=""" |
1750 | | - import enum |
| 1747 | + @final |
| 1748 | + @disjoint_base |
| 1749 | + class IsFinalSoDisjointBaseIsRedundant: ... |
| 1750 | + """, |
| 1751 | + error="test_module.IsFinalSoDisjointBaseIsRedundant", |
| 1752 | + ) |
| 1753 | + yield Case( |
| 1754 | + runtime=""" |
| 1755 | + import enum |
1751 | 1756 |
|
1752 | | - class IsEnumWithMembersSoDisjointBaseIsRedundant(enum.Enum): |
1753 | | - A = 1 |
1754 | | - B = 2 |
1755 | | - """, |
1756 | | - stub=""" |
1757 | | - from typing_extensions import disjoint_base |
1758 | | - import enum |
| 1757 | + class IsEnumWithMembersSoDisjointBaseIsRedundant(enum.Enum): |
| 1758 | + A = 1 |
| 1759 | + B = 2 |
| 1760 | + """, |
| 1761 | + stub=""" |
| 1762 | + from typing_extensions import disjoint_base |
| 1763 | + import enum |
1759 | 1764 |
|
1760 | | - @disjoint_base |
1761 | | - class IsEnumWithMembersSoDisjointBaseIsRedundant(enum.Enum): |
1762 | | - A = 1 |
1763 | | - B = 2 |
1764 | | - """, |
1765 | | - error="test_module.IsEnumWithMembersSoDisjointBaseIsRedundant", |
1766 | | - ) |
| 1765 | + @disjoint_base |
| 1766 | + class IsEnumWithMembersSoDisjointBaseIsRedundant(enum.Enum): |
| 1767 | + A = 1 |
| 1768 | + B = 2 |
| 1769 | + """, |
| 1770 | + error="test_module.IsEnumWithMembersSoDisjointBaseIsRedundant", |
| 1771 | + ) |
1767 | 1772 |
|
1768 | 1773 | @collect_cases |
1769 | 1774 | def test_has_runtime_final_decorator(self) -> Iterator[Case]: |
|
0 commit comments