Commit 101070c
committed
Fix overload resolution returning Any instead of Never when argument contains Any
When a function has overloads where one returns Never and another returns a
concrete type, and the argument has type Any (causing overload ambiguity),
mypy was falling back to Any instead of using the Never-returning overload.
Never is a subtype of every type, so an overload returning Never is always
the most specific possible result. When Any-argument ambiguity fires and
exactly one matching overload returns Never, there is no real ambiguity to
resolve: the Never overload is the right answer.
This was particularly visible with iter() on generic classes that have both
__iter__ (returning Never) and __getitem__: iter(x) was returning Any when
x had an Any type argument, while x.__iter__() correctly returned Never.
Fixes #210271 parent f779f77 commit 101070c
2 files changed
Lines changed: 43 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3092 | 3092 | | |
3093 | 3093 | | |
3094 | 3094 | | |
| 3095 | + | |
| 3096 | + | |
| 3097 | + | |
| 3098 | + | |
| 3099 | + | |
| 3100 | + | |
| 3101 | + | |
| 3102 | + | |
| 3103 | + | |
| 3104 | + | |
| 3105 | + | |
| 3106 | + | |
| 3107 | + | |
| 3108 | + | |
3095 | 3109 | | |
3096 | 3110 | | |
3097 | 3111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1906 | 1906 | | |
1907 | 1907 | | |
1908 | 1908 | | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
| 1913 | + | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
| 1919 | + | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
1909 | 1938 | | |
1910 | 1939 | | |
1911 | 1940 | | |
| |||
0 commit comments