Inconsistent behavior with function / callable overrides
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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