Indexing TypedDict with a non-literal variable

Option 2 allows each type checker to decide the level of safety for that case individually. So option 2 does not necessarily removes the true positive, it just makes it not mandatory to all type checkers.

But yes, if type checker decide not to reject str, then it does remove the true positive for this case. See pyright-play:


Just to expand on this, with introduction of PEP 728: TypedDict with Typed Extra Items, if type checkers adapt it, it will be much easier to narow str to individual literals for a closed typed dict and rejecting non-literal keys may seem more appealing.

1 Like