Skip to content

Commit

Permalink
Fix internal crash when resolve same partial type twice (python#14552)
Browse files Browse the repository at this point in the history
Fixes: python#14548

Fixed case when untyped list item type resolving can lead to an internal
crash.

Code to reproduce this issue:
```py
arr = []
arr.append(arr.append(1))
```

Basically, the issue is that after the first resolving of `arr.append`
method, `var` is deleted from `partial_types`, and as war as
`arr.append` is a nested call we try to delete the same `var` that was
already deleted.
  • Loading branch information
uriyyo authored and hauntsaninja committed Jan 29, 2023
1 parent b7c850c commit 1948c38
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,8 @@ def try_infer_partial_type(self, e: CallExpr) -> None:
return
var, partial_types = ret
typ = self.try_infer_partial_value_type_from_call(e, callee.name, var)
if typ is not None:
# Var may be deleted from partial_types in try_infer_partial_value_type_from_call
if typ is not None and var in partial_types:
var.type = typ
del partial_types[var]
elif isinstance(callee.expr, IndexExpr) and isinstance(callee.expr.base, RefExpr):
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,13 @@ class A:
[out]
main:4: error: "None" has no attribute "__iter__" (not iterable)

[case testPartialTypeErrorSpecialCase4]
# This used to crash.
arr = []
arr.append(arr.append(1))
[builtins fixtures/list.pyi]
[out]
main:3: error: "append" of "list" does not return a value

-- Multipass
-- ---------
Expand Down

0 comments on commit 1948c38

Please sign in to comment.