Commit 2fbfb60
authored
Fix inference with UninhabitedType (python#16994)
At the moment, inference fails if an empty dict is used (without
annotation) as one of the types. It's because the constraint solver
can't resolve `dict[str, int]` and `dict[Never, Never]`. However in this
case it's more reasonable to interpret the empty dict as `dict[Any,
Any]` and just using the first type instead. That matches the behavior
of pyright.
```py
T = TypeVar("T")
class A(Generic[T]): ...
def func1(a: A[T], b: T) -> T: ...
def a1(a: A[Dict[str, int]]) -> None:
reveal_type(func1(a, {}))
```
```
# before
main: error: Cannot infer type argument 1 of "func1" (diff)
main: note: Revealed type is "Any" (diff)
# after
main: note: Revealed type is "builtins.dict[builtins.str, builtins.int]"
```1 parent 2f0f8f2 commit 2fbfb60
2 files changed
+43
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
112 | 116 | | |
113 | 117 | | |
114 | | - | |
115 | | - | |
116 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
117 | 122 | | |
118 | 123 | | |
119 | 124 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3813 | 3813 | | |
3814 | 3814 | | |
3815 | 3815 | | |
| 3816 | + | |
| 3817 | + | |
| 3818 | + | |
| 3819 | + | |
| 3820 | + | |
| 3821 | + | |
| 3822 | + | |
| 3823 | + | |
| 3824 | + | |
| 3825 | + | |
| 3826 | + | |
| 3827 | + | |
| 3828 | + | |
| 3829 | + | |
| 3830 | + | |
| 3831 | + | |
| 3832 | + | |
| 3833 | + | |
| 3834 | + | |
| 3835 | + | |
| 3836 | + | |
| 3837 | + | |
| 3838 | + | |
| 3839 | + | |
| 3840 | + | |
| 3841 | + | |
| 3842 | + | |
| 3843 | + | |
| 3844 | + | |
| 3845 | + | |
| 3846 | + | |
| 3847 | + | |
| 3848 | + | |
| 3849 | + | |
0 commit comments