You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make dict expression inference more consistent (#15174)
Fixes#12977
IMO current dict expression inference logic is quite arbitrary: we only
take the non-star items to infer resulting type, then enforce it on the
remaining (star) items. In this PR I simplify the logic to simply put
all expressions as arguments into the same call. This has following
benefits:
* Makes everything more consistent/predictable.
* Fixes one of top upvoted bugs
* Makes dict item indexes correct (previously we reshuffled them causing
wrong indexes for non-star items after star items)
* No more weird wordings like `List item <n>` or `Argument <n> to
"update" of "dict"`
* I also fix the end position of generated expressions to show correct
spans in errors
The only downside is that we will see `Cannot infer type argument` error
instead of `Incompatible type` more often. This is because
`SupportsKeysAndGetItem` (used for star items) is invariant in key type.
I think this is fine however, since:
* This only affects key types, that are mixed much less often than value
types (they are usually just strings), and for latter we use joins.
* I added a dedicated note for this case
Copy file name to clipboardExpand all lines: test-data/unit/pythoneval.test
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1350,7 +1350,7 @@ def f() -> Dict[int, str]:
1350
1350
def d() -> Dict[int, int]:
1351
1351
return {}
1352
1352
[out]
1353
-
_testDictWithStarStarSpecialCase.py:4: error: Argument 1 to "update" of "MutableMapping" has incompatible type "Dict[int, int]"; expected "SupportsKeysAndGetItem[int, str]"
1353
+
_testDictWithStarStarSpecialCase.py:4: error: Unpacked dict entry 1 has incompatible type "Dict[int, int]"; expected "SupportsKeysAndGetItem[int, str]"
1354
1354
1355
1355
[case testLoadsOfOverloads]
1356
1356
from typing import overload, Any, TypeVar, Iterable, List, Dict, Callable, Union
0 commit comments