Skip to content

Fix partial type assignment with walrus expressions - #22282

Closed
zhengguangzhuo wants to merge 2 commits into
python:masterfrom
zhengguangzhuo:zhengguangzhuo/mypy-partial-walrus-22262
Closed

zhengguangzhuo wants to merge 2 commits into
python:masterfrom
zhengguangzhuo:zhengguangzhuo/mypy-partial-walrus-22262

Conversation

@zhengguangzhuo

Copy link
Copy Markdown

Fixes #22262.

When a local variable starts as None and an assignment's right-hand side contains a walrus assignment to that same variable, the walrus can resolve the variable's partial type while the outer assignment is being checked. The outer assignment then skips the compatibility check, allowing an incompatible value to pass silently.

If evaluating the right-hand side has already resolved the partial type, check the inferred right-hand-side type against the variable's current type. Add a regression test for the reported tuple assignment.

Validation:

  • runtests.py check-python38.test — 63 passed.
  • python -m mypy --config-file mypy_self_check.ini -p mypy — passed for 196 source files.
  • git diff --check — passed.

This change was prepared with AI assistance; the validation above was run locally.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
- sphinx/builders/_epub_base.py:34:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

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.

False negative involving partial assignment and walrus

2 participants