python / python/mypy

False positive with assignment expression shadowing a builtin

Open
#15,904 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-pep-572
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=a7fd112a423a2e565e5f2cd3e3607e58

def foo(x: int) -> int:
    res = vars + 1 if (vars := x) else 0
    return res

Expected Behavior

This should type check without errors. It does if I rename vars to anything that is not a name of a built-in.

Actual Behavior

main.py:2: error: Unsupported operand types for + (overloaded function and "int")  [operator]

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags: no
  • Mypy configuration options from mypy.ini (and other config files): no
  • Python version used: 3.11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked mypy-play reproducer with Python 3.11 and mypy 1.5.1, then trace how the assignment expression and the name vars are analyzed. Done means the example type checks without the reported operator error while preserving the existing behavior for builtin names.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.