Make __replace__ stop interfering with variance inference

If type checker notice that covariance is an issue, they could point to the methods that cause this variance - especially if it’s a single synthesized method. I don’t know enough about type checker implementations to create a POC, but I would be surprised if this is information that is hard to get for them.

E.g. the pyright error message for the example in OP:

Method "f" overrides class "X" in an incompatible manner
  Return type mismatch: base method returns type "C[object]", override returns type "C[float]"
    "C[float]" is not assignable to "C[object]"
      Type parameter "T@C" is invariant, but "float" is not the same as "object"  (reportIncompatibleMethodOverride)

Could be extended to include

"T@C" is invariant because of the synthesized "__replace__" method.

The inclusion or exclusion of this message could be controlled by heuristics and/or type checker options.

2 Likes