As I already said, these appear to be similar issues but are not Identical. These are each independently different ways in which something can go wrong with narrowing under the current behaviors of type checkers.
- I’d fix this variance issue
- For the dict/typed dict case, I’ve already brought up other changes I think are necessary to increase both usability and safety here: Structural subtyping, EAFP, and exceptions which inform structure, and I think TypeIs likely needs a note here (Note that TypedDict is the only structural type where this issue currently exists due to the ability to remove keys, if
NotRequired
is expanded to protocols, it would apply to them too) - for the use of nonlocal to modify function locals out from under narrowing? I’m fine leaving that unsolved.
Not all of this is going to be solvable at the type system level for preventing issues, but some of it can be.