Fix #5722 by making methodswith check TypeVar.ub