Repository navigation
Commit 59196ba
authored
[ty] Unify polarity-aware relation construction (#27707)
## Summary
Part of astral-sh/ty#3557 and the second step in rebooting #26712 as
smaller focused changes.
On `main`, generic inference does not consistently propagate the
variance of an enclosing generic class into structural-protocol and
callable comparisons. Those comparisons instead construct the forward
`actual <= formal` relation even when the surrounding position is
contravariant or invariant. This produces incorrect bounds and inferred
types:
```py
from typing import Callable
class Middle: ...
class Derived(Middle): ...
class Sink[T]:
def put(self, value: T) -> None: ...
def infer[T](sink: Sink[Callable[[], T]], value: T) -> T:
return value
reveal_type(infer(Sink[Callable[[], Middle]](), Derived()))
# main: Middle
# this PR: Derived
```
This PR centralizes relation construction so inference consistently
follows the surrounding polarity: covariance requires `actual <=
formal`, contravariance requires `formal <= actual`, invariance requires
both, and bivariance adds no constraints. The fix applies to nested
generic protocols, callable signatures, callback protocols,
TypedDict/protocol relations, and materialized protocols while
preserving existing nominal inference, callable-union alternatives,
TypedDict-union optimizations, and cycle-safe handling of recursive
protocols.
## Limitation
The new constraint solver does not yet support `ParamSpec` or
`TypeVarTuple`. Whenever an inference context includes either variadic,
all affected relations must continue through the old, forward-only
inference path, including comparisons involving ordinary type variables
alongside an unrelated variadic. This deliberately avoids regressions in
parameter inference, structural protocols, descriptor signatures, and
downstream argument diagnostics, but it also means the polarity fixes in
this PR do not apply whenever variadics are involved. Focused TODO tests
document cases that still incorrectly infer `Middle` instead of
`Derived`; fixing those cases requires adding variadic support to the
new solver.
## Test plan
Added mdtests cover nested nominal classes, generic protocols, callable
signatures, and callback protocols under covariance, contravariance, and
invariance; double contravariance, callable-union alternatives, and
invariant rejection; finite, recursive, and recursive-only materialized
protocols; invariant, contravariant, and covariant `ParamSpec` inference
through `Concatenate`; bounded and constrained prefixes, descriptor
receivers, structural protocol unions, nominal callable objects,
higher-order callbacks, and mixed ordinary/variadic inference; analogous
`TypeVarTuple` callable, protocol, structural-union, and
nominal-implementation cases; and explicit TODO coverage for unrelated
variadics suppressing otherwise-correct polarity.
Additional mdtests cover repeated comparisons of the same protocol or
callable under opposite polarities, plus overloaded callable acceptance
and rejection across covariant, contravariant, and invariant wrappers.1 parent b8c5e73 commit 59196ba
6 files changed
Lines changed: 919 additions & 71 deletions
File tree
- crates/ty_python_semantic
- resources/mdtest
- generics
- legacy
- pep695
- type_properties
- src/types
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1864 | 1864 | | |
1865 | 1865 | | |
1866 | 1866 | | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
1867 | 1890 | | |
1868 | 1891 | | |
1869 | 1892 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
746 | 746 | | |
747 | 747 | | |
748 | 748 | | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
749 | 915 | | |
750 | 916 | | |
751 | 917 | | |
| |||
0 commit comments