That's a mypy bug. I have fixed that in #19422 a few days ago, your original snippet works on mypy master (will likely be released in 1.18.0).