Skip to content

Discard walrus narrowing from repeated inference of call arguments - #22142

Open
rheard wants to merge 1 commit into
python:masterfrom
rheard:fix-22138
Open

rheard wants to merge 1 commit into
python:masterfrom
rheard:fix-22138

Conversation

@rheard

@rheard rheard commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #22138

mypy infers the arguments of some calls more than once, and narrowing from an assignment expression in one inference stayed in the binder for the next one. The arguments before the assignment, and the assignment's own value, were then inferred with the narrowed type. This happens in:

  • generic calls (the first and second inference passes, before the final check against the inferred signature)
  • overloaded calls (the first inference in empty context, then once per item)
  • calls on a union of callables (once per item)
  • arguments matched to *args/**kwargs (inferred while mapping actuals to formals)
  • x.append(...) and similar when x has a partial type

This uses the same tool as #20622 used for return values: inferences that are repeated later run in a binder frame with discard=True. The last inference keeps its narrowing as before: the final check of a generic call, the last item of a union, and the fallback check when no overload item matches. Overloaded calls are the exception, since every item is inferred in a discarded frame. When an overload result is used, the arguments are inferred once more just for the narrowing. This only happens if an assignment expression changed the binder during the call, tracked with assignment_expression_effect from #21294, so other calls don't infer their arguments an extra time.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assignment expression in a call argument changes the types of earlier arguments

1 participant