Repository navigation
Description
Crash Report
If a decorated function is defined inside an if block and then defined again, also decorated, further down the same module, with other statements in between and after, mypy crashes in fix_function_overloads.
Depending on which statement comes right before the second definition, it can instead throw away the body of an unrelated if statement without saying anything. Errors inside that if are then not reported, and names assigned in it show up as undefined. That second behavior turned up while reducing the crash.
Traceback
mypy master at 2b4bcb3, uncompiled, mypy crash.py --show-traceback:
Traceback (most recent call last):
...
File ".../mypy/nativeparse.py", line 397, in read_statements
defs = fix_function_overloads(state, defs)
File ".../mypy/nativeparse.py", line 2184, in fix_function_overloads
assert isinstance(popped, IfStmt)
~~~~~~~~~~^^^^^^^^^^^^^^^^
AssertionErrorWith --no-native-parser the same assert fails in fix_function_overloads in mypy/fastparse.py (line 710).
To Reproduce
All three files run fine under Python 3.14. In each one, deleting the last line makes the problem go away, so that's the control.
- Crash.
crash.py:
import functools
import sys
if sys.version_info < (3, 9):
@functools.lru_cache(maxsize=None)
def f() -> int:
return 1
y = 0
@functools.cache
def f() -> int:
return 1
z = 0mypy crash.py exits 2 with INTERNAL ERROR. Without the last line it reports no issues.
- Missing error.
missing.py:
import functools
import sys
if sys.version_info < (3, 9):
@functools.lru_cache(maxsize=None)
def f() -> int:
return 1
if len(sys.argv) > 5:
bad: int = "not an int"
@functools.cache
def f() -> int:
return 1
z = 0mypy missing.py reports no issues. Without the last line it reports:
missing.py:10: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
- False positive.
false.py:
import functools
import sys
if sys.version_info < (3, 9):
@functools.lru_cache(maxsize=None)
def f() -> int:
return 1
if sys.platform == "win32":
SEP = "\\"
else:
SEP = "/"
@functools.cache
def f() -> int:
return 1
print(SEP)mypy false.py reports:
false.py:18: error: Name "SEP" is not defined [name-defined]
Without the last line it reports no issues.
From trying variations, what it takes is, within one block (module, class body or function body) and in this order: an if whose body is only a decorated function, or a run of overloads, named f; some other statement; a later decorated def f or more @overloads of f; and at least one statement after that. The decorators don't have to be @overload. If the statement right before the second f is an if, you get the dropped body, otherwise the crash.
One caveat: in all three files the first block is unreachable for mypy (sys.version_info < (3, 9)). That's the only way I found to get this in a file mypy otherwise accepts. If the block is reachable, mypy also reports no-redef on the second definition, which is right, but the crash and the dropped if body still happen. That's the case I actually hit: mutmut (a mutation testing tool) rewrites a module so that a statement ends up between if TYPE_CHECKING: overloads and the implementation, decorates the implementation, and then runs mypy on the result (boxed/mutmut#667).
Expected Behavior
The same output as without the last line: no errors for 1 and 3, and the assignment error on line 10 for 2.
Cause
fix_function_overloads (both the nativeparse.py and the fastparse.py copy) records the name of the conditional block in last_if_stmt_overload_name when it appends the pending IfStmt to ret. That name is only cleared when another such if block starts a run. So when a later run with the same name ends, the current_overload_name == last_if_stmt_overload_name check passes again, and ret.pop() removes whatever happens to be last, assuming it's the IfStmt that was just appended. In file 1 that's y = 0, which trips the assert. In files 2 and 3 it's the unrelated if, which passes the assert and gets its contents removed by strip_contents_from_if_stmt.
To check this I set last_if_stmt_overload_name = None right after the pop, in both files, on master (2b4bcb3). With that, all three files give the expected output. I haven't looked into whether that's the right fix.
Your Environment
- Mypy version used: 2.4.1 (compiled), and master at 2b4bcb3 (uncompiled) with and without
--no-native-parser. Same results for all three files on each. - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.14.7
- Operating system and version: macOS 27.2.1
This goes back to #10712, first released in 0.940: crash.py crashes on 0.940, 1.0.0, 1.15.0, 2.0.0 and 2.4.1, and passes on 0.931.
Activity
willfrey commentedon Dec 3, 2026
Correction to the Cause section: I wrote that setting
last_if_stmt_overload_name = Noneright after the pop makes all three files pass. It doesn't fix any of them. In each file, the name is recorded when the statement after theifflushes it, and the first pop forfis already the wrong one, so clearing the name afterward changes nothing. I reran the three files on 2b4bcb3 with that change, under both parsers, and got the same output as unpatched. #22593 gives the expected output for all three. Sorry for the noise.