Closed
Description
Bug Report
Mypy doesn't seem to like it when you check if an argument with typevar type is checked to see if it is None
. It incorrectly reports error: Statement is unreachable [unreachable]
To Reproduce
import typing as t
T = t.TypeVar("T")
def foo(arg: T) -> None:
if arg is None:
return None
# Do something with arg
return None
foo(None) ## Allowed
foo(123)
Run mypy with --warn-unreachable
flag
Expected Behavior
No errors
Actual Behavior
$ mypy .vscode/scratch.py
.vscode/scratch.py: note: In function "foo":
.vscode/scratch.py:9: error: Statement is unreachable [unreachable]
Found 1 error in 1 file (checked 1 source file)
The error seems to only happen when checking if arg is None
, but not with other types. For example, I tired changing if arg is None:
line to the following and these were the results.
if arg is True:
-> no errorsif isinstance(arg, int):
-> no errorsif isinstance(arg, type(None)):
-> unreachable errorif arg == None:
-> No error
Also, changing the type to T | None
removes the error as well.
Your Environment
- Mypy version used: mypy 1.13.0 (compiled: yes)
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini
(and other config files):warn_unreachable = True
- Python version used: 3.12