Make __replace__ stop interfering with variance inference

It makes sense that variables in a NamedTuple and a frozen dataclass should be considered read-only and should therefore not cause a TypeVar to be inferred as invariant.

Interestingly, Python 3.13’s addition of a __replace__ method effectively breaks this.

Seems really related to what I was worried about in Proposal: Optional Explicit covariance/contravariance for PEP 695.

Maybe explicit is better than implicit after all.

1 Like