python / python/mypy

Inconsistent behavior with function / callable overrides

Open
#11,510 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Mypy appears to allow a callable variable to be overridden by a function declaration (a symbol defined using def statement) if the signatures match and they are in different suites (e.g. one in the "if" block and the other in an "else" block). If the order is reversed, mypy generates an error.

Is this intended or a bug? It strikes me as inconsistent.

def func1(cond: bool):
    if cond:
        def a(a: int, /) -> None:
            return None

        b: Callable[[int], None] = lambda a: None

    else:
        a: Callable[[int], None] = lambda a: None  # Mypy error: Name "a" already defined

        def b(a: int, /) -> None:
            return None

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

Use the provided func1 example as the entry point; compare mypy's diagnostics when the def declaration and Callable assignment occur in each branch ordering. Done means the differing behavior is explained and, if confirmed as a bug, both orderings are covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.