Gnats were in my bag of potatoes; can I still cook and eat the potatoes?
Opinion answer, no authority here, just going by what to me is common practice.

Potatoes are grown in the ground with dirt and bugs around all the time.  In general, not a big deal.  Gnats or small fruit flies are not an issue for a relatively hard and skinned item. As long as there is no soft spots or rot, just scrub then and you should be rid of any eggs they may have laid.

However, if the potatoes themselves were doing the attracting, that is a sign you may have rotting potaties. Those potatoes involved I would consider not only contaminated, but in danger of contaminating the remainder of the bag, drawing more unwanted visitors, and ready to smell up your world. I would remove any that show such signs.  

If it is more than one or two, well, potatoes are relatively cheap in most places, so time for a new supply.